Skip to content

perf(arrays): typed-cell element reads, authoritative braced miss, borrow-on-append - #1615

Merged
caomengxuan666 merged 3 commits into
masterfrom
perf/slice-arrays
Oct 25, 2026
Merged

caomengxuan666 merged 3 commits into
masterfrom
perf/slice-arrays

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

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_8000 11.0×). The fix threads a read-only &VariableStore view 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 existing array_cells_dirty trust gate; negative subscripts rebase off the cell shape. Write-side discipline is strengthened in step: set_variable now marks array cells dirty when the old binding was array-shaped, matching the existing set_array_element/set_assoc_element discipline.

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 requires index >= 0 (negative-subscript rebasing and bad-subscript diagnostics stay on the mirror leg) and dynamic_values.is_none().

3. Compound append borrows the mirror instead of cloning it (00a27bf0)
apply_shell_assignment_inner cloned 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 (baseline append@100000 19.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)

Case Before After
array_arith_read_8000 mirror leg 8000/8000, 878 MB hashed mirror leg 0 (8000/8000 via cell)
arith_assoc_read_4000 mirror leg 4000/4000 mirror leg 0
assoc miss ×2000 mirror scan 2000, 23.5 MB mirror scan 0
append fill ×20000 entry clone of whole mirror per append clone eliminated (borrow)

Timing 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

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_subscript 13.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.

… 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.
@caomengxuan666

Copy link
Copy Markdown
Member Author

Integration verification on master f6b5a3f5 + these 3 commits (integration tree, purified env, RUSTFLAGS="-C debuginfo=0" CARGO_BUILD_JOBS=1):

Gate Result
build --locked ✓ (11m 12s)
issue1363 / 1364 / 1523 / 1525 / 1555 / 1556 21/21, 12/12, 6/6, 8/8, 12/12, 13/13 — identical to cluster-side
lib full (--skip login_posix_env) 771 passed / 1 failed / 1 ignored; sole failure = ownership_unary_operators_check_existing_file (pre-existing root-env baseline item, same as pristine master). The debug build's typed-cell verifier compares cell↔mirror at every command boundary — zero inconsistency panics across the suite, i.e. the #616/#1510 invariant holds on the integrated tree
z2 PIPESTATUS matrix 40/40
246 differential raw 243 pass / 3 fail {gl08, ms10, pf203}; pf203 adjudicated a clock flake by isolated rerun (its only diff is a 1-second epoch boundary in a %T printf; 2/3 isolated trials byte-match, the third straddled the second tick) → effective failure set {gl08, ms10}, the stable set
rustfmt (8 changed files) all --check clean
CR count / instrumentation CR 0 on all 8 files; grep perf_count in src/ = 0; continuation.rs untouched

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 (array_arith_read_8000, arith_assoc_read_4000, array_braced_read_8000, assoc_get_miss, append_fill, arith_read_1000) produce byte-identical output+rc under the integrated binary and the GNU oracle; the 40-shape spotcheck matches the cluster's fixed-release record exactly (modulo one argv[0] path prefix), and against the GNU record differs only in the pre-existing $((a=7, a[0])) array-collapse line already called out in the PR body.

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).

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