Performance improvement: Avoid attribute_names in ActiveRecord after_initialize state check - #888
Open
sergioisidoro wants to merge 1 commit into
Conversation
aasm_ensure_initial_state runs as an after_initialize callback for every record instantiated from the database. Its column check, attribute_names.include?, goes through AttributeSet#keys, which builds an ActiveModel::Attribute for every column of the record and so defeats ActiveRecord's lazy attribute set. For wide tables this dominates load time (10k rows x 50 columns on Rails 8.1: ~255 ms -> ~66 ms). Use has_attribute? instead: a constant-time lookup that is likewise false for columns left out of a partial select. has_attribute? resolves alias_attribute names while attribute_names does not, so aliased columns keep the original check to preserve behaviour. The state reader is now called once instead of up to twice. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.
aasm_ensure_initial_stateruns as anafter_initializecallback for every record instantiated from the database.Its column check,
attribute_names.include?, goes throughAttributeSet#keys, which builds anActiveModel::Attributefor every column of the record and so defeats ActiveRecord's lazy attribute set.For wide tables this dominates load time (10k rows x 50 columns on Rails 8.1: ~255 ms -> ~66 ms).
Use
has_attribute?instead: a constant-time lookup that is likewise false for columns left out of a partial select.has_attribute?resolvesalias_attributenames whileattribute_namesdoes not, so aliased columns keep the original check to preserve behaviour (and will not benefit from this speedup)Inlcudes a test for the new behaviour for alias.
Disclaimer: I used AI to make this PR, but reviewed things manually. Benchmarks of this change for our application follow bellow. We use Alba with OJ writer. Used stackprof to analyse our test suite runs. The tests were made with 5.5.2, but they also apply to v6 (no changed to this piece of code since then)
Edit: Here's v6 also, same, but ran the test anyway just in case.