-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Optimize Ascii.Equals for small values #93191
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
03edbc2
da7f4a2
e193bce
e01effd
404b53f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -20,15 +20,35 @@ public abstract class AsciiEqualityTests<TLeft, TRight> | |||||
| protected abstract bool Equals(ReadOnlySpan<TLeft> left, ReadOnlySpan<TRight> right); | ||||||
| protected abstract bool EqualsIgnoreCase(ReadOnlySpan<TLeft> left, ReadOnlySpan<TRight> right); | ||||||
|
|
||||||
| private static readonly int[] BufferLengths = | ||||||
| [ | ||||||
| 1, | ||||||
| (sizeof(long) / sizeof(short)) - 1, | ||||||
| (sizeof(long) / sizeof(short)), | ||||||
| (sizeof(long) / sizeof(short)) + 1, | ||||||
| Vector128<short>.Count - 1, | ||||||
| Vector128<short>.Count, | ||||||
| Vector128<short>.Count + 1, | ||||||
| Vector256<short>.Count - 1, | ||||||
| Vector256<short>.Count, | ||||||
| Vector256<short>.Count + 1, | ||||||
| Vector512<short>.Count - 1, | ||||||
| Vector512<short>.Count, | ||||||
| Vector512<short>.Count + 1 | ||||||
| ]; | ||||||
|
|
||||||
| public static IEnumerable<object[]> ValidAsciiInputs | ||||||
| { | ||||||
| get | ||||||
| { | ||||||
| yield return new object[] { "test" }; | ||||||
|
|
||||||
| for (char textLength = (char)0; textLength <= 127; textLength++) | ||||||
| foreach (int textLength in BufferLengths) | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That is a lot of test cases, I am not sure how long it's going to take to run all of them with debug builds. Before your change, this test was covering strings that were 0 to 127 chars long. Now the max length is reduced to 33. I am not convinced that this is the right thing to do, please revert this particular change or convince me that it's the right thing to do. |
||||||
| { | ||||||
| yield return new object[] { new string(textLength, textLength) }; | ||||||
| for (char chr = (char)0; chr <= 127; chr++) | ||||||
| { | ||||||
| yield return new object[] { new string(chr, textLength) }; | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
|
|
@@ -55,15 +75,18 @@ public static IEnumerable<object[]> DifferentInputs | |||||
| { | ||||||
| yield return new object[] { "tak", "nie" }; | ||||||
|
|
||||||
| for (char i = (char)1; i <= 127; i++) | ||||||
| foreach (int textLength in BufferLengths) | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above, I don't see benefits of this change. |
||||||
| { | ||||||
| if (i != '?') // ASCIIEncoding maps invalid ASCII to ? | ||||||
| for (char chr = (char)0; chr <= 127; chr++) | ||||||
| { | ||||||
| yield return new object[] { new string(i, i), string.Create(i, i, (destination, iteration) => | ||||||
| if (chr != '?') | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this comment was valuable, please restore it
Suggested change
|
||||||
| { | ||||||
| destination.Fill(iteration); | ||||||
| destination[iteration / 2] = (char)128; | ||||||
| })}; | ||||||
| yield return new object[] { new string(chr, textLength), string.Create(textLength, chr, (destination, character) => | ||||||
| { | ||||||
| destination.Fill(character); | ||||||
| destination[destination.Length / 2] = (char)128; | ||||||
| })}; | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
|
|
@@ -91,11 +114,14 @@ public static IEnumerable<object[]> EqualIgnoringCaseConsiderations | |||||
| { | ||||||
| yield return new object[] { "aBc", "AbC" }; | ||||||
|
|
||||||
| for (char i = (char)0; i <= 127; i++) | ||||||
| foreach (int textLength in BufferLengths) | ||||||
| { | ||||||
| char left = i; | ||||||
| char right = char.IsAsciiLetterUpper(left) ? char.ToLower(left) : char.IsAsciiLetterLower(left) ? char.ToUpper(left) : left; | ||||||
| yield return new object[] { new string(left, i), new string(right, i) }; | ||||||
| for (char chr = (char)0; chr <= 127; chr++) | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see the value of this change, but we should decrease the number of test cases. How about just focusing on Ascii letters? Because for other characters we would be duplicating other tests work. |
||||||
| { | ||||||
| char left = chr; | ||||||
| char right = char.IsAsciiLetterUpper(left) ? char.ToLower(left) : char.IsAsciiLetterLower(left) ? char.ToUpper(left) : left; | ||||||
| yield return new object[] { new string(left, textLength), new string(right, textLength) }; | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
|
|
@@ -112,7 +138,7 @@ public static IEnumerable<object[]> ContainingNonAsciiCharactersBuffers | |||||
| { | ||||||
| get | ||||||
| { | ||||||
| foreach (int length in new[] { 1, Vector128<byte>.Count - 1, Vector128<byte>.Count, Vector256<byte>.Count + 1 }) | ||||||
| foreach (int length in BufferLengths) | ||||||
| { | ||||||
| for (int index = 0; index < length; index++) | ||||||
| { | ||||||
|
|
||||||
Uh oh!
There was an error while loading. Please reload this page.