Skip to content

perf(1c): grow the compound append in place from the structured cell (#616) - #632

Merged
caomengxuan666 merged 1 commit into
masterfrom
perf/compound-append-in-place
Oct 15, 2026
Merged

caomengxuan666 merged 1 commit into
masterfrom
perf/compound-append-in-place

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

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 idiom
a+=($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_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 — but it
is still as_ref().clone() of the whole map (storage.rs:51). That is why 1b
could amortise the element write and this one stayed quadratic.

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. The ARRAY object 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 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 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 now 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 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 no
longer fed, so every iteration became an uncached O(n) miss — a full parse
replacing a memo-hit clone:

case bash master branch (step 1) master/branch
cmp2000 42 2453 3649 0.67×
cmp4000 51 9896 14277 0.69×

Removing 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

1` means the branch wins.

case bash ms master ms branch ms master/bash branch/bash master/branch
cmp2000 39 1985 68 50.9× 1.74× 29.2×
cmp4000 43 8175 95 190.1× 2.21× 86.1×
cmp_literal2000 34 1979 63 58.2× 1.85× 31.4×
elem2000_ctl (1b control) 33 61 55 1.85× 1.67× 1.11×

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×). cmp2000 is now 1.7× GNU instead of 50.9×; the remaining
gap 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 --lib 584/584.
  • tests/array_store_parity.rs (54 GNU-verified cases, +12 for 1c) green with
    the store on and with RUBASH_ARRAY_STORE=0.
  • cli_tests and executor_tests failure sets identical to master
    37d6caf, compared serial. Run in parallel this suite is not a usable gate:
    at --test-threads=4 it races on shared paths under target/ and reported 2
    "new" cli_tests failures and 4 new / 6 disappeared executor_tests rows —
    every one passes in isolation. Hence serial, and by failure set, never count.
  • 52-case whole-class differential matrix (assignment position ×
    literal/variable/substitution/arithmetic × spacing/quoting × append shape ×
    attributes): 0 branch != master, and no new bash divergence.
  • 2500-seed differential fuzz (branch vs master vs bash) over the write routes
    that can precede an append — unset, readonly, declare -i, mapfile -O,
    local, namerefs, export, eval, compgen: branch != master 0/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 on
master 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.

…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.
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