Repository navigation
perf(1b): amortise the indexed element write with the structured cell (#616) - #630
Merged
Merged
Conversation
…#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.
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 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-elementarray stops being O(n²).
Durable write-up:
docs/perf-issue-616-array-store.md(the "Phase 1b" section).Attribution
array_assignment_exec.rsdid four O(n) operations pera[i]=v:env_vars.get(name).cloned()— deep-copies the whole mirrorString;indexed_array_entries(¤t)— deep-copies the parsed map (the 128-entrymemo makes it a hit and still O(n));
format_indexed_array_storage(entries)— re-renders the whole array;env_vars.insert(...)— swaps in the newString.So building an n-element array was O(n²). GNU touches one node —
gnu:arrayfunc.c:815 assign_array_elementreachesgnu:array.c:516 array_insert, which splices oneARRAY_ELEMENTand maintainsnum_elements/max_index(array.c:533-629, read atarray.h:121) — and neverre-serialises anything, because the
ARRAYobject is the array.Fix
When the structured cell is a complete
IndexedArray, the mirror is acanonical
STORAGE_WORD_PREFIX(...)render, and the write is an append(
index == max_index + 1,0for an empty cell):BTreeMap::get, O(log n));extend_indexed_element, O(log n));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:
fallback re-parses the already-extended mirror and re-inserts the same element
— idempotent, landing on the correct value, never a stale mirror;
len == PREFIX.len() + 2), neverends_with('('), because an element value may itself end in(. Getting thiswrong rendered the first write into an empty array as
( [0]=...); theregression 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.
08_arlenscalar control (200k)build10kcompound +${#a[@]}len@sparse+ 200k${#a[@]}15_arrbuildn=200015b_arrbuildn=1000015c_arrbuild_compoundn=200016_arritern=200019_arrcopyn=2000comboundedit(write+len)No row regresses, and the fix's shape shows in the scaling:
mastergoes1760 → 46071 ms for 5x the elements (26.2x, quadratic); the branch goes
51 → 157 ms (3.1x, ~linear).
declare -pbyte-identity is the correctness bar for the in-place extension:the mirror must equal a full re-render, and
declare -pprints the mirror'sparse.
tests/array_store_parity.rspins the empty-array first write, valuesending in
(/), appends across a sparse gap, and compound-then-element append,each also re-run with
RUBASH_ARRAY_STORE=0.Gates
tests/array_store_parity.rs(42 GNU-verified cases) green with the store onand
RUBASH_ARRAY_STORE=0;cli_testsred-set name-identical to master;executor_testsfailure-setidentical to master (1229, set-compared);
Scope
The compound append
a+=(...)still re-renders the whole mirror(
append_array_value,arrays.rs:779), soa+=($i)in a loop is quadratic inthe render — the structured cell is already refreshed tail-only by Phase 1a.
A mid-array overwrite (
a[3]=xin a loop) is likewise still O(n) per write. Bothwant 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.