Skip to content

perf(assoc): skip discarded whole-word pass on side-effect-free element writes - #963

Merged
caomengxuan666 merged 1 commit into
masterfrom
fix/assoc-double-expand
Oct 21, 2026
Merged

caomengxuan666 merged 1 commit into
masterfrom
fix/assoc-double-expand

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Follow-up to #960. m[sub]=value ran the full whole-word expansion then discarded it (prepare rebuilt the synthetic word). GNU has no whole-word pass: general.c:480 assignment() splits syntactically, arrayfunc.c:382 assign_array_element_internal expands subscript once + value once. Run the assignment pass first and return its synthetic word when the whole-word pass is provably side-effect-free; whitelist over raw source (#117: $( $(( ${ backtick quotes backslash ~ all fall back). Tests: lib 673 + cli 469 green; 67-shape A/B zero changes. assoc interleave 8k: 0.87x (writes 0.83x).

…lement writes (rubash#960)

An associative element-assignment word (`m[sub]=value`) is expanded twice
per command: `expand_command_word` runs the full whole-word expansion, then
`prepare_array_element_assignment_word` resolves a synthetic word from the
raw subscript and the cooked value and the whole-word result is thrown
away. GNU has no whole-word pass for this shape — general.c:480
`assignment()` splits the syntactic word, arrayfunc.c:382
`assign_array_element_internal` expands the subscript once via
`expand_subscript_string` (arrayfunc.c:396 -> subst.c:11063) and the value
once as an assignment RHS (arrayfunc.c:865). #960 measured the discarded
pass as the largest remaining fixed cost on the write fast lane.

Run the assignment pass FIRST and return its synthetic word when the
whole-word pass is provably side-effect-free; otherwise keep the original
order. The admission is a whitelist over the raw source (rubash#117 — doubt
falls through): plain parameter reads (`$name`, `$1`, `$?`, `$@`, `$#` …)
and literal text only. Command substitution `$(`/arithmetic `$((`, braced
parameters `${`, backticks, process substitution, quoting, backslash and
tilde are rejected: each can run a side effect, or resolve differently
between the two passes (a side-effecting subscript changes the value the
value pass reads; a side-effecting value would run a different number of
times). Anything outside the class keeps the double pass unchanged.

The block sits right after the per-word memo guards and before the
pre-scans (`apply_parameter_assignment_expansions_in_word`, the eval /
brace / nameref / declaration arms), so the skipped pass costs nothing at
all. Those arms cannot fire for an admitted word: the whitelist already
rejects every marker they key on (`(`, `{`, quotes, `$(`), and the one arm
that would claim an element-assignment word —
`expand_simple_substitution_fragments` — already declines it explicitly.
Only the expansion entry point is touched; the generic word-expansion core
(`expand_word_mut`) is untouched.

Verified (aarch64 Termux, GNU bash 5.3.20 oracle):
- cargo fmt --check clean; `cargo test --lib` 670 passed / 0 failed;
  `cargo test --test cli_tests` 469 passed / 0 failed on a clean tree.
- assoc/array regressions green; `oils_spec_assoc_append_regressions::
  assoc_nameref` fails identically on cc0eeb4 (pre-existing rubash#961).
- A/B (base cc0eeb4 vs new, debug) over 67 element-assignment shapes
  (literal/var/append/space/nested-bracket/eq/tilde/quote/arith/cmdsub/
  nameref/readonly/integer/indexed/multi-assign/eval/declare/export/local/
  prefix-assignment): zero output or status changes. The fast path fires
  for `m[$i]=$v` / `A[k$i]=v$i` and does not fire for `m[$((n++))]`,
  `m[k]=$(...)`, `m["a]b"]` (verified by instrumentation, removed).
- Interleaved release, N=8000, min-of-9 (base vs new vs GNU bash):
  interleaved `A[k$i]=v$i; x=${A[k$i]}` new/base 0.84x; writes-only
  `A[k$i]=v$i` new/base 0.81x — matching the #960 experiment's
  0.83x/0.80x. vs GNU bash under the same run: interleaved 3.6-4.1x,
  writes-only 3.5-4.0x (host load average ~18, so the absolute bash
  multiple swings; new/base is load-independent and stable at 0.84/0.81).
@caomengxuan666
caomengxuan666 merged commit 9e75825 into master Oct 21, 2026
8 checks passed
@caomengxuan666
caomengxuan666 deleted the fix/assoc-double-expand branch October 21, 2026 08:01
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