Repository navigation
perf(executor): append indexed overwrite writes instead of re-rendering (#616) - #652
Merged
Merged
Conversation
…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.
This was referenced Oct 16, 2026
This was referenced Oct 16, 2026
This was referenced Oct 28, 2026
This was referenced Nov 30, 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 indexed overwrite writes instead of re-rendering (#616)
What
a[i]=von an indexed array cost O(n) per write (mirror clone, BTreeMapdeep-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]=vsegment in O(segment) with the same budget-seededcompaction 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: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 (
BTreeMaplast-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:
the
IndexedArraycell, so every append extends it (extendverifiesthe size and falls back to full sync on disagreement — self-healing).
+=reads the old value from the cell (no parse at all).array_compact_budgetmap (renamedfrom
assoc_compact_budget) is seeded from the distinct entry count onevery canonical render; the string never exceeds twice canonical.
format_array_valuebuiltins(
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), sothe 1b fast path misfired on
index==max+1and duplicated the segment —a[2]=zprinted([2]="y" [5]="x" [2]="z")with count 3. Both declare syncsites now preserve keys (
replace_indexed_array_entries). The cell wasuntrustworthy for any cell-gated write; the battery proves it now.
Verification
is
unset "a[@]"leaving a stale count — filed as unset 'a[@]' leaves stale typed cell: ${#a[@]} disagrees with the mirror #651 (unset path, notthis PR). Baseline master is 48/50 (sparse-overwrite dupe + the same
unset case): this PR fixes the first, leaves the second.
array_store_parity13,issue371,issue638,compound-quote,
issue631,issue614, declare/nameref/export,fieldsplit/bracket). Two failures (
issue326tz,issue298positionals)fail identically on the unmodified baseline — Android environment issues.
333973c: target sets identical (248=248ok, 26=26 failed, NEW=0); test-level NEW=0, 4 DISAPPEARED all in the
known-nondeterministic
part_080coproc family (all pass in isolation onthe baseline tree).
6295367, only rubash[patch]switched): failure sets byte-identical (49=49).
Honest residuals
compactions always miss, master always hits; inside noise, already
negative by K=10).
at K=1) — separate subsystem.
(...)mirrors still count segments inarray_values(pre-existing, no writer produces them).
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).