Skip to content

Vectorize the substring searches behind String#index, #rindex and #include? - #79

Merged
Largo merged 1 commit into
masterfrom
ccr-4d94625d-0splsp-search
Nov 3, 2026
Merged

Largo merged 1 commit into
masterfrom
ccr-4d94625d-0splsp-search

Conversation

@Largo

@Largo Largo commented Nov 3, 2026

Copy link
Copy Markdown
Owner

Utils.IndexOf / Utils.LastIndexOf in Src/Ruby/Runtime/Utils.cs have 16 overloads, unchanged since the 2016 import. Each one searched with a nested loop over every candidate position. They are what String#index, #rindex and #include? use on char-array and binary string contents. The searches that mix bytes and chars (a binary string against a char needle, and the reverse) also fall back to them.

Change

  • Two generic cores now do the work with the runtime's vectorized ReadOnlySpan<T>.IndexOf / LastIndexOf. Behaviour is unchanged:
    • the same search window, including count past the end and Int32.MaxValue counts;
    • the same answer for an empty needle (start, also when start is past the end).
  • Mixed element types are converted first. A byte needle is widened to chars; a char needle is narrowed to bytes after a vectorized check that every char is ≤ ÿ. A char above ÿ can't equal any byte, so such a needle is not found, exactly as before.
  • The net effect is −143 lines: the 16 overloads are now one-liners over the two cores.

Correctness

A harness copied the old implementations from master and compared them, via reflection, with the new ones. That covered all 16 overloads on 3,000,000 random calls:

  • small alphabets so that matches are frequent, chars above 0xff, and buffers with capacity beyond the length;
  • start/count at and past the bounds within MutableString's contract, plus (Int32.MaxValue - 1, Int32.MaxValue) for LastIndexOf.

The results were identical, including the exceptions.

Numbers (Release, net8.0)

In C#, with a 1 MiB haystack, the needle at the far end, 50 calls:

before after
IndexOf(char[], string) 48.9 ms 8.5 ms
IndexOf(byte[], byte[]) 48.9 ms 4.1 ms
IndexOf(byte[], string) 48.7 ms 3.9 ms
IndexOf(string, byte[]) 33.2 ms 8.5 ms
IndexOf(char[], byte) 18.7 ms 3.2 ms
LastIndexOf(char[], string) 49.0 ms 7.9 ms
LastIndexOf(byte[], byte[]) 42.2 ms 4.0 ms

In Ruby, with a 100 KB string, 2,000 calls, best of 3:

before after
String#index (char content) 155 ms 22 ms
String#include? (binary) 176 ms 24–28 ms
String#index (binary) 177 ms 25–29 ms
each_line / split over 80-byte lines 425 / 367 ms 432–465 / 356–398 ms (unchanged: allocation dominates)

Testing (Linux, net8.0, Release)

  • Util/run-tests.sh (IronRuby.Tests): PASSED, 0 failure lines.
  • ruby/spec at the CI pin, covering core/string, core/symbol, library/stringio, library/stringscanner and core/io/{readlines,gets}: 5,258 examples, 0 failures.
    • The 2 errors are mspec's fixnum_max() helper ("unknown implementation") in the shared slice spec for String#byteslice/String#[]. They are the same on master.

This is independent of #78 and branches from master.


Generated by Claude Code

Utils.IndexOf/LastIndexOf, unchanged since the 2016 import, searched
with a nested loop per candidate position. They are what
String#index/#rindex/#include? use on char-array and binary contents,
and what the mixed byte/char cases fall back to.

Two generic cores now do the work with the runtime's vectorized
span IndexOf/LastIndexOf, with the same window, the same answer for an
empty needle and the same handling of a count past the end. A needle of
the other element type is widened (bytes to chars) or narrowed (chars
to bytes) first; a char above ÿ can't equal any byte, so such a
needle simply isn't found, as before.

Checked against the old code on 3,000,000 random calls covering all 16
overloads, start/count at and past the bounds and Int32.MaxValue counts:
identical results and exceptions.

1 MiB haystack, 50 calls: 33-49 ms -> 4-9 ms per overload.
Ruby, 100 KB string, 2000 calls (Release, net8.0):
  String#index (char content)    155 -> 22 ms
  String#include? (binary)       176 -> 24-28 ms
  String#index (binary)          177 -> 25-29 ms
each_line/split over short lines: unchanged (allocation dominates).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01KPwZf5xqgL3eZiUCWPfG2b
@coderabbitai

coderabbitai Bot commented Nov 3, 2026

Copy link
Copy Markdown

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 38 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

Learn how review limits work.

Review configuration:

Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 0d038f47-5d52-462d-bd6f-f2750a39f3fa
Commits

Reviewing files that changed from the base of the PR and between e84190b and c3f8d75.

Files selected for processing: 1
  • Src/Ruby/Runtime/Utils.cs
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

CodeRabbit Verify

  • Run CodeRabbit Verify to test the changes in this pull request.

@Largo
Largo merged commit 3f78f37 into master Nov 3, 2026
8 checks passed
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.

2 participants