Skip to content

perf(expand): inert-prefix brace-close fast path + ${name} whole-word lane - #965

Merged
caomengxuan666 merged 1 commit into
masterfrom
fix/param-expand-shared-brace-scan
Oct 21, 2026
Merged

caomengxuan666 merged 1 commit into
masterfrom
fix/param-expand-shared-brace-scan

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Parameter-expansion group is a systemic 2-2.5x across the 68-item matrix.

Profile: the same ${...} body is brace-scanned 4-5x per expansion (walker whole_braced, assignment pre-scan, fragment expand, quoted word, braced word); each scan allocates a String + Vec. bare-brace (2.31x) vs bare-dollar (1.26x) isolates the ${} entry tax (~4us/iter vs bash ~0.1us).

Fix (shared entry layer only, whitelist + fall-through, no operator changes):

  1. matching_parameter_brace_in_context inert-prefix fast path: if no \ $ { } ` ( ) < > " ' [ ] (or C0/non-ASCII) before }, the first } closes. GNU subst.c:1825 extract_dollar_brace_string skips/nests exactly those constructs; quote/POSIX context does not change RBRACE nesting.
  2. fast_plain_scalar_rhs whole-word ${name} reuses the existing $name lane (GNU subst.c:9777 parameter_brace_expand same path; rejects dynamic/set -u, non-name bodies fall back).

Numbers (release best-of-3, GNU bash 5.3.20): operators -16~-22% (default-minus 2.21->1.76x, length 2.37->1.90x, replace-all 2.12->1.73x, prefix-trim 2.01->1.60x, upper 1.90->1.55x, ...); ${x} entry tax eliminated (bare-brace 2.31->1.31x, now level with bare-dollar); control load unchanged.

Tests: lib 674 + cli 469 + typed-cell fuzz green; byte-identical vs bash (brace-diff.sh, brace-name-diff.sh: unset/array/nameref/IFS/glob/backslash).

…itelist

gdb sampling (release, 120 samples/load) of the 68-item matrix's parameter
group showed the systematic 2.0-2.34x is a real per-${} entry tax, not any
single operator: bare ${x} (assignment RHS) runs 2.28x while unbraced $x runs
1.38x. The tax is diffuse at the leaf (no function >10%) but structurally a
shared redundancy: every ${...} fragment is brace-scanned several times per
expansion, and each scan goes through the allocating lexer
(scan_braced_parameter_body wraps the input in a String, collects a Vec<char>,
and builds a byte-translation table). The walker span check, the fragment
re-entry, the quoted-word entry, and the braced expander each call
matching_parameter_brace_in_context over the same body.

Two shared-entry cuts, both whitelists that fall through to the real scanner
(rubash#117 discipline); no operator implementation is touched.

1. matching_parameter_brace_in_context (parameter_ops.rs): when no byte before
   the first } can open a construct that skips or nests it, the first } is the
   terminator under every quote/POSIX setting, so return it without the lexer
   scan. The inert-byte set is exactly the constructs GNU's
   extract_dollar_brace_string (subst.c:1825) reacts to: backslash/CTLESC
   escape (pass_character), backtick, $( / <( / >( substitution spans,
   double/single quote spans, an [ subscript (skipsubscript), and nested ${.
   dolbrace_state only gates quote handling, never the RBRACE nesting
   decrement, so the plain-prefix answer is context-independent. Anything with
   a quote, backslash, $, [, or substitution still runs the full scanner.
   Unit test pins the fast arm against the slow scan's contract.

2. fast_plain_scalar_rhs (assignment_expansion.rs): a whole-word NAME=${name}
   RHS with a bare identifier body is the same expansion as NAME=$name (GNU
   subst.c:9777 parameter_brace_expand falls through to
   parameter_brace_expand_word/param_expand for a name with no operator), so
   reuse the existing $name shortcut. fast_plain_copy already refuses
   dynamics, RANDOM/SRANDOM and set -u, and any non-name body (${a[0]},
   ${!r}, ${s:-w}, ${#s}) fails it and falls through unchanged.

Also: resolve_dollar_quoted_parameter_name early-outs on a name that does not
start with $' before collecting a Vec<char>; the walker's whole_braced probe
checks the cheap thread-local site flag before the span scan.

Measured (release, three-way interleaved best-of-3, GNU bash 5.3.20 aarch64),
old -> new vs bash:
  default-minus     2.11x -> 1.68x    length          2.03x -> 1.70x
  default-minus-set 2.34x -> 1.90x    substr-off-len  2.11x -> 1.73x
  substr-neg        2.11x -> 1.79x    replace-all     2.04x -> 1.73x
  replace-first     1.98x -> 1.68x    upper-all       1.64x -> 1.42x
  lower-all         1.73x -> 1.47x    prefix-trim     1.96x -> 1.59x
  suffix-trim       2.10x -> 1.67x    indirect-nameref 1.86x -> 1.67x
  bare-brace        2.28x -> 1.33x    bare-dollar     1.38x -> 1.38x (control)
The operator loads keep the walker/operator cost; only bare-brace loses the
whole entry tax (now == $x). Residual after the cut is diffuse: shared
per-command overhead plus the per-operator work, with the walker entry chain
and the funsub-span scan as the next targets.

Verified: cargo fmt; cargo test --lib 674 green (incl. the new scanner test);
cli_tests 468/469 (the one failure is the version-banner dirty stamp from the
pre-existing third_party/bash gitlink drift, not this change); typed-cell
verifier typed_cell_coherence_fuzz 2 green; parameter/assignment/quote
regression targets green; byte-identical to GNU bash 5.3.20 on a brace
differential (fast-arm and slow-arm shapes, assignment and argument
position, set -u) and a ${name}-vs-$name differential.
@caomengxuan666
caomengxuan666 merged commit 40a8db6 into master Oct 21, 2026
8 checks passed
@caomengxuan666
caomengxuan666 deleted the fix/param-expand-shared-brace-scan branch October 21, 2026 08:04
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