Repository navigation
perf(assoc): skip discarded whole-word pass on side-effect-free element writes - #963
Merged
Merged
Conversation
…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).
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.
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).