Repository navigation
perf(arrays): typed-cell element reads, authoritative braced miss, borrow-on-append - #1615
Conversation
… mirror GNU expr.c:1183 expr_streval -> arrayfunc.c:1580 array_value_internal reads the element from the ARRAY object itself. The port's arithmetic evaluator only held the serialized mirror, so every $((a[i])) and $((m[k])) read re-hashed the whole render through the content-addressed parse cache and (for assoc) linearly scanned it: Theta(mirror bytes) per read. Measured by path counters at N=8000: 8000/8000 reads on the mirror leg, 878MB hashed (perf baseline: array_arith_read_8000 11.0x vs braced reads 3.2x — the one shape-level gap left in the table). Thread an optional read-only store view through the evaluation funnel, supplied only when env_vars is the Executor's live table or a plain clone of it taken at evaluation start (subscript overlays carrying pending writes pass None, as do parser-only contexts). Element reads consult the complete typed cell under the existing array_cells_dirty discipline (rubash#616/#1510); PIPESTATUS stays on its dynamic-context snapshot (rubash#1364 R3). Negative subscripts rebase from the cell shape, and arith_negative_subscript_is_bad answers from the shape instead of an uncached full parse. As the enabling discipline, set_variable now marks the cells dirty when a scalar bind replaces an array-shaped binding — the same treatment set_array_element and set_assoc_element already had — so a mid-evaluation overwrite cannot leave a later read trusting a stale cell. Zero behavior change: cell misses fall through to the mirror leg unchanged. Verified: lib 732/1/1 (sole failure the known root-env ownership test), issue1363 21/21, issue1364 12/12, issue1523 6/6, issue1525 8/8, issue1555 12/12, issue1556 13/13, z2 matrix 40/40 (same-name protocol), 246 differential stable set {gl08, ms10}.
A braced ${a[i]} / ${m[k]} read that missed the typed cell fell
through to a full mirror clone + content hash + linear scan, even when
the gate had already established the cell trustworthy. The cell is a
complete mirror of the render or absent (rubash#616 Phase 1a
invariant), so under the existing trust gate (!dirty || per-name
fresh, rubash#1510) a miss against an existing cell proves the element
unset — the mirror scan below could only agree.
Answer None directly for assoc cells, and for indexed cells when the
index is non-negative and the name is not a dynamic view. Negative
indices keep the mirror leg (it owns the rebase and the bad-subscript
diagnostic), as do scalar bases and mirror-only names. This is the
shellbench assoc_get_miss shape (37.3x): path counters showed 2000/2000
misses falling through with 23.5MB of mirror traffic before, 0 after.
Zero behavior change; same verification ledger as the arithmetic
cell-read commit.
apply_shell_assignment_inner cloned the target's whole mirror string unconditionally at the append (+=) branch, but the clone was only ever read by the render fallback — the in-place segment append, which serves 100% of fill-loop appends (20000/20000 by path counter), never touches it. With the mirror growing to Theta(n) bytes the clone was a Theta(mirror) memcpy per append (shellbench append@100000: 19.5x). Borrow the mirror text through the read-only arms and re-fetch it in the fallback arm after the in-place attempt: a refused attempt wrote nothing, so the re-fetch is byte-identical to the old clone; an extended-but-cell-refused attempt re-parses to the same folded entries, and the call-site tail re-renders and re-syncs identically either way. The integer-attribute arm materializes its small scalar text as before. Zero behavior change; same verification ledger as the arithmetic cell-read commit.
|
Integration verification on master
Probe byte-equality (counter substitute — the profiling instrumentation was stripped before commit by design, so counters cannot be re-run on this tree): the cluster probe cases ( Process disclosure: the load gate (load<7, MemAvailable>1.5G) never opened during this integration — the S1 gate waited its full ~120 min and timed out at load 12.5 with the machine saturated by parallel cluster builds; the build then ran and completed cleanly. No timing numbers of any kind were taken on the integration tree; the PR's timing caliber statement stands (ratios pending gap-v2). |
Performance-only slice: three shape-level fixes on the array/subscript hot paths, from path-counter profiling against the merged perf baseline (
PERF-BASELINE-2026-10-25.md+ gap-v1). No behavior changes; each fix removes a Θ(mirror-bytes) leg that the counters proved was being paid per operation.The three fixes
1. Arithmetic element reads answer from the typed cells, not the mirror (
addb0cca)The arithmetic evaluator only held the serialized mirror, so every
$((a[i]))/$((m[k]))read re-hashed the whole render through the content-addressed parse cache (and linearly scanned it for assoc). Counters at N=8000: 8000/8000 reads on the mirror leg, 878,280,000 bytes hashed (~110 KB/read) — this was the baseline table's worst single shape (array_arith_read_800011.0×). The fix threads a read-only&VariableStoreview through the evaluation funnel, supplied only when the env is the executor's live table (or a plain clone of it); subscript overlays carrying pending writes and parser-only contexts keep the old path verbatim. Element reads consult the complete typed cell under the existingarray_cells_dirtytrust gate; negative subscripts rebase off the cell shape. Write-side discipline is strengthened in step:set_variablenow marks array cells dirty when the old binding was array-shaped, matching the existingset_array_element/set_assoc_elementdiscipline.2. Braced element misses answer authoritatively from the cell (
afee9df1)A braced
${a[i]}/${m[k]}read that missed the typed cell fell through to a full mirror clone + hash + linear scan, even when the trust gate had already established the cell as complete (#616 Phase 1a invariant: the cell is a complete mirror of the render, or absent). Counters: 2000/2000 assoc misses fell through, 23.5 MB of mirror traffic. Under the existing gate (!dirty || per-name fresh, #1510), a miss against an existing cell now answers "unset" directly. The indexed leg additionally requiresindex >= 0(negative-subscript rebasing and bad-subscript diagnostics stay on the mirror leg) anddynamic_values.is_none().3. Compound append borrows the mirror instead of cloning it (
00a27bf0)apply_shell_assignment_innercloned the target's whole mirror string unconditionally at the+=branch, but the clone was only ever read by the render fallback; the in-place segment append that serves fill loops (20000/20000 by counter) never touched it — a Θ(mirror) memcpy per append, Θ(n²) overall (baselineappend@10000019.5×). The append branch now borrows the mirror text through the read-only arms and re-fetches it in the fallback, with both re-fetch shapes argued byte-equivalent to the old code in the commit message.Counter evidence (post-fix, same instrumented build method)
array_arith_read_8000arith_assoc_read_4000Timing caliber — read before quoting numbers
The strict sampling window (load < 2.0, no cargo/rustc running) never opened on the profiling day, so final ratios are pending the gap-v2 re-measurement; nothing here is a formal benchmark conclusion. Interleaved min-of-N timings taken under load 11.5–13.4 are indicative of shape only: arith_read_8000 4.0× faster, arith_read_32000 11.5× faster, with the scaling slope moving from superlinear (13.5× time for 4× N) to linear (4.7×) — the Θ shape changed; where the fixed/GNU ratio settles (baseline strict caliber was 11.0× at the 8000 tier) is for gap-v2 to say.
Invariants
readonly PIPESTATUSdoes not freeze pipeline-status updates; GNU leaves it empty #1364 PIPESTATUS shape machine untouched: the arithmetic leg explicitly excludes PIPESTATUS (its reads go through the dynamic-context snapshot); the braced authoritative-miss path requiresdynamic_values.is_none().src/lexer/continuation.rsuntouched. Instrumentation used for profiling was fully stripped before commit (grep perf_count= 0).Residual
The subscript-arithmetic evaluator (
eval_conditional_arith_value_categorized_with_writes_and_state) still clones the whole VarTable and diffs all writes per evaluation (expand.arith_in_subscript13.3×). Removing that needs an evaluator write-interception layer and is deferred to the 1.5.0 state-view line, where the single-store evaluation already lives; this slice's read-only cell view is registered there as a new read consumer.Separately, profiling surfaced a pre-existing correctness bug (present in the baseline, not fixed here, zero behavior change in this slice):
$((a=7, a[0]))collapses an array to([0]="7")where GNU keeps the array and sets element 0. To be filed and fixed on its own.