Repository navigation
perf(1c): grow the compound append in place from the structured cell (#616) - #632
Merged
Merged
Conversation
…616) Phase 1c of #616, on top of merged Phase 1b (37d6caf): the compound append `a+=(...)` no longer re-parses and re-renders the whole array, so the loop idiom `a+=($i)` stops being O(n^2). Attribution: append_array_value opened with `indexed_array_entries(current)` (the whole mirror parsed into a BTreeMap) and the caller then re-rendered the mirror (format_indexed_array_storage) and re-synced the structured cell -- three O(n) passes per element appended. The parse is memoised (arrays/storage.rs:64, content-addressed on the exact storage string, and every render feeds it back), so master's is a *hit*; it is still `as_ref().clone()` of the whole map (storage.rs:51). GNU edits the ARRAY object in place: assign_compound_array_list (arrayfunc.c:700) inserts each element at `last_ind = array_max_index(a) + 1` (arrayfunc.c:743) through array_insert (array.c:516) and never re-serialises it. Fix: seed the decode from the structured cell, which the 1a invariant says is a complete mirror of the render or absent. `Variables::indexed_array_cell` exposes it; `IndexedDecodeBase` borrows it and keeps this call's writes in a small `mutations` map, answering max/get/is_empty in O(log n) without ever building the element map. `append_array_value` now returns `IndexedAppendOutcome`, whose `entries` is None for a pure append -- the write is fully described by `appended`, the elements strictly above `old_max`. `array_assignment_index` takes the max index instead of the map (it only ever read `keys().next_back()`). On that route the mirror grows by exactly the segments the renderer would have appended (`append_indexed_mirror_segments` -- 1b's per-element helper generalised to a segment list, now the single owner of both) and the cell gains the keys (extend_indexed_element). Every other shape -- an overwrite at or below `old_max`, an integer array (the post-pass rewrites every element), no cell, a non-canonical mirror, a readonly target -- falls back to `render_indexed_outcome`, which rebuilds the map from the mirror and folds `appended` back in: the unchanged real path, a whitelist whose fallback is the real thing. The mirror is still extended before the cell, so a refused cell write makes the fallback re-render and re-sync from the already-correct mirror; and "is the body empty?" is still judged by length, never `ends_with('(')`. An intermediate cut that only stopped rendering measured 0.67x -- SLOWER. With the render gone the parse memo is no longer fed, so every iteration became an uncached O(n) miss, replacing a memo-hit clone with a full parse. The win requires removing the O(n) itself, not the render; the measurement is why the design changed. Measured (order-rotated best-of-9, release, same tree, local bash 5.3.20): cmp2000 1985 -> 68 ms 29.2x cmp4000 8175 -> 95 ms 86.1x cmp_literal2000 1979 -> 63 ms 31.4x elem2000_ctl (1b control) 61 -> 55 ms 1.11x No row regresses, and the 1b control is unchanged, so the win is the compound path. Master scales quadratically (1985 -> 8175 ms for 2x n) while the branch is ~linear (68 -> 95 ms); cmp2000 is now 1.7x GNU instead of 50.9x. The master absolute drifts with host load, so the ratio is the claim, not the millisecond. Gates: lib 584/584; array_store_parity (54 GNU-verified cases) green with the store on and off; cli_tests red-set and executor_tests failure-set identical to master 37d6caf (serial runs -- at --test-threads=4 the suite races on shared target/ paths and reports phantom rows that all pass in isolation); a 52-case whole-class differential matrix (assignment position x literal/variable/ substitution/arithmetic x spacing/quoting x append shape x attributes) shows branch == master with no new bash divergence; 2500-seed differential fuzz branch == master 0/2500. Durable write-up: docs/perf-issue-616-array-store.md.
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.
Phase 1c of #616, on top of merged Phase 1b (
37d6caf).a+=(...)no longer re-parses and re-renders the whole array, so the loop idioma+=($i)stops being O(n²). On this host it is 29–86× faster than master,and the remaining gap to GNU is 1.7–2.2×.
Attribution
append_array_valueopened withindexed_array_entries(current)— the wholemirror parsed into a
BTreeMap— and the caller then re-rendered the mirror(
format_indexed_array_storage) and re-synced the structured cell: three O(n)passes per element appended.
The parse is memoised (
arrays/storage.rs:64, content-addressed on the exactstorage string, and every render feeds it back), so master's is a hit — but it
is still
as_ref().clone()of the whole map (storage.rs:51). That is why 1bcould amortise the element write and this one stayed quadratic.
GNU edits the
ARRAYobject in place:assign_compound_array_list(
arrayfunc.c:700) inserts each element atlast_ind = array_max_index(a) + 1(
arrayfunc.c:743) througharray_insert(array.c:516) and neverre-serialises it. The
ARRAYobject is rubash's structured cell.Fix
Seed the decode from the structured cell, whose 1a invariant is "a complete
mirror of the render, or absent" — the same invariant 1b already relies on.
Variables::indexed_array_cell(src/shell/variables.rs) exposes the cell.IndexedDecodeBase(src/executor/arrays.rs) borrows it and keeps thiscall's writes in a small
mutationsmap, answeringmax/get/is_emptyin O(log n) without ever building the element map.
append_array_valuereturnsIndexedAppendOutcome, whoseentriesisNonefor a pure append: the write is fully described by
appended, the elementsstrictly above
old_max.array_assignment_indexnow takes the max indexinstead of the map (it only ever read
keys().next_back()).appended (
append_indexed_mirror_segments— 1b's per-element helpergeneralised to a segment list, now the single owner of both) and the cell
gains the keys (
extend_indexed_element).Every other shape — an overwrite at or below
old_max, an integer array (thepost-pass rewrites every element), no cell, a non-canonical mirror, a readonly
target — falls back to
render_indexed_outcome, which rebuilds the map from themirror and folds
appendedback in. The fallback is the unchanged real path— a whitelist whose fallback is the real thing, not a symptom blacklist.
Inherited ordering still holds: the mirror is extended before the cell, so a
refused cell write makes the fallback re-render and re-sync from the
already-correct mirror; and "is the body empty?" is judged by length, never
ends_with('(').An intermediate cut that measured slower (kept for the record)
A first attempt removed the render and the trailing sync but still seeded from
indexed_array_entries(current). With the render gone the parse memo is nolonger fed, so every iteration became an uncached O(n) miss — a full parse
replacing a memo-hit clone:
cmp2000cmp4000Removing work is not the same as removing the right work. The measurement is
why the design changed instead of being tuned by eye.
Measured
Order-rotated best-of-9, release, same tree, local bash 5.3.20. `master/branch
cmp2000cmp4000cmp_literal2000elem2000_ctl(1b control)No row regresses and the 1b control is unchanged, so the win is entirely the
compound path. The shape is the point: master is quadratic
(1985 → 8175 ms for 2× the elements, 4.12×) while the branch is ~linear
(68 → 95 ms, 1.40×).
cmp2000is now 1.7× GNU instead of 50.9×; the remaininggap is the one render per call that 1c still performs.
The master absolute drifts with host load, so the ratio — measured in the same
tree with per-round shell-order rotation — is the claim, not the millisecond.
Gates
cargo test --lib584/584.tests/array_store_parity.rs(54 GNU-verified cases, +12 for 1c) green withthe store on and with
RUBASH_ARRAY_STORE=0.cli_testsandexecutor_testsfailure sets identical to master37d6caf, compared serial. Run in parallel this suite is not a usable gate:at
--test-threads=4it races on shared paths undertarget/and reported 2"new"
cli_testsfailures and 4 new / 6 disappearedexecutor_testsrows —every one passes in isolation. Hence serial, and by failure set, never count.
literal/variable/substitution/arithmetic × spacing/quoting × append shape ×
attributes): 0
branch != master, and no new bash divergence.that can precede an append —
unset,readonly,declare -i,mapfile -O,local, namerefs,export,eval,compgen:branch != master0/2500.Found while building the 1c matrix, filed separately
An out-of-range negative subscript in a compound assignment (
a=(); a+=([-1]=x))is silently dropped by rubash where GNU reports
bad array subscript(
arrayfunc.c:801-809), abandons the assignment and exits 1 — pre-existing onmaster and unrelated to the store. Filed as #631 so this performance line stays
one concern.
Durable write-up:
docs/perf-issue-616-array-store.md.