Skip to content

perf(executor): append indexed overwrite writes instead of re-rendering (#616) - #652

Merged
caomengxuan666 merged 1 commit into
masterfrom
perf/issue616-indexed-write
Oct 16, 2026
Merged

caomengxuan666 merged 1 commit into
masterfrom
perf/issue616-indexed-write

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

perf(executor): append indexed overwrite writes instead of re-rendering (#616)

What

a[i]=v on an indexed array cost O(n) per write (mirror clone, BTreeMap
deep-clone out of the parse memo, full re-render), so a fill loop was O(n²):
at K=5000, 674x slower than GNU per write. Overwrites and sparse jumps now
append one [i]=v segment in O(segment) with the same budget-seeded
compaction discipline as #618; the tail-append fast path (1b/1c) is
unchanged and composes with it.

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

Per-write cost of a[i%K]=$i, N=20000:

keys master branch master/GNU branch/GNU
1 17.10µs 18.75µs 3.60x 3.95x
10 24.80µs 20.10µs 5.45x 4.42x
100 101.10µs 20.85µs 21.51x 4.44x
1000 889.55µs 20.45µs 172.73x 3.97x
5000 3638.45µs 20.60µs 673.79x 3.81x

5000x key growth moves branch per-write 1.1x (noise floor); master blows up
213x. K=5000: 177x faster than master, GNU gap 674x → 3.8x.

Design

Unlike assoc, indexed readers already fold duplicates (BTreeMap
last-wins for value, distinct for keys/count, sorted for order — surveyed
~125 A-class sites, all transparent), so no parse/render changes were
needed. Three indexed-specific additions:

  1. Cell extend per append. Assoc has no typed cell; indexed length reads
    the IndexedArray cell, so every append extends it (extend verifies
    the size and falls back to full sync on disagreement — self-healing).
    += reads the old value from the cell (no parse at all).
  2. Shared budget + compaction. The array_compact_budget map (renamed
    from assoc_compact_budget) is seeded from the distinct entry count on
    every canonical render; the string never exceeds twice canonical.
  3. Fold before print. The two verbatim format_array_value builtins
    (declare -p, export -p/readonly -p) would print transient dupes;
    they now re-render from the folded map only when the segment count
    exceeds the distinct count (identity otherwise).

Precondition fix (load-bearing, same PR): declare -a a=([5]=x [2]=y)
seeded a dense {0,1} cell (keys dropped by a values-only replace), so
the 1b fast path misfired on index==max+1 and duplicated the segment —
a[2]=z printed ([2]="y" [5]="x" [2]="z") with count 3. Both declare sync
sites now preserve keys (replace_indexed_array_entries). The cell was
untrustworthy for any cell-gated write; the battery proves it now.

Verification

  • 50-case indexed-write GNU differential battery (new): 49/50. The one diff
    is unset "a[@]" leaving a stale count — filed as unset 'a[@]' leaves stale typed cell: ${#a[@]} disagrees with the mirror #651 (unset path, not
    this PR). Baseline master is 48/50 (sparse-overwrite dupe + the same
    unset case): this PR fixes the first, leaves the second.
  • Focused suites green (array_store_parity 13, issue371, issue638,
    compound-quote, issue631, issue614, declare/nameref/export,
    fieldsplit/bracket). Two failures (issue326 tz, issue298 positionals)
    fail identically on the unmodified baseline — Android environment issues.
  • Full serial failure-set gate vs 333973c: target sets identical (248=248
    ok, 26=26 failed, NEW=0); test-level NEW=0, 4 DISAPPEARED all in the
    known-nondeterministic part_080 coproc family (all pass in isolation on
    the baseline tree).
  • niubash product A/B (niubash pinned 6295367, only rubash [patch]
    switched): failure sets byte-identical (49=49).

Honest residuals

GNU owners: array.c:516 array_insert, arrayfunc.c:815 assign_array_element, array.h:30-41 (num_elements/max_index),
arrayfunc.c:1180-1200 unbind_array_element (context for #651).

…ng (#616)

a[i]=v on an indexed array re-rendered every element on every write, so a
fill loop was O(n^2): 674x GNU per write at K=5000. Overwrites and sparse
jumps now append one [i]=v segment in O(segment) with the same
budget-seeded compaction discipline as #618 (amortized O(1), string never
beyond twice canonical); the tail-append fast path (1b/1c) is unchanged and
composes. K=5000 is 177x faster than master, the GNU gap closes 674x ->
3.8x, and the key-count curve is flat (5000x keys move per-write 1.1x).

Indexed readers fold duplicates by construction (BTreeMap last-wins), so
no parse/render changes were needed — except the two verbatim
format_array_value printers, which re-render from the folded map only when
segments outnumber distinct keys. Every append also extends the typed cell
(indexed length reads it), with size-verified fallback to full sync.

Load-bearing precondition, same PR: `declare -a a=([5]=x [2]=y)` seeded a
dense {0,1} cell (values-only replace dropped sparse keys), so the 1b fast
path misfired on index==max+1 and duplicated the segment. Both declare
sync sites now preserve keys via replace_indexed_array_entries.

Verification: new 50-case GNU differential 49/50 (only the unset-flush
stale count, filed as #651; master baseline is 48/50); full serial
failure-set gate vs 333973c identical (NEW=0 test/targets; 4 disappeared
are part_080 coproc flake passing in isolation on baseline); niubash A/B
failure sets byte-identical (49=49). K=1 trails master ~+1.6us/write
(structural memo-miss price, negative by K=10); ~3.8x vs GNU is fixed
pipeline overhead, a separate concern.
@caomengxuan666
caomengxuan666 merged commit bc258ad into master Oct 16, 2026
8 checks passed
@caomengxuan666
caomengxuan666 deleted the perf/issue616-indexed-write branch October 16, 2026 05:10
caomengxuan666 added a commit that referenced this pull request Oct 16, 2026
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