Improve string.{Last}IndexOf perf on Unix for Ordinal/OrdinalIgnoreCase
authorstephentoub <stoub@microsoft.com>
Tue, 20 Oct 2015 19:34:38 +0000 (15:34 -0400)
committerstephentoub <stoub@microsoft.com>
Thu, 22 Oct 2015 18:31:58 +0000 (14:31 -0400)
commit80800eb0ead17dc724275d426904755b3bc64139
tree319fdadf2945c08ff72f3bd0b43955abb64e8455
parent3ddea17fc4e5b0c9ff2cb0651be82594a0eddffb
Improve string.{Last}IndexOf perf on Unix for Ordinal/OrdinalIgnoreCase

Our current implementation of IndexOfOrdinal for strings on Unix uses Substring to get the piece of the source string we care about; this results in an unnecessary allocation / string copy.  When using OrdinalIgnoreCase, we also convert both the source and search strings to upper-case using ToUpperInvariant, resulting in more allocations.  And our LastIndexOfOrdinal implementation delegates to IndexOfOrdinal repeatedly, incurring such allocations potentially multiple times.

This change reimplements Ordinal searching in managed code to not use Substring, and it implements OrdinalIgnoreCase searching via new functions exposed in the native globalization shim, so as to use ICU without having to make managed/native transitions for each character.

With the changes, {Last}IndexOf with Ordinal/OrdinalIgnoreCase are now allocateion-free (as you'd expect), and throughput when startIndex/count and/or OrdinalIgnoreCase are used is increased significantly, on my machine anywhere from 20% to 3x, depending on the inputs.
src/corefx/System.Globalization.Native/collation.cpp
src/mscorlib/corefx/Interop/Unix/System.Globalization.Native/Interop.Collation.cs
src/mscorlib/corefx/System/Globalization/CompareInfo.Unix.cs