Commit 66aadad
Return frozen EMPTY array from StackableValues on miss (#2690)
`StackableValues#[]` is the accessor behind `namespace_stackable[key]` —
called multiple times per request (five filter phases, plus ad-hoc
settings lookups). When neither `inherited_values` nor `new_values`
have an entry for the key, the old `return new_value || []` allocated
a fresh empty array on every miss.
Introduce a frozen `EMPTY` singleton and return it in the miss branch.
Saves two short-lived empty-array allocations per request in the
realistic shape (some filters set, some not) and up to five in the
worst case.
One caller (`Endpoint#initialize`) stored `namespace_stackable[:validations]`
directly into `route[:saved_validations]` by reference, and later
`.concat`'d parent validations into it via `inherit_settings`. With the
stackable now able to return a frozen array, that mutation would
`FrozenError`. Dup at capture so the route keeps its own mutable list —
good hygiene regardless of this change.
Realistic (3 keys populated, 2 absent):
current: 2.01 M i/s, 80B / 2 objects allocated
variant: 2.19 M i/s, 0B / 0 objects allocated
Worst case (all 5 absent):
current: 2.18 M i/s, 200B / 5 objects
variant: 2.49 M i/s, 0B / 0 objects (1.14x faster)
No behavior change; all 2,236 specs pass.
Co-authored-by: Claude Opus 4.7 (1M context) <noreply@anthropic.com>1 parent 952a74c commit 66aadad
3 files changed
Lines changed: 6 additions & 3 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
| 14 | + | |
14 | 15 | | |
15 | 16 | | |
16 | 17 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
46 | 46 | | |
47 | 47 | | |
48 | 48 | | |
49 | | - | |
| 49 | + | |
50 | 50 | | |
51 | 51 | | |
52 | 52 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
6 | | - | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
7 | 9 | | |
8 | 10 | | |
9 | 11 | | |
10 | 12 | | |
11 | | - | |
| 13 | + | |
12 | 14 | | |
13 | 15 | | |
14 | 16 | | |
| |||
0 commit comments