Skip to content
This repository was archived by the owner on Nov 13, 2026. It is now read-only.

Improve performance of String.IndexOf(char) and String.LastIndexOf(char) - #1339

Merged
jkotas merged 1 commit into
dotnet:masterfrom
bbowyersmyth:StringIndexOfChar
May 30, 2016
Merged

jkotas merged 1 commit into
dotnet:masterfrom
bbowyersmyth:StringIndexOfChar

Conversation

@bbowyersmyth

Copy link
Copy Markdown
Contributor

Before porting to CoreCLR some changes need to be made to maintain the existing performance given by the C++ implementation.

Char not found

Method Median StdDev length
ClrIndexOf 2.3536 ns 0.0433 ns 1
RtIndexOf 2.2333 ns 0.0466 ns 1
NewIndexOf 2.0248 ns 0.0391 ns 1
ClrIndexOf 9.9791 ns 0.1260 ns 12
RtIndexOf 11.6317 ns 0.2680 ns 12
NewIndexOf 5.2121 ns 0.0663 ns 12
ClrIndexOf 35.3210 ns 0.3570 ns 50
RtIndexOf 46.3973 ns 1.0294 ns 50
NewIndexOf 33.8483 ns 0.3785 ns 50
ClrIndexOf 60.6154 ns 0.6055 ns 100
RtIndexOf 79.3279 ns 1.4144 ns 100
NewIndexOf 48.2122 ns 0.4125 ns 100
ClrIndexOf 109.5456 ns 0.7991 ns 200
RtIndexOf 155.3314 ns 3.7292 ns 200
NewIndexOf 77.1061 ns 0.7688 ns 200
ClrIndexOf 257.6082 ns 2.1920 ns 500
RtIndexOf 420.8954 ns 28.0848 ns 500
NewIndexOf 164.0551 ns 1.9831 ns 500

IndexOf - String of length 15 that differs at position offset

Method Median StdDev offset
ClrIndexOf 2.1723 ns 0.0982 ns 0
RtIndexOf 1.6250 ns 0.0636 ns 0
NewIndexOf 1.8871 ns 0.0591 ns 0
ClrIndexOf 2.4480 ns 0.0917 ns 1
RtIndexOf 2.7291 ns 0.1096 ns 1
NewIndexOf 2.6416 ns 0.1344 ns 1
ClrIndexOf 3.2352 ns 0.0878 ns 2
RtIndexOf 3.9107 ns 0.1636 ns 2
NewIndexOf 2.6931 ns 0.0828 ns 2
ClrIndexOf 3.9382 ns 0.1211 ns 3
RtIndexOf 5.0206 ns 0.2187 ns 3
NewIndexOf 4.0220 ns 0.1404 ns 3
ClrIndexOf 4.6619 ns 0.1129 ns 4
RtIndexOf 5.9470 ns 0.2495 ns 4
NewIndexOf 4.3271 ns 0.0891 ns 4
ClrIndexOf 5.2063 ns 0.0792 ns 5
RtIndexOf 5.7774 ns 0.1005 ns 5
NewIndexOf 3.5705 ns 0.1941 ns 5
ClrIndexOf 6.2780 ns 0.1335 ns 6
RtIndexOf 6.5456 ns 0.1182 ns 6
NewIndexOf 3.7988 ns 0.1829 ns 6
ClrIndexOf 6.8111 ns 0.1668 ns 7
RtIndexOf 7.7846 ns 0.3535 ns 7
NewIndexOf 4.0371 ns 0.2391 ns 7
ClrIndexOf 7.3673 ns 0.2072 ns 8
RtIndexOf 8.6105 ns 0.3107 ns 8
NewIndexOf 4.3943 ns 0.2680 ns 8
ClrIndexOf 7.8907 ns 0.1769 ns 9
RtIndexOf 9.4134 ns 0.4834 ns 9
NewIndexOf 4.7222 ns 0.3327 ns 9
ClrIndexOf 8.4381 ns 0.1870 ns 10
RtIndexOf 9.8019 ns 0.1831 ns 10
NewIndexOf 4.9610 ns 0.3498 ns 10
ClrIndexOf 8.9722 ns 0.1447 ns 11
RtIndexOf 10.6145 ns 0.1949 ns 11
NewIndexOf 5.2444 ns 0.2500 ns 11
ClrIndexOf 9.5154 ns 0.1324 ns 12
RtIndexOf 11.6821 ns 0.3126 ns 12
NewIndexOf 5.4905 ns 0.4615 ns 12
ClrIndexOf 10.0508 ns 0.2322 ns 13
RtIndexOf 12.6482 ns 0.3670 ns 13
NewIndexOf 5.7482 ns 0.4268 ns 13
ClrIndexOf 10.5811 ns 0.2319 ns 14
RtIndexOf 12.9956 ns 0.2965 ns 14
NewIndexOf 6.0515 ns 0.1922 ns 14

LastIndexOf - Char not found

Method Median StdDev length
ClrLastIndexOf 2.6540 ns 0.0523 ns 1
NewLastIndexOf 2.0758 ns 0.0590 ns 1
ClrLastIndexOf 8.5287 ns 0.1115 ns 12
NewLastIndexOf 5.3518 ns 0.1258 ns 12
ClrLastIndexOf 32.6194 ns 0.3135 ns 50
NewLastIndexOf 34.5868 ns 0.2946 ns 50
ClrLastIndexOf 52.5579 ns 0.5424 ns 100
NewLastIndexOf 49.3015 ns 0.6625 ns 100
ClrLastIndexOf 91.8019 ns 0.5884 ns 200
NewLastIndexOf 79.3640 ns 1.0746 ns 200
ClrLastIndexOf 209.0110 ns 1.6954 ns 500
NewLastIndexOf 167.7079 ns 6.4669 ns 500

LastIndexOf char found has roughly the same percentages as IndexOf.
Profiling code https://gist.github.com/bbowyersmyth/791ff071a10ef901ed7a

@bbowyersmyth

Copy link
Copy Markdown
Contributor Author

Note if we want to add some loop unrolling here the times are at least half of the original CLR C++ code.
It would help with finding '?' and '#' in a url for example.

Method Median StdDev length
NewIndexOfUnroll 1.7680 ns 0.0698 ns 1
NewIndexOfUnroll 3.7536 ns 0.0524 ns 12
NewIndexOfUnroll 11.8368 ns 0.1271 ns 50
NewIndexOfUnroll 21.4534 ns 0.2648 ns 100
NewIndexOfUnroll 53.3115 ns 0.5707 ns 200
NewIndexOfUnroll 119.1522 ns 0.8786 ns 500

The implementation is in the profiling code.

@jkotas

jkotas commented May 29, 2016

Copy link
Copy Markdown
Member

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 offset with incrementing pCh may be slightly better - something like:

    ReturnIndex3: pCh++;
    ReturnIndex2: pCh++;
    ReturnIndex1: pCh++;
    ReturnIndex:
    return (int)(pCh - pChars);

@bbowyersmyth

Copy link
Copy Markdown
Contributor Author

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.
It doesn't appear that the JIT combines them as I'm seeing (int_)(byte* + 4) being faster than (int_)(char_ + 2) in another function when they should be the same for this.

@bbowyersmyth

Copy link
Copy Markdown
Contributor Author

Clarification to the above: pCh++ comes through as an add but pCh + 2 comes out as a mul + add.

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.

Method Median StdDev length
NewIndexOfUnrollInc 1.9456 ns 0.0560 ns 1
NewIndexOfUnrollInc 4.1826 ns 0.0802 ns 12
NewIndexOfUnrollInc 12.3469 ns 0.1354 ns 50
NewIndexOfUnrollInc 31.1959 ns 0.3343 ns 100
NewIndexOfUnrollInc 50.0072 ns 0.4643 ns 200
NewIndexOfUnrollInc 111.9723 ns 1.1223 ns 500

Even still, going with that as it's half the original time and I find it more readable than what I had.

@manu-st

manu-st commented May 30, 2016 •

Copy link
Copy Markdown

I'm not sure if it has an impact, but what about which I find easier to read:

while (count >= 4)
{
        if ((*pCh == value) || (*++pCh == value) || (*++pCh == value) || (*++pCh == value)) return (int) (pCh - pChars);
        count -= 4;
}

@jkotas

jkotas commented May 30, 2016 •

Copy link
Copy Markdown
Member

I'm not sure if it has an impact

This pattern would have negative perf impact.

@jkotas

jkotas commented May 30, 2016

Copy link
Copy Markdown
Member

👍

@jkotas
jkotas merged commit 033fbc2 into dotnet:master May 30, 2016
@bbowyersmyth

Copy link
Copy Markdown
Contributor Author

Hey Manu, a few things there. The first if comparison needs an increment for the next iteration. After fixing that logic and starting pCh at -1 the above code for the 500 length runs at 146ns where as the PR is at 111ns.
Most of the structure of the PR is designed to reduce data dependencies and loop complexity. It is not a structure I would recommend for normal use but this is expected to be performance system code.

@bbowyersmyth
bbowyersmyth deleted the StringIndexOfChar branch May 30, 2016 22:13
@manu-st

manu-st commented May 31, 2016

Copy link
Copy Markdown

@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.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants