Skip to content

Forward nth and size_hint on JsStr windows iterator - #5663

Open
shu-chen-dot-ca wants to merge 1 commit into
boa-dev:mainfrom
shu-chen-dot-ca:fix-5651-index-of-from-index
Open

shu-chen-dot-ca wants to merge 1 commit into
boa-dev:mainfrom
shu-chen-dot-ca:fix-5651-index-of-from-index

Conversation

@shu-chen-dot-ca

Copy link
Copy Markdown

This Pull Request fixes/closes #5651.

JsStr::index_of (StringIndexOf) does self.windows(n).skip(from_index). Our Windows wrapper only implemented next, so Skip fell back to the default Iterator::nth, which calls next once per skipped window. Every search therefore cost O(from_index) before its first comparison, which made forward indexOf loops, includes with a position, and split / replaceAll with a string pattern quadratic in the string length.

It changes the following:

  • Windows now forwards nth and size_hint to the inner std::slice::Windows, so skipping is constant time (and size_hint is exact, matching the existing ExactSizeIterator impl).
  • Adds windows_nth_and_size_hint (checks nth, size_hint and the following next against std::slice::Windows for Latin-1 and UTF-16, every window size and skip count, including out of range ones).
  • Adds index_of_from_index (compares index_of against a naive search for every from_index up to len + 2, for one- and two-unit needles and the empty needle, on Latin-1 and UTF-16 strings, plus a match right at a large from_index).

Timings with the release boa CLI on a string of "abcdefghi@".repeat(40000) (400,000 units, 40,000 matches):

before after
indexOf loop from the previous match 3425 ms 14 ms
split("@") 3692 ms 2 ms
replaceAll("@", "#") 3053 ms 4 ms
indexOf loop, UTF-16 string 3661 ms 13 ms
lastIndexOf loop (unchanged, for comparison) 33 ms 24 ms

All results are identical before and after.

Tested with:

  • cargo fmt --all --check
  • cargo clippy -p boa_string --all-features --all-targets -- -D warnings
  • cargo test -p boa_string
  • cargo test -p boa_engine -- builtins::string builtins::regexp

Screenshots

Before After
before after

@shu-chen-dot-ca
shu-chen-dot-ca requested a review from a team as a code owner November 13, 2026 05:03
@github-actions github-actions Bot added the Waiting On Review Waiting on reviews from the maintainers label Nov 13, 2026
@github-actions github-actions Bot added this to the v0.23 milestone Nov 13, 2026
@github-actions github-actions Bot added the C-Tests Issues and PRs related to the tests. label Nov 13, 2026

This branch has not been deployed

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

Labels

C-Tests Issues and PRs related to the tests. Waiting On Review Waiting on reviews from the maintainers

Projects

None yet

Development

Successfully merging this pull request may close these issues.

StringIndexOf costs O(fromIndex), so indexOf loops, split and replaceAll are quadratic

1 participant