Skip to content

[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 #617

Closed

Description

@caomengxuan666

Result summary

Parameter substring expansion ${var:N:M} is 11-12× slower than GNU on-device (Android/Termux, rubash master 334afab vs bash 5.3.20, idle best-of-3, 200 000 ops):

op rubash niu product GNU bash × slower
${s:5:3} (200k) 8 645 ms 9 547 ms 752 ms 11.5× / 12.7×

That's ~43 µs per slice vs bash's ~3.8 µs. The index/len are fixed integers, so there is no pattern to match — bash does a direct byte/codepoint slice.

Contrast with the other parameter paths measured this round (separate findings): ${#s} length 2.9×, ${s##*/} strip 2.1×, ${s//} 2.1×, ${s^^} 2.0× — all benign compared to the substring path.

Root-cause hypothesis: the substring path (src/executor/parameter_core.rs::eval_parameter_substring_offset and the substring dispatch at parse_parameter_substring) walks the source in characters per call rather than computing the byte range once and memcopying. A per-call full re-scan (or reopening the pattern machinery, cf. the // re-derivation flagged in #608) would explain the ~12×.

Suggested fix direction: when both offset and length are integer literals, compute the byte window directly (UTF-8 boundary guard), copying only the substring — no per-call re-scan, no pattern machinery. Keep the existing slow path only for the +/--computed (invalid/negative offset) fallback that GNU resolves at read time.

Repro

s=abcdefghijklmnopqrstuvwxyz; for((i=0;i<200000;i++)); do t=${s:5:3}; done
# rubash ~8.6s | bash ~0.75s

Activity

  1. added
    P1-high
    performance
    on Oct 15, 2026
  2. caomengxuan666 commented on Oct 15, 2026

    @caomengxuan666
    MemberAuthor

    Root-cause refined with a length discriminator (script-file, no shell-escaping harness — an earlier inline probe was invalidated by a quoting artifact). ${s:5:3} ×200k, idle, rubash vs bash:

    string length rubash bash × slower
    26 chars 8 672 ms 887 ms 9.8×
    ~1.1 M chars 136 079 ms — (15.7× the short-string result, i.e. cost scales with total length)

    So the cost is O(n) per slice in the string length, not fixed per-op overhead and not the offset arithmetic: ${s:$((5)):3} vs ${s:5:3} differed by only ~19% (+16 ms/op), and raising the string length from 26 → 1.1 M chars drove 8.7 s → 136 s. That points to the slice path materializing/translating the whole source string (e.g. a full chars()/copy pass) before extracting N:M, rather than a direct byte window with a boundary check. GNU bash stays ~O(1) for pure-ASCII slices. Suggest: compute the byte range directly and copy only the substring (guard UTF-8 boundaries at the two cut points) — keep the slow path only for computed offsets. 136 s for 200k ops on a 1.1 MB string is a 15× amplifier of the already-bad short-string ratio.

  3. caomengxuan666 commented on Oct 15, 2026

    @caomengxuan666
    MemberAuthor

    Correction after an attempted fix + A/B (and catching a measurement artifact in my own harness): the dominant cost is the braced-parameter dispatch, not the substring cut. I removed the O(n) work from the slice layer itself — the multibyte path's leading value.chars().count() full pass + second skip/take walk, and the byte path's double locale_byte_span to_vec() — rebuilt, and the benchmark did not move (26-char and ~1.1 M-char both still land at the same ~8.7 s / 136 s signature). So parameter_substring/parameter_substring_bytes are not the hotspot.
    What's left, for the maintainer's profile: the ~43 µs/op fixed cost (8.7 s / 200k) is incurred before the slice — in resolving ${s:5:3} (the parse_parameter_substring dispatch, eval_parameter_substring_offset re-running expand_substring_assoc_subscripts + expand_arithmetic_special_parameters per call, and the expand_braced_special detect path). This is the same per-eval class as #607 and the [[ ]] / case family in #619; the length growth I quoted earlier (8.7 → 136 s at 1.1 M chars) is a real but secondary effect in huge-value handling, not the headline.
    Actionable direction, not a patch: instrument the braced-parameter dispatch (time each of parse → detect → suffix-parse → offset-eval → apply) on a ${s:5:3} loop; the offset-eval's per-call arithmetic/assoc-subscript expansion is the prime suspect. Optimizing the slice low-level op will not close the gap. Keeping the issue open (11.5× is real and actionable) with this redirect.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

P1-highperformance

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions