Repository navigation
Improve performance of String.IndexOf(char) and String.LastIndexOf(char) - #1339
Conversation
|
Note if we want to add some loop unrolling here the times are at least half of the original CLR C++ code.
The implementation is in the profiling code. |
|
Thank you for doing all these perf fixes! The loop unrolling looks pretty good to me. Would you like to update this PR with it? Replacing |
|
Thanks @jkotas I'll replace the PR with unrolling after I give your suggestion a profile. One thing I've noticed is that incrementing a char_, or dereferencing an offset, is more complex than it needs to be. It compiles to a mul by 2 then an add to the pointer. I haven't found in Roslyn where it determines a mul is needed but it would be good to pre-multiply the constant value seeing pointers are normally used for performance. |
|
Clarification to the above: Your increment suggestion has almost the exact same times when a match is found. Some interesting results when no match is found on length 100. I can't explain that one.
Even still, going with that as it's half the original time and I find it more readable than what I had. |
|
I'm not sure if it has an impact, but what about which I find easier to read: |
This pattern would have negative perf impact. |
|
👍 |
|
Hey Manu, a few things there. The first |
|
@bbowyersmyth Thanks for running the benchmarks and fixing the code :-). I ran it on my end against the .NET Framework. I got similar results. One thing I found out was that the return statement in the loop had a negative impact. For my proposed loop, it went from 165ns with a return vs 145ns with a goto. |
Before porting to CoreCLR some changes need to be made to maintain the existing performance given by the C++ implementation.
Char not found
IndexOf - String of length 15 that differs at position offset
LastIndexOf - Char not found
LastIndexOf char found has roughly the same percentages as IndexOf.
Profiling code https://gist.github.com/bbowyersmyth/791ff071a10ef901ed7a