Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.Sign up
String.StartsWith performance - OrdinalCompareSubstring #2825
Alternative to #2667
Improves the performance of an ordinal based StartsWith comparison. The existing code has the overhead of figuring out the exact index that differs along with redundant argument validation.
OrdinalCompareSubstring is a variation of EqualsHelper which is aware when it has reached the end of the string and can control the result of reading the null terminator (or next character after the provided length). The bitwise OR provided the best performance while avoiding having a branch or a result dependency.
The new function does not replace EqualsHelper as it has enough of a different performance profile than I was comfortable applying to
Results for when strings match:
For the curious here is how OrdinalCompareSubstring performs in place of EqualsHelper within
Two 15 character strings that differ at index n:
Two equal length character strings:
I think it is fine for this to be a different method.
BTW: We will apply a profile guided optimizations (aka