Skip to content

Commit 67495bc

Browse files
authored
Lower regex MaxUnrollSize from 16 to 7 (#126092)
1 parent 487cc8c commit 67495bc

2 files changed

Lines changed: 8 additions & 6 deletions

File tree

src/libraries/System.Text.RegularExpressions/gen/RegexGenerator.Emitter.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1606,9 +1606,10 @@ private static void EmitTryMatchAtCurrentPosition(IndentedTextWriter writer, Reg
16061606
// "doneLabel" is simply the final return location from the TryMatchAtCurrentPosition method that will undo any captures and exit, signaling to
16071607
// the calling scan loop that nothing was matched.
16081608

1609-
// Arbitrary limit for unrolling vs creating a loop. We want to balance size in the generated
1610-
// code with other costs, like the (small) overhead of slicing to create the temp span to iterate.
1611-
const int MaxUnrollSize = 16;
1609+
// Limit for unrolling vs creating a loop. Benchmarking shows vectorized operations
1610+
// (e.g. ContainsAnyExcept) beat unrolled scalar checks at counts above ~4-8, so
1611+
// we unroll up to/including this threshold and use a loop with vectorization beyond it.
1612+
const int MaxUnrollSize = 7;
16121613

16131614
RegexOptions options = rm.Options;
16141615
RegexTree regexTree = rm.Tree;

src/libraries/System.Text.RegularExpressions/src/System/Text/RegularExpressions/RegexCompiler.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4585,9 +4585,10 @@ void EmitSingleCharRepeater(RegexNode node, bool emitLengthChecksIfRequired = tr
45854585
return;
45864586
}
45874587

4588-
// Arbitrary limit for unrolling vs creating a loop. We want to balance size in the generated
4589-
// code with other costs, like the (small) overhead of slicing to create the temp span to iterate.
4590-
const int MaxUnrollSize = 16;
4588+
// Limit for unrolling vs creating a loop. Benchmarking shows vectorized operations
4589+
// (e.g. ContainsAnyExcept) beat unrolled scalar checks at counts above ~4-8, so
4590+
// we unroll up to/including this threshold and use a loop with vectorization beyond it.
4591+
const int MaxUnrollSize = 7;
45914592

45924593
if (iterations <= MaxUnrollSize)
45934594
{

0 commit comments

Comments
 (0)