Repository navigation
Only dup reflections that are serialized - #2501
Open
sucicfilip wants to merge 2 commits into
Open
sucicfilip wants to merge 2 commits into
sucicfilip wants to merge 2 commits into
Conversation
Serializer#associations deep-duped every class reflection on each serializer instance, even ones skipped by `excluded?` or the include directive. Dup each reflection lazily, once it passes both checks. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Purpose
Serializer#associationsdeep-dups every class reflection on each serializer instance, even the ones it then skips because ofexcluded?or the include directive. With sparse fieldsets most reflections are skipped, so most of those copies are thrown away right after they are made.Changes
self.class._reflectionsand dup each reflection lazily, once it passesexcluded?andinclude_directive.key?.associationscalls reuse it, same as before.excluded?andkey?only read the class reflection, they don't mutate it.Caveats
excluded?still runs beforeinclude_directive.key?, soif:/unless:conditions are evaluated for unrequested associations like before. Swapping the two checks would remove most of the remaining cost, but it changes when user conditions run, so I left it out.Related GitHub issues
#2270
Additional helpful information
1000 PORO records, JSON:API adapter,
fields: { posts: [:name] },belongs_toassociations, none requested. Ruby 3.4.7, Rails 8.1, median of 40 renders, 3 processes each:Allocations per record with 20 associations: 111 -> 90.