-
-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve object shape friendliness #20
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good! What if we use a single @associated_objects
hash ivar?
We can initialize it to nil
in init_internals
and then assign it in a reader method?
I considered that, but didn't want to add a Hash allocation to every single ActiveRecord instance of a model with at least 1 associated object. |
Oh, you'd initialize it to |
Co-authored-by: Kasper Timm Hansen <hey@kaspth.com>
@kaspth it should be good to go now |
Oh, the documentation needs to change too I guess |
I'll fix up the documentation after merging. |
I'm mimicking what Jean did here:
rails/rails#47023
rails/rails#47032
rails/rails#47049
Otherwise, if you've got multiple associated objects on a single class, you can end up with a lot of permutations of object shapes because the instance variables are lazily defined in whatever order the associated objects may or may not have been called in.
Author has these 3 instance variables:
@archiver
@classified
@fortification
That leaves you with these permutations of object shapes:
@archiver
@archiver @classified
@archiver @fortification
@archiver @classified @fortification
@archiver @fortification @classified
@classified
@classified @archiver
@classified @fortification
@classified @archiver @fortification
@classified @fortification @archiver
@fortification
@fortification @archiver
@fortification @classified
@fortification @archiver @classified
@fortification @classified @archiver
Now, we'll have 1 shape:
@archiver @classified @fortification