Sitelet https://github.com/aasm/aasm/pull/888
Skip to content

Performance improvement: Avoid attribute_names in ActiveRecord after_initialize state check - #888

Open
sergioisidoro wants to merge 1 commit into
aasm:masterfrom
mestamaster:perf/ensure-initial-state-without-attribute-names
Open

sergioisidoro wants to merge 1 commit into
aasm:masterfrom
mestamaster:perf/ensure-initial-state-without-attribute-names

Conversation

@sergioisidoro

@sergioisidoro sergioisidoro commented Sep 25, 2026 •

Copy link
Copy Markdown

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 (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)

  ┌──────────────────────────────────────┬─────────────────────┬───────────────┬─────────────┐
  │          Entities                    │ AASM 5.5.2 (median) │ Fork (median) │  Speed-up   │
  ├──────────────────────────────────────┼─────────────────────┼───────────────┼─────────────┤
  │ Load 8.5k entities                   │ 268 ms              │ 121 ms        │ 2.2×        │
  ├──────────────────────────────────────┼─────────────────────┼───────────────┼─────────────┤
  │ Full index 8.5k (load + Alba + JSON) │ 466 ms              │ 351 ms        │ 1.3× (−25%) │
  ├──────────────────────────────────────┼─────────────────────┼───────────────┼─────────────┤
  │ Full index, page = 1000              │ 53 ms               │ 39 ms         │ 1.4× (−26%) │
  └──────────────────────────────────────┴─────────────────────┴───────────────┴─────────────┘

Edit: Here's v6 also, same, but ran the test anyway just in case.

┌────────────────────────────────────────────┬──────────────────────┬──────────────────┬──────────┐
│                                            │ v6 upstream (median) │ v6 fork (median) │ Speed-up │
├────────────────────────────────────────────┼──────────────────────┼──────────────────┼──────────┤
│ Load 8.5k entities                         │ 238 ms               │ 105 ms           │ 2.3×     │
├────────────────────────────────────────────┼──────────────────────┼──────────────────┼──────────┤
│ Full index 8.5k (load + Alba + JSON)       │ 435 ms               │ 334 ms           │ 1.3×     │
├────────────────────────────────────────────┼──────────────────────┼──────────────────┼──────────┤
│ Full index, page  = 1000                   │ 49 ms                │ 36 ms            │ 1.4×     │
└────────────────────────────────────────────┴──────────────────────┴──────────────────┴──────────┘

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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant