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

Use vectorized SpanHelpers.SequenceEqual for string equality #16994

Merged
merged 1 commit into from
Mar 17, 2018

Conversation

jkotas
Copy link
Member

@jkotas jkotas commented Mar 16, 2018

SpanHelpers.SequenceEqual is our optimized workhorse for comparing blocks of memory. Use it for string equality instead of ad-hoc optimized loops.

"Hello world!".Equals("Hello world?") is 20% faster on x64 with this change and it only gets better from there.

@jkotas jkotas requested a review from ahsonkhan March 16, 2018 17:51
@jkotas
Copy link
Member Author

jkotas commented Mar 16, 2018

Depends on dotnet/corefx#28080 that is changing SpanHelpers.SequenceEqual argument to nuint.

@jkotas
Copy link
Member Author

jkotas commented Mar 16, 2018

cc @marek-safar

@@ -1126,7 +1018,10 @@ public Boolean StartsWith(String value, StringComparison comparisonType)
}
return (value.Length == 1) ?
true : // First char is the same and thats all there is to compare
StartsWithOrdinalHelper(this, value);
SpanHelpers.SequenceEqual(

Choose a reason for hiding this comment

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

What happens if this.Length is less than value.Length?

Copy link
Member Author

Choose a reason for hiding this comment

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

This condition is checked two lines above this one.

Copy link

@ahsonkhan ahsonkhan left a comment

Choose a reason for hiding this comment

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

lgtm

@marek-safar
Copy link

@jkotas very nice

We are still using https://github.com/dotnet/corert/blob/master/src/System.Private.CoreLib/src/System/String.Comparison.cs but this file looks quite easy to unify/share

@jkotas
Copy link
Member Author

jkotas commented Mar 17, 2018

@dotnet-bot test this please

@jkotas
Copy link
Member Author

jkotas commented Mar 17, 2018

@dotnet-bot test Ubuntu x64 Checked corefx_baseline
@dotnet-bot test Windows_NT x64 Checked corefx_baseline

@jkotas jkotas merged commit f8228fe into dotnet:master Mar 17, 2018
@jkotas jkotas deleted the spanhelpers-string branch March 19, 2018 05:44
@dotnet dotnet deleted a comment Nov 7, 2018
@dotnet dotnet deleted a comment from ahsonkhan Nov 7, 2018
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.

3 participants