Repository navigation
perf(executor): append assoc element writes instead of re-rendering (#618) - #650
Merged
Merged
Conversation
…618) m[k]=v on an associative array paid O(keys) three times per write (string clone, Vec deep-clone out of the parse memo, full re-render), so filling an array was O(n^2): 433x GNU per write at K=5000. Plain element writes now append one [k]=v segment in O(1); a budget-seeded canonical render compacts duplicates back to one binding per key every O(entries) writes (amortized O(1), string never beyond twice canonical). K=5000 is 45x faster than master, the GNU gap closes 445x -> 9.9x, and the key-count curve is flat (5000x keys move per-write 1.14x). Appending creates transient duplicated segments, so all three storage parsers fold to first-slot / last-value at the source via one shared dedupe_assoc_entries (identity on dupe-free input) — the contract bash_assoc_order documents, and GNU assoc.c assoc_insert's one-binding shape. bash_assoc_order now supplies the last value as documented (it returned the first; unreachable until a probe injected dupes), and ${#m[@]} counts distinct keys. A lazy-cell design was surveyed and rejected: ~20 direct env_vars.get borrows admit no render hook. The compaction parse exposed an O(n^2) tokenizer: StorageWordIter collected the whole rest of input into a Vec<char> per [-leading word. New lexer::skip::arraysub_span_len_str scans the &str directly (same GNU owner, parse.y:5635-5651 P_ARRAYSUB), used by the two storage-input splitters; five command-bounded callers untouched. True parse of 10001 segments: 563ms -> 17.8ms, linear. Verification: 50/50 GNU differential before and after; full serial failure-set gate vs ce471a2 identical (26=26 targets; 3 NEW / 1 DISAPPEARED all in the known-nondeterministic part_080 coproc family, passing in isolation on both trees); niubash A/B failure sets identical apart from a load-flaky 50x timing-ratio test that fails in isolation on both trees. K<=3 trails master ~+1.5us/write (structural memo-miss price, inside host noise, gone by K=5); the ~9x vs GNU remainder is fixed per-statement pipeline overhead, a separate concern.
This was referenced Oct 16, 2026
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.
perf(executor): append assoc element writes instead of re-rendering (#618)
What
m[k]=von an associative array cost O(keys) per write (three of them:string clone, Vec deep-clone out of the parse memo, full re-render), so
filling an array was O(n²): at K=5000, 433x slower than GNU per write.
Plain element writes now append one
[k]=vsegment in O(1); abudget-seeded canonical render compacts duplicates back to one binding per
key every O(entries) writes (amortized O(1), string never beyond twice
canonical).
Measured attribution (order-rotated min-of-N, same tree, release)
Per-write cost of
m[k$((i%K))]=$i, N=20000:5000x key growth moves branch per-write 1.14x (noise); master blows up
52.8x. K=5000: 45x faster than master, GNU gap 445x → 9.9x.
Write-path decomposition at K=5000 (why render-only fixes fail): re-render
73–75%, parse-memo Vec deep-clone 24–25%, string clone 0.05%. The memo
removed the scan but not the clone.
Design (why append, not a lazy cell)
A typed-cell-authoritative + lazy-mirror design was surveyed and rejected:
~125 read sites sit behind one good hook, but ~20 direct
env_vars.getborrows through
VarTable'sDeref<HashMap>admit no lazy hook, plus 8whole-table byte consumers. Appending keeps the mirror never stale, so
zero read sites change.
+=, non-(...)storage, and append-failures fallthrough to the unchanged canonical path.
Appending creates transient duplicated segments; three readers now implement
the contract
bash_assoc_orderalways documented (first slot, last value;GNU
assoc.c assoc_insertholds one binding per key):dedupe_assoc_entries(identity on dupe-free input),bash_assoc_ordersupplies the last value (its comment promised this; thecode returned the first — unreachable until now, proven by injection),
${#m[@]}counts distinct keys.The tokenizer was O(n²) — fixed at the root to unblock this
Compaction parses a novel string (memo always misses), exposing that
StorageWordItercollected the entire rest of input into aVec<char>per
[-leading word: 10001 segments took 563ms (~O(n^1.6)). Newlexer::skip::arraysub_span_len_strscans the&strdirectly (same GNUowner, parse.y:5635-5651 P_ARRAYSUB), used by the two storage-input
splitters; five command-bounded callers untouched. True parse now linear:
10001 segments 17.8ms (31.7x).
Verification
after (master baseline is 50/50, so the battery is a clean guard).
array_store_parity,issue371,issue638,compound-quote, declare/nameref/export).
ce471a2: target sets identical (26=26,NEW=0); test-level 3 NEW / 1 DISAPPEARED all in the known-nondeterministic
command_chaining::part_080coproc family, all four pass in isolation onboth trees.
6295367, only rubash[patch]switched): failure sets byte-identical, NEW=0.
Honest residuals
master's canonical always memo-HITs while a compaction parse always
MISSES (append-built strings are novel by construction). Structural price
of the design, linear and 2x-bounded.
K=1, shared with scalar writes) — different subsystem, follow-up.
Vec<char>collects remain at 4 command-bounded sites plusprintf/test splitters — pre-existing, follow-up, not introduced here.
GNU owners:
assoc.c assoc_insert,arrayfunc.c:815 assign_array_element,parse.y:5635-5651,arrayfunc.c:610,subst.c:7787-7790(boundness rulecontext, #638).