-
Notifications
You must be signed in to change notification settings - Fork 21.6k
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
ActiveRecord initialization optimizations #29215
Changes from 3 commits
a24912c
bfc62fe
63dd12b
fb6ccce
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -217,7 +217,7 @@ def type_condition(table = arel_table) | |
def subclass_from_attributes(attrs) | ||
attrs = attrs.to_h if attrs.respond_to?(:permitted?) | ||
if attrs.is_a?(Hash) | ||
subclass_name = attrs.with_indifferent_access[inheritance_column] | ||
subclass_name = attrs[inheritance_column] || attrs[inheritance_column.to_sym] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, the default value of |
||
|
||
if subclass_name.present? | ||
find_sti_class(subclass_name) | ||
|
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.
This is avoiding the wasted shallow dup in
initialize_dup
below, right?Does
dup.tap do |copy| copy.attributes.transform_values!(&:deep_dup) end
bench similarly?My only hesitation about using
new
is that while it currently saves an allocation, it seems more likely to become the costlier of the two at some point in the future.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.
Yes, this change is to avoid the wasted shallow dup in
initialize_dup
.I think using
Class#allocate
may be the best approach:That seems to do exactly what we want: create a new instance of
AttributeSet
without callinginitialize
, so that we can initialize@attributes
(and any future instance variables) ourselves withindeep_dup
and avoid wasted shallow duplication. (This is effectively whatObject#dup
does under the hood.)As you would expect, this variation provides the same speedup as the current optimization.
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.
@matthewd I went ahead and pushed that change. Let me know what you think.