Skip to content

Settle the attributes iterator's per-scope state once per pass - #2928

Closed
ericproulx wants to merge 1 commit into
masterfrom
perf/attributes-iterator-per-pass-state
Closed

ericproulx wants to merge 1 commit into
masterfrom
perf/attributes-iterator-per-pass-state

Conversation

@ericproulx

Copy link
Copy Markdown
Contributor

Summary

For every element of an array scope, and for every validator on it, AttributesIterator#do_each re-derived things that depend only on the scope and the request: whether the scope iterates elements, whether its params are an Array, which scope an element's index is recorded against, and the request's ParamScopeTracker (a fiber-storage read). They are now settled once per #each and handed down, and the top-level call no longer allocates its empty parent_indices. An element-iterating scope whose params are not an Array returns before iterating, since it yielded and recorded nothing anyway.

One validator's iteration overhead on a ten-element array: 5.46 → 4.64 µs.

Missing spec, added

The branch where a lateral scope (a with group inside an array) records each element's index against its nearest array ancestor had no coverage: removing it passed the whole suite, while errors then named the wrong element (array[2][b] for the first element, and two errors collapsed into one). The new spec fails a first and a last element; it passes before and after this change, and fails with that branch removed.

Benchmarks

Median of 9 interleaved subprocess rounds against master, Ruby 4.0.6, no JIT:

request delta
JSON POST, 10 array elements × 3 attributes +4.1%
GET with flat params (control) +0.2% (noise)

Test plan

  • Full RSpec suite passes locally.
  • RuboCop clean.
  • Mutation-checked: never recording indices fails 23 specs; dropping parent indices fails 2; validating an array scope that did not get an Array fails 1; ignoring the nearest array ancestor fails 1 (the new spec).
  • CI green.

🤖 Generated with Claude Code

For every element of an array scope, and for every validator on it,
`AttributesIterator#do_each` re-derived things that depend only on the
scope and the request: whether the scope iterates elements, whether its
params are an Array, which scope an element's index is recorded
against, and the request's `ParamScopeTracker`, a fiber-storage read.

They are now settled once per `#each` and handed down, and the
top-level call no longer allocates its empty `parent_indices`. An
element-iterating scope whose params are not an Array returns before
iterating, since it yielded and recorded nothing anyway. On a
ten-element array, one validator's iteration overhead went from 5.46 to
4.64 µs.

The branch where a lateral scope, such as a `with` group inside an
array, records each element's index against its nearest array ancestor
had no spec: removing it passed the suite while naming the wrong element
in errors. It now has one, which passes before and after this change.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@ericproulx
ericproulx force-pushed the perf/attributes-iterator-per-pass-state branch from 65851db to 840b2cf Compare September 10, 2026 19:53
@github-actions

github-actions Bot commented Sep 10, 2026

Copy link
Copy Markdown

Danger Report

No issues found.

View run

@ericproulx

Copy link
Copy Markdown
Contributor Author

Closing in favour of #2936, which combines #2922#2934 into one PR, re-benchmarked as a whole against master. The write-up here (behaviour matrix, mutation results) still describes this part of the change.

@ericproulx ericproulx closed this Sep 11, 2026
@ericproulx
ericproulx deleted the perf/attributes-iterator-per-pass-state branch September 15, 2026 21:49
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