Skip to content

perf(1.4.1): short-circuit literal integer offset/length in parameter substring ( 11.4x -> 2.4x) - #622

Merged
caomengxuan666 merged 1 commit into
masterfrom
perf/substring-literal-fastpath
Oct 15, 2026
Merged

caomengxuan666 merged 1 commit into
masterfrom
perf/substring-literal-fastpath

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Summary

${var:N:M} parameter-substring expansion paid a fixed ~43 µs/op because both the offset and the length go through eval_parameter_substring_offset*, which per operand runs: an assoc-subscript scan, an embedded-parameter walk, and a full $(( )) arithmetic parse (plus a VarTable clone on the &self path). For the hot literal case ${s:5:3} this is pure overhead: GNU bash evaluates it once and treats a bare integer literally.

Root cause (GNU comparison)

GNU subst.c expand_arith_string (≈line 3981) is a literal fast path: it scans for $, backtick, CTLESC, or a quote — if none, it does a savestring copy and skips all expansion machinery (subst.c:4038). Offsets/lengths that look like a plain integer need no arithmetic/parameter expansion at all. rubash was running the full arithmetic pipeline twice (offset + length) for every expansion regardless.

Change

  • Add Self::literal_arith_integer_text(s) -> Option<isize> — accepts only a trimmed optional-sign decimal, no-leading-zero integer literal.
  • Short-circuit the offset and length branches of parse_parameter_substring and parse_parameter_substring_mut with it, before eval_parameter_substring_offset*.

Semantics preserved by falling through to the full eval for everything else:

  • real arithmetic expressions ($((2+3)), $var), leading-0 octals (010 == 8, not 10 — deliberately excluded from the fast path so GNU evalexp base handling is never bypassed), hex (0x10), '-quoted spans (still hit the single-quoted-operand error path), and negative/large offsets (returned raw as isize and adjusted later in parameter_substring*, exactly as GNU does at subst.c:8455-8504).

Validation (idle, rubash master 334afab vs GNU bash 5.3.20)

Correctness — 10/10 vs bash (script-file, no harness escaping): ${s:5:3}, ${s:5}, ${s: -3:2}, i=7;${s:$i:2}, ${s:$((2+3)):2}, ${s:010:1}, ${s:0x3:1}, ${s:+x}, ${s:5:0}, concatenated slice.

Performance — A/B (${s:5:3} ×200k):

before after GNU bash
time 8 672 ms 1 826 ms 761 ms
× bash 11.4× 2.4× —

~4.7× faster; the remaining 2.4× is the (now single-pass) braced dispatch, not the slice.

Regression gates: cargo test --lib → 578/578 pass; executor_command_chaining substring-family green; differential probes.sh battery → 95/116 pass, unchanged (no user-visible compat regression).

Closes #617.

… substring

${var:N:M} paid a fixed ~43us/op because both offset and length went through
eval_parameter_substring_offset*, which runs the assoc-subscript scan,
embedded-parameter walk and arithmetic parser per operand. Per the GNU fast
path (subst.c expand_arith_string), a trimmed optional-sign decimal integer
literal needs no expansion.

Add literal_arith_integer_text() and short-circuit the offset/length branches
of parse_parameter_substring (and the _mut form). Real arithmetic expressions,
leading-0 octals, hex, quoted spans and variable offsets still fall through to
the full eval, so GNU evalexp base handling and the invalid-offset
expansion-error path are unchanged.

Measured (idle, rubash master 334afab): ${s:5:3} x200k 8.7s -> 1.83s (11.4x
-> 2.4x vs bash). Correctness 10/10 vs bash (010 octal, 0x3, negative offset,
$i and $(( )) offsets); lib 578/578; probes 95/116 unchanged.
Closes #617.
@caomengxuan666
caomengxuan666 merged commit 7f23e22 into master Oct 15, 2026
8 checks passed
@caomengxuan666
caomengxuan666 deleted the perf/substring-literal-fastpath branch October 16, 2026 00:44
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.

[perf][P1] Parameter substring ${var:N:M} is 11-12× slower (43µs/op vs 3.8µs) — per-op re-scan instead of a direct byte slice

1 participant