Skip to content

perf(executor): append assoc element writes instead of re-rendering (#618) - #650

Merged
caomengxuan666 merged 1 commit into
masterfrom
perf/issue618-assoc-write
Oct 16, 2026
Merged

caomengxuan666 merged 1 commit into
masterfrom
perf/issue618-assoc-write

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

perf(executor): append assoc element writes instead of re-rendering (#618)

What

m[k]=v on 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]=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).

Measured attribution (order-rotated min-of-N, same tree, release)

Per-write cost of m[k$((i%K))]=$i, N=20000:

keys master branch master/GNU branch/GNU
1 44.75µs 45.90µs 8.77x 9.00x
100 87.45µs 47.85µs 15.08x 8.25x
1000 537.40µs 48.90µs 99.52x 9.06x
5000 2361.35µs 52.40µs 445.54x 9.89x

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.get
borrows through VarTable's Deref<HashMap> admit no lazy hook, plus 8
whole-table byte consumers. Appending keeps the mirror never stale, so
zero read sites change. +=, non-(...) storage, and append-failures fall
through to the unchanged canonical path.

Appending creates transient duplicated segments; three readers now implement
the contract bash_assoc_order always documented (first slot, last value;
GNU assoc.c assoc_insert holds one binding per key):

  • all three storage parsers fold at the source via one shared
    dedupe_assoc_entries (identity on dupe-free input),
  • bash_assoc_order supplies the last value (its comment promised this; the
    code 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
StorageWordIter collected the entire rest of input into a Vec<char>
per [-leading word: 10001 segments took 563ms (~O(n^1.6)). 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 now linear:
10001 segments 17.8ms (31.7x).

Verification

  • 50-case assoc-write GNU differential battery: 50/50 identical before and
    after (master baseline is 50/50, so the battery is a clean guard).
  • Focused suites green (array_store_parity, issue371, issue638,
    compound-quote, declare/nameref/export).
  • Full serial failure-set gate vs ce471a2: target sets identical (26=26,
    NEW=0); test-level 3 NEW / 1 DISAPPEARED all in the known-nondeterministic
    command_chaining::part_080 coproc family, all four pass in isolation on
    both trees.
  • niubash product A/B (niubash pinned 6295367, only rubash [patch]
    switched): failure sets byte-identical, NEW=0.

Honest residuals

  • K≤3 trails master ~+1.5µs/write (~3%, inside host noise; gone by K=5):
    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.
  • Remaining ~9x vs GNU is fixed per-statement pipeline overhead (present at
    K=1, shared with scalar writes) — different subsystem, follow-up.
  • Cold-path Vec<char> collects remain at 4 command-bounded sites plus
    printf/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 rule
context, #638).

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