Repository navigation
Vectorize the substring searches behind String#index, #rindex and #include? - #79
Merged
Merged
Conversation
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
|
Warning Review limit reachedYou'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. View limit details
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. CodeRabbit Verify
|
32 of 36 tasks
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.
Utils.IndexOf/Utils.LastIndexOfinSrc/Ruby/Runtime/Utils.cshave 16 overloads, unchanged since the 2016 import. Each one searched with a nested loop over every candidate position. They are whatString#index,#rindexand#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
ReadOnlySpan<T>.IndexOf/LastIndexOf. Behaviour is unchanged:countpast the end andInt32.MaxValuecounts;start, also whenstartis past the end).ÿ. A char aboveÿcan't equal any byte, so such a needle is not found, exactly as before.Correctness
A harness copied the old implementations from
masterand compared them, via reflection, with the new ones. That covered all 16 overloads on 3,000,000 random calls:0xff, and buffers with capacity beyond the length;start/countat and past the bounds withinMutableString's contract, plus(Int32.MaxValue - 1, Int32.MaxValue)forLastIndexOf.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:
IndexOf(char[], string)IndexOf(byte[], byte[])IndexOf(byte[], string)IndexOf(string, byte[])IndexOf(char[], byte)LastIndexOf(char[], string)LastIndexOf(byte[], byte[])In Ruby, with a 100 KB string, 2,000 calls, best of 3:
String#index(char content)String#include?(binary)String#index(binary)each_line/splitover 80-byte linesTesting (Linux, net8.0, Release)
Util/run-tests.sh(IronRuby.Tests): PASSED, 0 failure lines.core/string,core/symbol,library/stringio,library/stringscannerandcore/io/{readlines,gets}: 5,258 examples, 0 failures.fixnum_max()helper ("unknown implementation") in the shared slice spec forString#byteslice/String#[]. They are the same onmaster.This is independent of #78 and branches from
master.Generated by Claude Code