Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Better performance for string.CompareOrdinalHelper - #6119

Merged
jkotas merged 26 commits into
dotnet:masterfrom
jamesqo:compare-perf
Jul 20, 2016
Merged

jkotas merged 26 commits into
dotnet:masterfrom
jamesqo:compare-perf

Conversation

@jamesqo

@jamesqo jamesqo commented Jul 5, 2016 •

Copy link
Copy Markdown

Right now string.CompareOrdinal is fast, but it could be a lot faster. This PR makes the following changes:

  • 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.
      • Since we were already checking for differences in m_firstChar in 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 in CompareOrdinalHelper.
  • Used goto within 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 *OrdinalHelper are for.)
  • Changed null check in CompareOrdinalHelper to Contract.Assert, instead of Contract.Requires.

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

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

Comment thread src/mscorlib/src/System/String.cs Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@jamesqo jamesqo Jul 5, 2016 •

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jkotas I understand your reasoning; is there any other way to read from the second char before pinning the string via fixed?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not think there is easy way to do this today.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@jkotas

jkotas commented Jul 5, 2016

Copy link
Copy Markdown
Member

LGTM otherwise. Thanks!

Comment thread src/mscorlib/src/System/String.cs Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not think there is anything "TODO" here

@jkotas

jkotas commented Jul 5, 2016

Copy link
Copy Markdown
Member

Thank you for adding the matching tests in corefx!

@bbowyersmyth

Copy link
Copy Markdown

Removing the m_secondChar is going to change the performance profile a bit. Even with it, a string now has to be 14 chars long to get into unrolled loop. It would be good to see timings on how much of a hit lengths 11 to 13 take.
Also the long reads make narrowing in on a difference a bit more expensive. Can you run some tests when the differences are a index 3 to 6 for example?

@jamesqo

jamesqo commented Jul 5, 2016 •

Copy link
Copy Markdown
Author

@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 Equals/StartsWithOrdinalHelper.

stephentoub pushed a commit to dotnet/corefx that referenced this pull request Jul 6, 2016
@jamesqo

jamesqo commented Jul 11, 2016

Copy link
Copy Markdown
Author

@jkotas Maybe this can be merged now that tests have been added to CoreFX for this? (Additionally the perf results are for the latest revision of this.)

@jkotas

jkotas commented Jul 12, 2016

Copy link
Copy Markdown
Member

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.

Comment thread src/mscorlib/src/System/String.cs Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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;

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@jkotas

jkotas commented Jul 12, 2016

Copy link
Copy Markdown
Member

Added a few comments about where it may be interesting to do additional tweaks. Thanks!

@jamesqo

jamesqo commented Jul 13, 2016 •

Copy link
Copy Markdown
Author

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

https://gist.github.com/jamesqo/04ae4d6142c16a4f9376fe430d26919d

https://gist.github.com/jamesqo/de2fa42f6ffe30e1bbabf3e893051320

(I can see what you meant about the tail-merging-- the same code seems to be getting generated multiple times for x86 around the return statements.)

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 dotnet publish for x86 runtimes (dotnet/aspnetcore#1624). I'll try copy-and-pasting the x86 CoreCLR binaries into my published app later, and see how that goes.

@SunnyWar

Copy link
Copy Markdown

if (*(a + 1) != *(b + 1)) goto DiffOffset1;

This is a change in style and reduces readability to boot.

Comment thread src/mscorlib/src/System/String.cs Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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);

@jamesqo jamesqo Jul 13, 2016 •

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I agree with your edit.

@jkotas
jkotas merged commit 719c34f into dotnet:master Jul 20, 2016
@jkotas

jkotas commented Jul 20, 2016

Copy link
Copy Markdown
Member

Thanks a lot for fine tuning this improvement. Could you please port the final change to CoreRT?

@jamesqo
jamesqo deleted the compare-perf branch July 20, 2016 00:07
@jamesqo

jamesqo commented Jul 20, 2016 •

Copy link
Copy Markdown
Author

@jkotas Gladly! Great to see this finally got merged 😄 😄 There are a bunch of more String optimizations coming.

picenka21 pushed a commit to picenka21/runtime that referenced this pull request Feb 18, 2022
- 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
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants