dotnet / aspnetcore Public
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use IndexOfAny(char, char) with Span #39743
base: main
Are you sure you want to change the base?
Conversation
Use AsSpan().IndexOfAny(char, char) rather than pre-allocate an array for string.IndexOfAny(params char[]).
I just realised that based on the benchmark result for |
Thanks for contributing this, @martincostello! I can sort of speculate about why the However I don't have any intuition for why |
A fair question. Looking at the source, my guess would be that maybe it's due to the aggressive inlining? Otherwise the implementations seem to ultimately go to the same place to me. |
Agreed, that looks like the only difference (assuming that So if I'm reading this correctly, the optimization in this PR does have a behavioral difference, in that the old version would throw for a null input, and the new one won't. However I don't think there can be a null input so that's OK. |
This is old routing, so it doesn't really impact anything :) |
Use ROS.IndexOfAny(char, char)
Use
IndexOfAny(char, char)
withReadOnlySpan<char>
.Description
Use
AsSpan().IndexOfAny(char, char)
rather than pre-allocate a static array for use withIndexOfAny(params char[])
on a string.I spotted the array in
DefaultFileVersionProvider
while working on #39738 so thought I'd see if there were any that could be removed in lieu of overloads that just took the characters without an array. This PR updates all the ones I found that were in projects with TFMs that supported it, except for the usages inRoutePatternFactory
that use an array of 5 characters in multiple places.I wrote a quick microbenchmark comparing the two approaches with .NET 6, and using the span version does appear to give a performance improvement.
Benchmarks
Results
Code
The text was updated successfully, but these errors were encountered: