Skip to content

perf(1b): amortise the indexed element write with the structured cell (#616) - #630

Merged
caomengxuan666 merged 1 commit into
masterfrom
perf/array-store-1b
Oct 15, 2026
Merged

caomengxuan666 merged 1 commit into
masterfrom
perf/array-store-1b

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Phase 1b of the #616 array-subsystem plan, on top of the merged Phase 1a (#627,
now 14209b9): amortise the indexed element write, so building an n-element
array stops being O(n²).

Durable write-up: docs/perf-issue-616-array-store.md (the "Phase 1b" section).

Attribution

array_assignment_exec.rs did four O(n) operations per a[i]=v:

  1. env_vars.get(name).cloned() — deep-copies the whole mirror String;
  2. indexed_array_entries(&current) — deep-copies the parsed map (the 128-entry
    memo makes it a hit and still O(n));
  3. format_indexed_array_storage(entries) — re-renders the whole array;
  4. env_vars.insert(...) — swaps in the new String.

So building an n-element array was O(n²). GNU touches one node —
gnu:arrayfunc.c:815 assign_array_element reaches gnu:array.c:516 array_insert, which splices one ARRAY_ELEMENT and maintains
num_elements/max_index (array.c:533-629, read at array.h:121) — and never
re-serialises anything, because the ARRAY object is the array.

Fix

When the structured cell is a complete IndexedArray, the mirror is a
canonical STORAGE_WORD_PREFIX(...) render, and the write is an append
(index == max_index + 1, 0 for an empty cell):

  • read the old element from the cell (BTreeMap::get, O(log n));
  • add one key (extend_indexed_element, O(log n));
  • grow the mirror in place by exactly the segment the renderer would have
    appended (append_indexed_mirror_element).

Every other shape — overwrite, sparse insert, readonly, absent cell,
non-canonical mirror — falls through to the unchanged full path. This is a
whitelist whose fallback is the real path, not a symptom blacklist.

Two ordering details that matter:

  • the mirror is extended before the cell, so if the cell write is refused the
    fallback re-parses the already-extended mirror and re-inserts the same element
    — idempotent, landing on the correct value, never a stale mirror;
  • "is the body empty?" is judged by length (len == PREFIX.len() + 2), never
    ends_with('('), because an element value may itself end in (. Getting this
    wrong rendered the first write into an empty array as ( [0]=...); the
    regression case fails if the wrong test is restored (verified by injecting it).

Results

Order-rotated best-of-5, release, same tree and same minute. The harness rotates
the shell order per round on purpose: this host throttles, and the same
binary measures ~1.14x slower merely for running later in a round, which an
always-last branch would report as a phantom regression.

Workload bash master branch master/branch
08_arlen scalar control (200k) 540 1842 1751 1.1x
build10k compound + ${#a[@]} 30 64 52 1.2x
len@sparse + 200k ${#a[@]} 582 1875 1610 1.2x
15_arrbuild n=2000 31 1760 51 34.5x
15b_arrbuild n=10000 56 46071 157 293.4x
15c_arrbuild_compound n=2000 29 1898 1915 1.0x
16_arriter n=2000 36 1930 64 30.2x
19_arrcopy n=2000 5554 21843 22086 1.0x
comboundedit (write+len) 42 23199 9890 2.3x

No row regresses, and the fix's shape shows in the scaling: master goes
1760 → 46071 ms for 5x the elements (26.2x, quadratic); the branch goes
51 → 157 ms (3.1x, ~linear).

declare -p byte-identity is the correctness bar for the in-place extension:
the mirror must equal a full re-render, and declare -p prints the mirror's
parse. tests/array_store_parity.rs pins the empty-array first write, values
ending in (/), appends across a sparse gap, and compound-then-element append,
each also re-run with RUBASH_ARRAY_STORE=0.

Gates

  • lib 581/581;
  • tests/array_store_parity.rs (42 GNU-verified cases) green with the store on
    and RUBASH_ARRAY_STORE=0;
  • cli_tests red-set name-identical to master; executor_tests failure-set
    identical to master (1229, set-compared);
  • 2500-seed differential fuzz: branch==master 0/2500; bash-diff 677==677.

Scope

The compound append a+=(...) still re-renders the whole mirror
(append_array_value, arrays.rs:779), so a+=($i) in a loop is quadratic in
the render
— the structured cell is already refreshed tail-only by Phase 1a.
A mid-array overwrite (a[3]=x in a loop) is likewise still O(n) per write. Both
want a lazily materialised mirror (cell is truth, render deferred), which needs a
materialisation point at every whole-table consumer; that is Phase 1c, a design
of its own.

Oracle caveat: reference is GNU Bash 5.3.20 (this host), not the repo's
designated WSL 5.3.0. These Termux/Android numbers are not Windows/Linux x86_64
validation.

Refs #616.

…#616)

Phase 1a made `${#a[@]}` O(1) but left the element write quadratic, so an
n-element build stayed O(n^2): `15b_arrbuild` n=10000 was 46071 ms against
GNU's 56 ms.

Attribution: `array_assignment_exec.rs` did four O(n) operations per `a[i]=v` —
`env_vars.get(name).cloned()` (deep-copies the whole mirror String),
`indexed_array_entries` (deep-copies the parsed map; the memo makes it a *hit*
and still O(n)), `format_indexed_array_storage` (re-renders the whole array),
`env_vars.insert` (swaps the String). GNU touches one node:
arrayfunc.c:815 assign_array_element reaches array.c:516 array_insert, which
splices one ARRAY_ELEMENT and maintains num_elements/max_index
(array.c:533-629, read at array.h:121), and never re-serialises anything --
the ARRAY object *is* the array.

Fix: when the structured cell is a complete `IndexedArray`, the mirror is a
canonical `STORAGE_WORD_PREFIX(...)` render, and the write is an append
(index == max_index + 1, 0 for an empty cell), read the old element from the
cell (`BTreeMap::get`, O(log n)), add one key
(`extend_indexed_element`, O(log n)), and grow the mirror in place by exactly
the segment the renderer would have appended
(`append_indexed_mirror_element`). Every other shape -- overwrite, sparse
insert, readonly, absent cell, non-canonical mirror -- falls through to the
unchanged full path, so this is a whitelist whose fallback is the real path.

Two ordering details that matter: the mirror is extended BEFORE the cell, so a
refused cell write makes the fallback re-parse the already-extended mirror and
re-insert the same element -- idempotent, landing on the correct value rather
than a stale one. And "is the body empty?" is judged by length
(`len == PREFIX.len() + 2`), never by `ends_with('(')`, because a value may
itself end in `(`; getting that wrong rendered the first write into an empty
array as `( [0]=...)`. The regression case fails if the wrong test is restored
(verified by injecting it and watching the suite go red).

Measured (order-rotated best-of-5, release, same tree, local bash 5.3.20):

  15_arrbuild   n=2000   1760 ->    51 ms   34.5x
  15b_arrbuild  n=10000  46071 ->   157 ms   293.4x
  16_arriter    n=2000   1930 ->    64 ms   30.2x
  comboundedit           23199 ->  9890 ms   2.3x
  15c_arrbuild_compound  1898 ->  1915 ms   1.0x
  19_arrcopy             21843 -> 22086 ms  1.0x
  08 scalar control 1.1x, build10k 1.2x, len@sparse 1.2x

Scaling: master 1760 -> 46071 ms for 5x the elements (quadratic); the branch
51 -> 157 ms (linear).

Gates: lib 581/581; `array_store_parity` green with the store on and off;
`cli_tests` red-set and `executor_tests` failure-set identical to master
(6daf8e1); 2500-seed differential fuzz branch==master.
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