Repository navigation
Better performance for string.CompareOrdinalHelper - #6119
Conversation
There was a problem hiding this comment.
I do not think it is a good idea to introduce this field. Many random things out there depend on implementation details of string ... this has good chance to throw them off.
There was a problem hiding this comment.
@jkotas I understand your reasoning; is there any other way to read from the second char before pinning the string via fixed?
There was a problem hiding this comment.
I do not think there is easy way to do this today.
There was a problem hiding this comment.
@jkotas Gotten rid of the field and moved the check after fixed. The results appear to have slowed down slightly for a difference in the second char (0.020... to 0.023/5...) but it's still faster than the previous version.
|
LGTM otherwise. Thanks! |
There was a problem hiding this comment.
I do not think there is anything "TODO" here
|
Thank you for adding the matching tests in corefx! |
|
Removing the m_secondChar is going to change the performance profile a bit. Even with it, a |
|
@bbowyersmyth I've updated my test results with the scenarios you mentioned. You are correct, lengths 10-13 appear to regress slightly in performance (about 10-25%). However, it's only for those 4 lengths. Differences at 3 and 6 (entering the loop-unrolled code) appear to have mostly the same results. I think it's best to do the same thing we do in |
* Add more test coverage for dotnet/coreclr#6119
|
I was planning to do a bit more due diligence on this before merging this, once I am back in office in a week. In particular, I would like to verify the perf characteristics on x86 - if you have time to run your perf test on x86, it would help. |
There was a problem hiding this comment.
The long block of jump targets may not be the best choice for 32-bit platforms. E.g. it may be better to chance the first two to:
DiffOffset8: a += 8; b += 8; goto DiffOffset0;
DiffOffset6: a += 6; b += 6; goto DiffOffset0;
There was a problem hiding this comment.
Yeah, I just took a look at the generated x86 code. If DiffOffset8 is hit we have to go through 8 add instructions before hitting DiffOffset0, vs only 4 on x64. I agree the jump may be more worth it here than on 64-bit.
|
Added a few comments about where it may be interesting to do additional tweaks. Thanks! |
|
@jkotas Thanks for the pull request feedback. I dumped the disassembly for both the x64 and x86 version of this function (respectively) in its current state so you can see it: (I can see what you meant about the tail-merging-- the same code seems to be getting generated multiple times for x86 around the BTW I'm not sure if I'll be able to get benchmarks for x86 directly; I was trying that with my other String PR, but was unable to do so since the CLI is having issues with |
|
This is a change in style and reduces readability to boot. |
There was a problem hiding this comment.
Have you tried to measuring the different options here?
I would expect that this is going to have extra memory fetch that should cost a few extra cycles (many iterations are needed to measure a few cycle differences).
I would expect that the following is going to work better:
DiffNextInt:
if (*a == *b)
return *a - *b;
DiffOffset1:
Contract.Assert(...);
return *(a + 1) - *(b + 1);
There was a problem hiding this comment.
You're probably right; I had just refactored that to make the code size smaller (so there was only one return point, so multiple of these wouldn't get generated). I have yet to make any measurements.
Regarding the 64-bit version, do you think it would also be worth the extra code to avoid incrementing them by 2 and instead specialize the return for each char in the word? e.g.:
DiffOffset0:
#if BIT64
if (*a != *b) return *a - *b;
if (*(a + 1) != *(b + 1)) return *(a + 1) - *(b + 1);
if (*(a + 2) != *(b + 2)) return *(a + 2) - *(b + 2);
if (*(a + 3) != *(b + 3)) return *(a + 3) - *(b + 3);
#else // ...edit: Not going to do that since it will either increase generated code size/add an additional branch if we jump to DiffNextInt from the fallback loop, while simultaneously increasing LOC.
Also shaves down the code a bit.
…h are compiled out in Release builds
This reverts commit 50e30c7.
|
Thanks a lot for fine tuning this improvement. Could you please port the final change to CoreRT? |
|
@jkotas Gladly! Great to see this finally got merged 😄 😄 There are a bunch of more String optimizations coming. |
- For 64-bit, process a `long` at a time (and 3 longs per iteration of the loop), since it's the largest word size of the processor. This is what other functions, like `EqualsHelper` or `StartsWithOrdinalHelper` do. - Make sure we perform aligned reads on 64-bit platforms - Strings (objects in general?) are allocated on an aligned word boundary (that is, divisible by 4/8). However, the offset to the chars (`OffsetToStringData`) is 12 on 64-bit platforms, meaning that if we read a long from the first char it will *not* be aligned. To fix this, first check for differences in the first/second chars and start the loop from there. - Used `goto` within the loop code, so we don't have to keep making jumps in the common case. I also removed a comment above the `String` class that seems to be outdated (seeing as there is no string constructor that accepts another string). Commit migrated from dotnet/coreclr@719c34f
Right now
string.CompareOrdinalis fast, but it could be a lot faster. This PR makes the following changes:longat a time (and 3 longs per iteration of the loop), since it's the largest word size of the processor. This is what other functions, likeEqualsHelperorStartsWithOrdinalHelperdo.OffsetToStringData) is 12 on 64-bit platforms, meaning that if we read a long from the first char it will not be aligned. To fix this, first check for differences in the first/second chars and start the loop from there.m_firstCharin the callers, I've added a new field to String,m_secondChar. This will be the null terminator for one-char strings, and also 0 for empty strings due to padding (see the notes I made above it). I used this to compare the second chars inCompareOrdinalHelper.gotowithin the loop code, so we don't have to keep making jumps in the common case. (I haven't looked at the disassembly, but I'm assuming that's what all of the existing gotos in*OrdinalHelperare for.)CompareOrdinalHelpertoContract.Assert, instead ofContract.Requires.I also removed a comment above the
Stringclass that seems to be outdated (seeing as there is no string constructor that accepts another string).Perf results: old / new / test source code
cc @bbowyersmyth @jkotas @mikedn
This passes all the CoreFX tests. (I'm planning on adding more shortly to improve coverage.)