Repository navigation
perf(1.4.1): short-circuit literal integer offset/length in parameter substring ( 11.4x -> 2.4x) - #622
Merged
Conversation
… 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.
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.
Summary
${var:N:M}parameter-substring expansion paid a fixed ~43 µs/op because both the offset and the length go througheval_parameter_substring_offset*, which per operand runs: an assoc-subscript scan, an embedded-parameter walk, and a full$(( ))arithmetic parse (plus aVarTableclone on the&selfpath). 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.cexpand_arith_string(≈line 3981) is a literal fast path: it scans for$, backtick,CTLESC, or a quote — if none, it does asavestringcopy 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
Self::literal_arith_integer_text(s) -> Option<isize>— accepts only a trimmed optional-sign decimal, no-leading-zero integer literal.parse_parameter_substringandparse_parameter_substring_mutwith it, beforeeval_parameter_substring_offset*.Semantics preserved by falling through to the full eval for everything else:
$((2+3)),$var), leading-0 octals (010== 8, not 10 — deliberately excluded from the fast path so GNUevalexpbase handling is never bypassed), hex (0x10),'-quoted spans (still hit the single-quoted-operand error path), and negative/large offsets (returned raw asisizeand adjusted later inparameter_substring*, exactly as GNU does atsubst.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):~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_chainingsubstring-family green; differentialprobes.shbattery → 95/116 pass, unchanged (no user-visible compat regression).Closes #617.