Repository navigation
perf(assoc): trim per-op fixed costs on the write fast lane (#618) - #960
Merged
Merged
Conversation
assoc interleaved 8k (`m[k$i]=v$i; x=${m[k$i]}`) is still ~4.2x GNU bash
after the #937 typed-cell read fix. Scaling is linear (4k->32k doubles
with N), so there is no O(n) left in the write path: the #618
append + `array_compact_budget` scheme already amortizes the canonical
render/dedupe/sync. The residual is per-op fixed cost; this trims the
assoc-owned slice only (the discarded general word expansion in
`expand_command_word` is the larger remaining item and is owned by the
expansion pipeline, not touched here).
- `assoc_storage_append_element`: write the quoted `[k]=v` segment
straight into the mirror buffer instead of building a segment String, a
`format!(" {segment}")` and two quote Strings per append. Bytes are
identical to `format_assoc_storage` (pinned by a new unit test over the
quoting classes: plain/space/quote/backslash/`]`/`` ` ``/`$`/empty).
- `upsert_typed_assoc_element`: one BTreeMap lookup plus a HashMap upsert
instead of re-entering `set_associative_element` (name String alloc +
second lookup), and reuse the stored key on the overwrite arm — GNU
assoc.c `assoc_insert` overwrites in place.
- `prepare_array_element_assignment_word`: a subscript with no expansion
syntax and no `[`/`]`/`=` returns None without running the
side-effect-free `expand_subscript_string` pass (subst.c:11063 identity).
- `expand_subscript_string_inner`: skip the IFS_GLUE/E309/E401 marker-strip
chain when the expanded key carries none of the three sentinels
(`str::replace` always copies; GNU's dequote walks a marker-free C string
without copying).
- budget spends only on an OVERWRITE: a new key adds no duplicate segment,
so a pure fill never pays a canonical re-render, while an overwrite burst
still folds every `budget` writes. A cell the append cannot sync reports
`NoCell` and drops the budget so the next write re-syncs on the canonical
path (the "complete or absent" invariant is preserved).
Measured (aarch64 Termux, release, best-of-9 rotated, N=8000):
interleaved 4.21x -> 3.56x bash; writes-only 4.25x -> 3.34x; reads
3.43x -> 3.02x.
Verification: `cargo test --lib` 672 green; assoc/array/assignment
integration set green; `cli_tests` green except the dirty-tree
version-banner test. The one assoc integration failure,
`oils_spec_assoc_append_regressions::assoc_nameref`, is a pre-existing
debug-assert failure (nameref -> assoc-element scalar write leaves the
typed cell stale) — byte-identical on origin/master, not caused here.
caomengxuan666
force-pushed
the
fix/assoc-write-expand
branch
from
October 21, 2026 02:53
adfc189 to
3987ee5
Compare
caomengxuan666
added a commit
that referenced
this pull request
Oct 21, 2026
…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.
gdb: no O(n) frame; assoc writes already amortized by #618 append+budget (linear 4k->32k). Residual is fixed per-op cost. Cut: byte-direct mirror append (drops 3 String allocs/element), single-lookup typed upsert, skip identity expand_subscript_string pass for syntax-free subscripts, skip sentinel strip chain when key is marker-free, budget spends only on overwrite (pure fill never re-renders). Tests: lib 672 green; 25-target integration green (assoc_nameref fails identically on master - pre-existing, needs own issue). Interleave 8k 4.21x->3.56x, writes-only 4.25x->3.34x. Follow-up noted: assoc element-assignment word is expanded twice (~0.83x more available).