You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[perf][P1] Associative-array write m[$k]=v is 16.7× slower (73µs/op vs 4.3µs, 100 keys) — same store/diff-sync family as #616 #618
Associative-array (declare -A) write m[$k]=v is 16.7× slower than GNU on-device (Android, rubash master 334afab vs bash 5.3.20, idle best-of-3, 200 000 writes to 100 keys):
op
rubash
niu product
GNU bash
× slower
for((i=0;i<200000;i++)); do m[k$((i%100))]=$i; done
14 559 ms
14 793 ms
853 ms
17.1× / 17.3×
~73 µs per write vs bash's ~4.3 µs, holding only 100 distinct keys (so it's not key-count growth — the per-write constant is high). This is the same store family as the indexed-array finding (#616): the element set goes through the dual-store + capture-replay diff-sync, so every assoc write re-diffs/replays the mutable-key table.
Suggested fix direction: precompute the key hash once; for loop-batches, fence the capture-replay per loop-level command (mirrors #616 + the scalar proposal in #609). Verify the alias-table bridge (sync_dynamic_assoc_vars) is not re-hashing the whole table per write.
Repro
declare -A m; for((i=0;i<200000;i++));do m[k$((i%100))]=$i;done# rubash ~14.6s | bash ~0.85s
Measured on 12da0b5 (release build) with ~/bench_niu/attrib_618.sh: min-of-3,
shell order rotated per round, N=200000. Local GNU bash is 5.3.20 while the
repository oracle is WSL bash 5.3.0 — caveat noted; the rules cited below come
from the vendored 5.3 C source, not from observed output.
Headline: assoc write is O(keys) per write
m[k$((i%100))]=$i at N=200000:
ratio vs GNU
assoc write, 100 keys (16.830s vs 0.921s)
18.3x
assoc write, 1 key
3.5x
assoc read, 100 keys
7.1x
indexed write, 100 slots
25.6x
compound append
2.3x
scalar write
2.2x
The mechanism, isolated by a key-count sweep
Writes fixed at 20000, only the key count varies — per-write cost tracks the
number of keys, which is the signature of a full re-render per write:
keys K
GNU
rubash
ratio
10
0.007s
0.008s
1.1x
100
0.007s
0.013s
1.9x
1000
0.011s
0.242s
22.0x
5000
0.025s
5.574s
223.0x
The indexed side shows the same shape far more sharply (writes fixed at
20000): 5.3x / 21.7x / 211.1x / 907.8x — at K=5000 that is 98.947s for
rubash against GNU's 0.109s.
Every m[k]=v re-renders the entire assoc mirror through assignment_helpers.rs:345 format_assoc_storage, so a write costs O(keys)
instead of O(1). GNU's assoc_insert is O(1) amortized per key
(third_party/bash/assoc.c).
Correctness fallout already split out
The same survey found that the assoc typed cell has no writer-side sync (17
mirror-writer sites, no assoc counterpart to sync_typed_indexed_storage),
which made set -u boundness disagree with the value it guards. That is a
separate concern — tracked by issue #638 and fixed on top of this base;
the arithmetic-subscript nounset gap is #639.
What the perf phase of this issue must do
Remove the O(keys)-per-write re-render at the write path (assoc side: 223x
at K=5000). The indexed side shares the mechanism and is far sharper
(908x at K=5000), which is #616's next phase.
Design note for later agents: a lazy-cell redesign was surveyed and rejected (20 unhookable env_vars.get borrows + 8 whole-table byte consumers). The fix appends one [k]=v segment in O(1) with budget-seeded compaction, and folds transient dupes to first-slot/last-value at all three storage parsers. Compaction also exposed and fixed an O(n^2) tokenizer (whole-rest Vec<char> collect per segment; true parse of 10001 segs 563ms -> 17.8ms linear).
Honest residuals in the PR body: K<=3 trails master ~+1.5us/write (structural memo-miss price, gone by K=5); ~9x vs GNU remainder is fixed pipeline overhead, follow-up.
Landed as #650 (squash-merged 333973c). Assoc write curve is flat: K=5000 45x vs master, GNU gap 445x -> 9.9x. Residuals and follow-ups are in the PR body and #641.
Activity
caomengxuan666 commentedon Oct 16, 2026
Attribution (before any perf fix)
Measured on
12da0b5(release build) with~/bench_niu/attrib_618.sh: min-of-3,shell order rotated per round, N=200000. Local GNU bash is 5.3.20 while the
repository oracle is WSL bash 5.3.0 — caveat noted; the rules cited below come
from the vendored 5.3 C source, not from observed output.
Headline: assoc write is O(keys) per write
m[k$((i%100))]=$iat N=200000:The mechanism, isolated by a key-count sweep
Writes fixed at 20000, only the key count varies — per-write cost tracks the
number of keys, which is the signature of a full re-render per write:
The indexed side shows the same shape far more sharply (writes fixed at
20000): 5.3x / 21.7x / 211.1x / 907.8x — at K=5000 that is 98.947s for
rubash against GNU's 0.109s.
Every
m[k]=vre-renders the entire assoc mirror throughassignment_helpers.rs:345 format_assoc_storage, so a write costs O(keys)instead of O(1). GNU's
assoc_insertis O(1) amortized per key(
third_party/bash/assoc.c).Correctness fallout already split out
The same survey found that the assoc typed cell has no writer-side sync (17
mirror-writer sites, no assoc counterpart to
sync_typed_indexed_storage),which made
set -uboundness disagree with the value it guards. That is aseparate concern — tracked by issue #638 and fixed on top of this base;
the arithmetic-subscript nounset gap is #639.
What the perf phase of this issue must do
Remove the O(keys)-per-write re-render at the write path (assoc side: 223x
at K=5000). The indexed side shares the mechanism and is far sharper
(908x at K=5000), which is #616's next phase.
caomengxuan666 commentedon Oct 16, 2026
Perf half is up as PR #650 (on top of #640).
Key-count sweep, N=20000
m[k$((i%K))]=$i, min-of-3 order-rotated, release:Design note for later agents: a lazy-cell redesign was surveyed and rejected (20 unhookable
env_vars.getborrows + 8 whole-table byte consumers). The fix appends one[k]=vsegment in O(1) with budget-seeded compaction, and folds transient dupes to first-slot/last-value at all three storage parsers. Compaction also exposed and fixed an O(n^2) tokenizer (whole-restVec<char>collect per segment; true parse of 10001 segs 563ms -> 17.8ms linear).Honest residuals in the PR body: K<=3 trails master ~+1.5us/write (structural memo-miss price, gone by K=5); ~9x vs GNU remainder is fixed pipeline overhead, follow-up.
perf(executor): append assoc element writes instead of re-rendering (#…
caomengxuan666 commentedon Oct 16, 2026
Landed as #650 (squash-merged 333973c). Assoc write curve is flat: K=5000 45x vs master, GNU gap 445x -> 9.9x. Residuals and follow-ups are in the PR body and #641.
perf(executor): append indexed overwrite writes instead of re-renderi…
perf(assoc): trim per-op fixed costs on the write fast lane (rubash#618)
Merge pull request #960 from unixwin/fix/assoc-write-expand
docs(readme): overhaul — Performance section with pinned numbers, hon…