Skip to content

Extend case optimization in MatchCharacterClass to all chars that differ by one bit #63275

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

Merged
merged 1 commit into from
Jan 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3177,19 +3177,20 @@ private static string MatchCharacterClass(bool hasTextInfo, RegexOptions options
return $"(char.GetUnicodeCategory({chExpr}) {(negate ? "!=" : "==")} global::System.Globalization.UnicodeCategory.{category})";
}

// Next, if there's only 2, 3, or 4 chars in the set (fairly common due to the sets we create for prefixes),
// Next, if there's only 2 or 3 chars in the set (fairly common due to the sets we create for prefixes),
// it may be cheaper and smaller to compare against each than it is to use a lookup table. We can also special-case
// the very common case with case insensitivity of two characters next to each other being the upper and lowercase
// ASCII variants of each other, in which case we can use bit manipulation to avoid a comparison.
if (!invariant && !RegexCharClass.IsNegated(charClass))
{
Span<char> setChars = stackalloc char[4];
Span<char> setChars = stackalloc char[3];
int mask;
switch (RegexCharClass.GetSetChars(charClass, setChars))
{
case 2:
if ((setChars[0] | 0x20) == setChars[1])
if (RegexCharClass.DifferByOneBit(setChars[0], setChars[1], out mask))
{
return $"(({chExpr} | 0x20) {(negate ? "!=" : "==")} {Literal(setChars[1])})";
return $"(({chExpr} | 0x{mask:X}) {(negate ? "!=" : "==")} {Literal((char)(setChars[1] | mask))})";
}
additionalDeclarations.Add("char ch;");
return negate ?
Expand All @@ -3198,24 +3199,13 @@ private static string MatchCharacterClass(bool hasTextInfo, RegexOptions options

case 3:
additionalDeclarations.Add("char ch;");
return (negate, (setChars[0] | 0x20) == setChars[1]) switch
return (negate, RegexCharClass.DifferByOneBit(setChars[0], setChars[1], out mask)) switch
{
(false, false) => $"(((ch = {chExpr}) == {Literal(setChars[0])}) | (ch == {Literal(setChars[1])}) | (ch == {Literal(setChars[2])}))",
(true, false) => $"(((ch = {chExpr}) != {Literal(setChars[0])}) & (ch != {Literal(setChars[1])}) & (ch != {Literal(setChars[2])}))",
(false, true) => $"((((ch = {chExpr}) | 0x20) == {Literal(setChars[1])}) | (ch == {Literal(setChars[2])}))",
(true, true) => $"((((ch = {chExpr}) | 0x20) != {Literal(setChars[1])}) & (ch != {Literal(setChars[2])}))",
(false, true) => $"((((ch = {chExpr}) | 0x{mask:X}) == {Literal((char)(setChars[1] | mask))}) | (ch == {Literal(setChars[2])}))",
(true, true) => $"((((ch = {chExpr}) | 0x{mask:X}) != {Literal((char)(setChars[1] | mask))}) & (ch != {Literal(setChars[2])}))",
};

case 4:
if (((setChars[0] | 0x20) == setChars[1]) &&
((setChars[2] | 0x20) == setChars[3]))
{
additionalDeclarations.Add("char ch;");
return negate ?
$"(((ch = ({chExpr} | 0x20)) != {Literal(setChars[1])}) & (ch != {Literal(setChars[3])}))" :
$"(((ch = ({chExpr} | 0x20)) == {Literal(setChars[1])}) | (ch == {Literal(setChars[3])}))";
}
break;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1048,6 +1048,14 @@ public static bool IsBoundaryWordChar(char ch)
}
}

/// <summary>Determines whether the 'a' and 'b' values differ by only a single bit, setting that bit in 'mask'.</summary>
/// <remarks>This isn't specific to RegexCharClass; it's just a convenient place to host it.</remarks>
public static bool DifferByOneBit(char a, char b, out int mask)
{
mask = a ^ b;
return mask != 0 && (mask & (mask - 1)) == 0;
}

/// <summary>Determines a character's membership in a character class (via the string representation of the class).</summary>
/// <param name="ch">The character.</param>
/// <param name="set">The string representation of the character class.</param>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3615,17 +3615,17 @@ private void EmitMatchCharacterClass(string charClass, bool caseInsensitive)
// it's cheaper and smaller to compare against each than it is to use a lookup table.
if (!invariant && !RegexCharClass.IsNegated(charClass))
{
Span<char> setChars = stackalloc char[4];
Span<char> setChars = stackalloc char[3];
int numChars = RegexCharClass.GetSetChars(charClass, setChars);
if (numChars is 2 or 3)
{
if ((setChars[0] | 0x20) == setChars[1]) // special-case common case of an upper and lowercase ASCII letter combination
if (RegexCharClass.DifferByOneBit(setChars[0], setChars[1], out int mask)) // special-case common case of an upper and lowercase ASCII letter combination
{
// ((ch | 0x20) == setChars[1])
// ((ch | mask) == setChars[1])
Ldloc(tempLocal);
Ldc(0x20);
Ldc(mask);
Or();
Ldc(setChars[1]);
Ldc(setChars[1] | mask);
Ceq();
}
else
Expand All @@ -3651,27 +3651,6 @@ private void EmitMatchCharacterClass(string charClass, bool caseInsensitive)

return;
}
else if (numChars == 4 &&
(setChars[0] | 0x20) == setChars[1] &&
(setChars[2] | 0x20) == setChars[3])
{
// ((ch | 0x20) == setChars[1])
Ldloc(tempLocal);
Ldc(0x20);
Or();
Ldc(setChars[1]);
Ceq();

// ((ch | 0x20) == setChars[3])
Ldloc(tempLocal);
Ldc(0x20);
Or();
Ldc(setChars[3]);
Ceq();

Or();
return;
}
}

using RentedLocalBuilder resultLocal = RentInt32Local();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,11 @@ public static IEnumerable<object[]> Match_MemberData()
yield return (".[abc]", "xYZAbC", RegexOptions.IgnoreCase, 0, 6, true, "ZA");
yield return (".[abc]", "xYzXyZx", RegexOptions.IgnoreCase, 0, 6, false, "");

// Sets containing characters that differ by a bit
yield return ("123[Aa]", "123a", RegexOptions.None, 0, 4, true, "123a");
yield return ("123[0p]", "123p", RegexOptions.None, 0, 4, true, "123p");
yield return ("123[Aa@]", "123@", RegexOptions.None, 0, 4, true, "123@");

// "\D+"
yield return (@"\D+", "12321", RegexOptions.None, 0, 5, false, string.Empty);

Expand Down