Skip to content

[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

Closed

Description

@caomengxuan666

Result summary

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

Activity

  1. added
    P1-high
    performance
    on Oct 15, 2026
  2. caomengxuan666 commented on Oct 16, 2026

    @caomengxuan666
    MemberAuthor

    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))]=$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.

  3. caomengxuan666 commented on Oct 16, 2026

    @caomengxuan666
    MemberAuthor

    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:

    keys master/write branch/write master/GNU branch/GNU
    100 87.45us 47.85us 15.08x 8.25x
    1000 537.40us 48.90us 99.52x 9.06x
    5000 2361.35us 52.40us 445.54x 9.89x

    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.

  4. caomengxuan666 commented on Oct 16, 2026

    @caomengxuan666
    MemberAuthor

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

P1-highperformance

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions