Skip to content

Enable case-sensitive LeadingStrings with frequency-based heuristic - #124736

Merged
danmoseley merged 12 commits into
dotnet:mainfrom
danmoseley:regex-redux/leading-strings-frequency
Feb 25, 2026
Merged

Enable case-sensitive LeadingStrings with frequency-based heuristic#124736
danmoseley merged 12 commits into
dotnet:mainfrom
danmoseley:regex-redux/leading-strings-frequency

Conversation

@danmoseley

@danmoseleydanmoseley commented Feb 23, 2026

Copy link
Copy Markdown
Contributor

I thought it would be interesting to see whether AI could take another look at the commented out search strategy originally introduced by @stephentoub in #98791 to see whether we can enable it and keep the wins without the regressions that caused it to be commented out.

AI tried various experiments, and got to a dead end. I recalled the frequency table approach that Ripgrep uses (credit to @BurntSushi). Turns out that fixes the regressions entirely. This means our engine now has assumptions built in about char frequencies in ASCII (only) text. That's an approach that's been proven in ripgrep, one of the fastest engines, for 10 years, and it turns out to work OK for regex-redux as well because a, c, g, t are relatively high frequency in English anyway. Code unchanged if pattern has anything other than ASCII (see benchmark results below).

This gives us a nice win on regex-redux, a few other wins in existing tests, and no regressions.

Note: a char frequency table already existed in RegexPrefixAnalyzer.cs for ranking which fixed-distance character sets are most selective. Our new table serves a different purpose: deciding whether to use LeadingStrings vs FixedDistanceSets at all. The two are complementary.

====

When a regex has multiple alternation prefixes (e.g. a|b|c|...), this change decides whether to use SearchValues<string> (Teddy/Aho-Corasick) or fall through to FixedDistanceSets (IndexOfAny) based on the frequency of the starting characters.

High-frequency starters (common letters like lowercase vowels) benefit from multi-string search; low-frequency starters (uppercase, digits, rare consonants) are already excellent IndexOfAny filters. Non-ASCII starters bail out (no frequency data), preserving baseline behavior.

Benchmark results (444 benchmarks, BDN A/B with --statisticalTest 3ms)

BenchmarkBaselinePRRatioVerdict
RegexRedux_1 (Compiled)25.77ms14.27ms1.81x fasterFaster
Leipzig Tom.*river (Compiled)6.13ms1.87ms3.28x fasterFaster
RegexRedux_5 (Compiled)2.83ms2.35ms1.20x fasterFaster
Sherlock, BinaryData, BoostDocs, Mariomkas, SliceSlice------Same
LeadingStrings_NonAscii (all variants)------Same
LeadingStrings_BinaryData (all variants)------Same

Leipzig win is because the pattern is Tom.{10,25}river|river.{10,25}Tom so there is a short prefix that is common in the text; with this change it notices r is common and T fairly common in English text, so it switches to SearchValues which looks for Tom and river simultaneously, causing far fewer false starts.

regex-redux win is because it was previously looking for short, very common prefixes naively, and now (specifically because the pattern chars are common) it changed to use SearchValues (ie Aho-Corasick/Teddy) to search for the longer strings simultaneously.

No regressions detected. All MannWhitney tests report Same for non-improved benchmarks.

Key design decisions

  • Frequency table: First 128 entries of Rust's BYTE_FREQUENCIES from @BurntSushi's aho-corasick crate
  • Threshold: Average rank >= 200 triggers LeadingStrings; below 200 falls through to FixedDistanceSets
  • Non-ASCII: Returns false (no frequency data), so the heuristic does not engage and behavior is unchanged

Companion benchmarks: dotnet/performance#5126

New benchmark results (not yet in dotnet/performance, won't be picked up by PR bot)

These benchmarks are from the companion PR dotnet/performance#5126.

BenchmarkDotNet v0.16.0-custom.20260127.101, Windows 11 (10.0.26100.7840/24H2/2024Update/HudsonValley)
Intel Core i9-14900K 3.20GHz, 1 CPU, 32 logical and 24 physical cores
BenchmarkOptionsBaselinePRRatioMannWhitney(3ms)
LeadingStrings_BinaryDataNone4,483 us4,365 us0.97Same
LeadingStrings_BinaryDataCompiled2,188 us2,184 us1.00Same
LeadingStrings_BinaryDataNonBacktracking3,734 us3,725 us1.00Same
LeadingStrings_NonAscii CountNone913 us956 us1.05Same
LeadingStrings_NonAscii CountCompiled244 us243 us1.00Same
LeadingStrings_NonAscii CountIgnoreCaseNone1,758 us1,714 us0.98Same
LeadingStrings_NonAscii CountIgnoreCaseCompiled258 us250 us0.97Same
LeadingStrings_NonAscii CountNonBacktracking392 us398 us1.02Same
LeadingStrings_NonAscii CountIgnoreCaseNonBacktracking409 us431 us1.05Same

Binary didn't regress even though it's ASCII with non English frequencies because the pattern has chars that are not particularly common in English, so it uses the old codepath. It's hard to hypothesize about what one might search for in a binary file; searching for a bunch of leading lower case ASCII chars might regress somewhat. We've never particularly tested on binary before, and I don't recall seeing any bugs mentioning binary, so I don't think this is particularly interesting.

NonASCII didn't regress since as previously mentioned, the non ASCII leading chars in the pattern (presumably likely for any searching of non ASCII text) causes it to choose the existing codepath.

All MannWhitney tests report Same -- no regressions on binary or non-ASCII input.

CopilotAI review requested due to automatic review settings February 23, 2026 02:08
@danmoseley

Copy link
Copy Markdown
ContributorAuthor

@MihuBot benchmark Regex

CopilotAI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR enables case-sensitive multi-string prefix optimization for regex patterns by introducing a frequency-based heuristic to decide between SearchValues<string> (Teddy/Aho-Corasick) and IndexOfAny with character sets. Previously, case-sensitive prefixes were disabled due to regressions in patterns with low-frequency starting characters (e.g., uppercase letters, digits). The new heuristic uses empirical byte frequency data from Rust's aho-corasick crate to determine if starting characters are common enough in typical text to warrant multi-string search, or rare enough that IndexOfAny remains a better filter.

Changes:

  • Uncommented and enabled case-sensitive prefix optimization with a frequency guard
  • Added HasHighFrequencyStartingChars method to evaluate whether prefix starting characters are high-frequency (threshold >= 200)
  • Added AsciiCharFrequencyRank table containing the first 128 entries from BurntSushi's BYTE_FREQUENCIES data

@danmoseley
danmoseleyforce-pushed the regex-redux/leading-strings-frequency branch from fc2a7e2 to eb39721CompareFebruary 23, 2026 02:31
@MihuBot

Copy link
Copy Markdown

@danmoseley
danmoseleyforce-pushed the regex-redux/leading-strings-frequency branch from eb39721 to e877181CompareFebruary 23, 2026 04:19
CopilotAI review requested due to automatic review settings February 23, 2026 04:19

CopilotAI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 21 out of 9433 changed files in this pull request and generated no new comments.

@danmoseley
danmoseleyforce-pushed the regex-redux/leading-strings-frequency branch 2 times, most recently from da12aa4 to 3744268CompareFebruary 23, 2026 04:24
CopilotAI review requested due to automatic review settings February 23, 2026 04:24

CopilotAI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.

Comments suppressed due to low confidence (4)

src/libraries/System.Text.RegularExpressions/src/System/Text/RegularExpressions/RegexFindOptimizations.cs:232

  • leadingStringsFrequency uses -1 as the sentinel for "not computed / not applicable", but the subsequent checks use > 0. A valid computed frequency can be 0 (e.g., prefixes starting with '\x00' or other chars with 0 frequency in the table), which would incorrectly skip the LeadingStrings-vs-set comparison. Consider tracking availability via caseSensitivePrefixes is not null and checking leadingStringsFrequency >= 0 (or using a separate bool) so computed-0 still participates in the heuristic.
 if (leadingStringsFrequency > 0)
{
bool preferLeadingStrings = true;

src/libraries/System.Text.RegularExpressions/src/System/Text/RegularExpressions/RegexFindOptimizations.cs:307

  • Same sentinel issue as above: the fallback if (leadingStringsFrequency > 0) will skip using computed case-sensitive prefixes if the computed value is 0, potentially leaving FindMode as NoSearch when no other strategy is selected. Use an availability check like caseSensitivePrefixes is not null && leadingStringsFrequency >= 0 (or a dedicated flag) instead of > 0.
 // If we have case-sensitive leading prefixes and nothing else was selected, use them.
if (leadingStringsFrequency > 0)
{
LeadingPrefixes = caseSensitivePrefixes!;

src/libraries/System.Text.RegularExpressions/src/System/Text/RegularExpressions/RegexPrefixAnalyzer.cs:1512

  • The PR description says this change is based on Rust aho-corasick BYTE_FREQUENCIES ranks and a rank threshold (>= 200), but the implementation is using the existing RegexPrefixAnalyzer.Frequency table of percentage occurrences (generated from runtime/Gutenberg text) and compares summed percentages. Either the description needs updating to reflect the actual heuristic/table used, or the code needs to align with the described rank-based approach.
 /// <summary>Percent occurrences in source text (100 * char count / total count).</summary>
internal static ReadOnlySpan<float> Frequency =>
[
0.000f /* '\x00' */, 0.000f /* '\x01' */, 0.000f /* '\x02' */, 0.000f /* '\x03' */, 0.000f /* '\x04' */, 0.000f /* '\x05' */, 0.000f /* '\x06' */, 0.000f /* '\x07' */,

src/libraries/System.Text.RegularExpressions/src/System/Text/RegularExpressions/RegexFindOptimizations.cs:255

  • This PR introduces new behavior for RegexOptions.Compiled / NonBacktracking where case-sensitive LeadingStrings may now be selected based on a frequency heuristic. There are existing RegexFindOptimizationsTests, but none appear to cover this new decision logic in compiled/NB modes; adding a few targeted test cases (e.g., where compiled should choose LeadingStrings vs LeadingSet depending on starter frequency, and a non-ASCII starter that must not engage the heuristic) would help prevent future regressions.
 // Compute case-sensitive leading prefixes, but don't commit yet. We'll compare
// their starting-char frequency against the best FixedDistanceSet below to decide
// which strategy to use.
caseSensitivePrefixes = RegexPrefixAnalyzer.FindPrefixes(root, ignoreCase: false) is { Length: > 1 } csp ? csp : null;
leadingStringsFrequency = caseSensitivePrefixes is not null ? SumStartingCharFrequencies(caseSensitivePrefixes) : -1;
}
// Build up a list of all of the sets that are a fixed distance from the start of the expression.
List<FixedDistanceSet>? fixedDistanceSets = RegexPrefixAnalyzer.FindFixedDistanceSets(root, thorough: !interpreter);
Debug.Assert(fixedDistanceSets is null || fixedDistanceSets.Count != 0);
// See if we can make a string of at least two characters long out of those sets. We should have already caught
// one at the beginning of the pattern, but there may be one hiding at a non-zero fixed distance into the pattern.
if (fixedDistanceSets is not null &&
FindFixedDistanceString(fixedDistanceSets) is (string String, int Distance) bestFixedDistanceString)
{
FindMode = FindNextStartingPositionMode.FixedDistanceString_LeftToRight;
FixedDistanceLiteral = ('\0', bestFixedDistanceString.String, bestFixedDistanceString.Distance);
return;
}
// As a backup, see if we can find a literal after a leading atomic loop. That might be better than whatever sets we find, so
// we want to know whether we have one in our pocket before deciding whether to use a leading set (we'll prefer a leading
// set if it's something for which we can search efficiently).
(RegexNode LoopNode, (char Char, string? String, StringComparison StringComparison, char[]? Chars) Literal)? literalAfterLoop = RegexPrefixAnalyzer.FindLiteralFollowingLeadingLoop(root);
// If we got such sets, we'll likely use them. However, if the best of them is something that doesn't support an efficient
// search and we did successfully find a literal after an atomic loop we could search instead, we prefer the efficient search.
// For example, if we have a negated set, we will still prefer the literal-after-an-atomic-loop because negated sets typically
// contain _many_ characters (e.g. [^a] is everything but 'a') and are thus more likely to very quickly match, which means any
// vectorization employed is less likely to kick in and be worth the startup overhead.
if (fixedDistanceSets is not null)
{
// Sort the sets by "quality", such that whatever set is first is the one deemed most efficient to use.
// In some searches, we may use multiple sets, so we want the subsequent ones to also be the efficiency runners-up.
RegexPrefixAnalyzer.SortFixedDistanceSetsByQuality(fixedDistanceSets);
// If we have case-sensitive leading prefixes, compare the frequency of their starting characters
// against the best fixed-distance set's characters. If the best set isn't more selective than the
// starting chars (i.e. its frequency is at least as high), prefer LeadingStrings (SearchValues)
// which can match full multi-character prefixes simultaneously. Also prefer LeadingStrings when
// the best set is negated or range-based (no Chars), since those are weak filters.
if (leadingStringsFrequency > 0)
{
bool preferLeadingStrings = true;
if (fixedDistanceSets[0].Chars is { } bestSetChars &&
!fixedDistanceSets[0].Negated)
{
ReadOnlySpan<float> frequency = RegexPrefixAnalyzer.Frequency;
Debug.Assert(frequency.Length == 128);
float bestSetFrequency = 0;
foreach (char c in bestSetChars)
{
bestSetFrequency += c < frequency.Length ? frequency[c] : 0;
}
preferLeadingStrings = bestSetFrequency >= leadingStringsFrequency;
}
if (preferLeadingStrings)
{
LeadingPrefixes = caseSensitivePrefixes!;
FindMode = FindNextStartingPositionMode.LeadingStrings_LeftToRight;
#if SYSTEM_TEXT_REGULAREXPRESSIONS
LeadingStrings = SearchValues.Create(LeadingPrefixes, StringComparison.Ordinal);
#endif
return;
}

@danmoseley

Copy link
Copy Markdown
ContributorAuthor

@MihuBot benchmark Regex

@danmoseley

Copy link
Copy Markdown
ContributorAuthor

I also ran regex tests locally to verify it's still good, so I think this ready for final (?) review.

@danmoseley

danmoseley commented Feb 23, 2026

Copy link
Copy Markdown
ContributorAuthor

In my local runs, i get these wins. all others unchanged, no regressions

SuiteBenchmarkOptionsMean (Main)Mean (PR)RatioAlloc (Main)Alloc (PR)Alloc Ratio
LeipzigTom.{10,25}river|river.{10,25}TomCompiled6,421.7 μs1,169.9 μs0.1851 B10 B0.20
LeipzigTom.{10,25}river|river.{10,25}TomNonBacktracking7,183.8 μs1,442.7 μs0.2016,244 B4,428 B0.27
CommonSplitWordsCompiled2,517.20 ns1,202.89 ns0.487,432 B7,432 B1.00
CommonMatchesWordsCompiled2,551.48 ns1,240.66 ns0.493,448 B3,448 B1.00
CommonReplaceWordsCompiled2,423.42 ns1,233.94 ns0.516,848 B6,848 B1.00
CommonMatchWordCompiled126.98 ns64.76 ns0.51208 B208 B1.00
RegexRedux_1RegexRedux_1Compiled37.44 ms20.46 ms0.553.39 MB3.41 MB1.01
RegexRedux_5RegexRedux_5Compiled4.852 ms3.558 ms0.733.21 MB3.21 MB1.00
CommonReplaceWordsIgnoreCase, Compiled1,795.05 ns1,488.70 ns0.836,848 B6,848 B1.00
CommonOneNodeBacktrackingCompiled79.92 ns70.05 ns0.88--NA

@danmoseley

danmoseley commented Feb 23, 2026

Copy link
Copy Markdown
ContributorAuthor

How would this affect C# (and F#) on the leaderboard at https://programming-language-benchmarks.vercel.app/problem/regex-redux? ( the original does not have a [GeneratedRegex] entry yet.)

Currently the top .NET entry is 6th (AOT+generated). I didn't measure AOT, but assuming the ratio is the same as for jit, we'd get to 3rd on the list.

Rust would still be over 2x faster and the main reason is very likely because its regex engine operates on UTF-8 and ours uses UTF-16 so there's just twice the bytes to process, meaning SIMD has to do more chunks,. ..

@MihuBot

Copy link
Copy Markdown

@danmoseley

Copy link
Copy Markdown
ContributorAuthor

OK fishy mihubot was good before, but second run now doesn't match my local good results. Let me see

@MihuBot

Copy link
Copy Markdown

@danmoseley

Copy link
Copy Markdown
ContributorAuthor

@MihuBot benchmark Regex

@danmoseley

Copy link
Copy Markdown
ContributorAuthor

(I think we didn't get mihubot on x64 on the last commit, I'll do this to compare with ARM64

@danmoseley

danmoseley commented Feb 24, 2026

Copy link
Copy Markdown
ContributorAuthor

Below is the AI analysis of the code gen issue. which is correct as far as I can tell.

Question is whether we should explore adding the proposed "fix" to this PR. It seems like this code gen diff should itself be an improvement, albeit inadvertent. These are not patterns from the benchmarks though, so I'll measure one locally.

I spot checked a bunch of diffs and they all have this pattern, btw, so seems like just this one explanation.

===

For \b(in)\b with IgnoreCase | Singleline, the regex tree (after lowering) is:

Capture(0)
Concatenate
Boundary ← \b
Capture(1) ← the (in) group
Concatenate
Set([Ii]) ← 'i' lowered to case-insensitive set
Set([Nn]) ← 'n' lowered to case-insensitive set
Boundary ← \b

There are two pre-existing gaps that cause this pattern to fall through:

Gap 1: FindPrefixOrdinalCaseInsensitive can't see through Capture groups (line 163)

TryGetOrdinalCaseInsensitiveString (RegexNode.cs:2957) iterates the direct children of the Concatenate. It handles One, Multi, Set, Empty, and zero-width assertions — but when it hits Capture(1), it falls into the else branch (line 3019) and breaks. It never sees the "in" inside the capture. Result: returns null.

Gap 2: FindPrefixes(ignoreCase: true) returns only 1 prefix (line 175)

FindPrefixes can navigate through Capture nodes (line 84-87). It successfully finds the prefix "in" — but returns it as a single-element array ["in"]. The check at line 175 requires { Length: > 1 } (more than 1 string), so it fails.

The > 1 check was designed to exclude single-prefix cases that "should be" handled by FindPrefixOrdinalCaseInsensitive above — but since that also fails (Gap 1), the pattern falls through to FixedDistanceSets entirely.

How the PR "rescues" it

The PR's new code (line 218-220) calls FindPrefixes(root, ignoreCase: false), which case-expands the sets into 4 ordinal variants: ["IN", "iN", "In", "in"]. This passes { Length: > 1 } and uses SearchValues.Create(..., StringComparison.Ordinal).

Stephen's point

This works but is suboptimal — ideally Gap 1 should be fixed so TryGetOrdinalCaseInsensitiveString descends through Capture nodes (same as it already handles zero-width assertions). Then the pattern would use LeadingString_OrdinalIgnoreCase_LeftToRight with a single "in" string and OrdinalIgnoreCase comparison, which is cleaner and likely faster than searching for 4 ordinal variants.

The fix would be a one-line addition around RegexNode.cs:3014:

elseif(child.Kindis RegexNodeKind.Capture){// Descend into capture group to find the string inside// (similar to how FindPrefixesCore handles Capture)}

...though that would require restructuring since the method iterates children flat rather than recursing.

@danmoseley

danmoseley commented Feb 24, 2026

Copy link
Copy Markdown
ContributorAuthor

Looks like for this example pattern it's same/an improvement (of course depends on the particular text). I guess searching for in|In|iN|IN is faster than searching for n and each time backing up for i. In the text I used, half the n weren't preceded by i

BenchmarkBaseline (main)PRRatioVerdict
\b(in)\b IgnoreCase4,557 ns4,241 ns0.93~7% faster
\bin\b IgnoreCase (no capture)2,649 ns2,731 ns1.03Same
\b(from).+(to)\b.+ IgnoreCase79.9 ns79.7 ns1.00Same

I think I need guidance on whether I should pursue fixing this in this PR. Either way, we'd have a diff: it would just be a code improvement to code not changed in this PR.

-- Dan

@danmoseley

Copy link
Copy Markdown
ContributorAuthor

@MihuBot benchmark Regex https://github.com/MihaZupan/performance/tree/compiled-regex-only -medium -arm

@MihuBot

Copy link
Copy Markdown

@MihuBot

Copy link
Copy Markdown

@stephentoub

Copy link
Copy Markdown
Member

I think I need guidance on whether I should pursue fixing this in this PR.

No, we can do it separately

@danmoseley

danmoseley commented Feb 24, 2026

Copy link
Copy Markdown
ContributorAuthor

OK, AI analysis of the mihubot numbers on the final commit, comparing the two architectures:

MihuBot Benchmark Summary — Both Architectures on Latest Commit (21a68d75)

  • x64: AMD EPYC 9V74 (gist)
  • ARM64: Neoverse-N2 (gist)

All results below are Compiled mode. Only benchmarks with significant change (ratio ≤ 0.95) on at least one architecture are shown. Everything else is ~1.00. No regressions on either
architecture.

Benchmarkx64 RatioARM64 Ratiox64 SpeedupARM64 Speedup
Leipzig Tom.{10,25}river|river.{10,25}Tom0.180.48~5.6x~2.1x
Common ReplaceWords0.460.76~2.2x~1.3x
Common SplitWords0.470.77~2.1x~1.3x
Common MatchWord0.510.71~2.0x~1.4x
Common MatchesWords0.520.76~1.9x~1.3x
RegexRedux_10.550.66~1.8x~1.5x
RegexRedux_50.740.94~1.4x~1.1x
Common ReplaceWords (IgnoreCase)0.801.00~1.3x

x64 (AVX2, 256-bit) consistently shows ~1.5–2x larger improvements than ARM64 (NEON, 128-bit), as expected for the Teddy multi-string SIMD search algorithm. The Leipzig pattern sees the biggest gap: 5.6x on x64 vs 2.1x on ARM64.

@danmoseley

Copy link
Copy Markdown
ContributorAuthor

I think we're in a good shape now and everything's addressed? I can follow up on the code diff issue mentioned when this is merged.

@stephentoubstephentoub left a comment

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.

Thanks.

@danmoseley

Copy link
Copy Markdown
ContributorAuthor

@MihuBot benchmark Regex https://github.com/MihaZupan/performance/tree/compiled-regex-only

@danmoseley

Copy link
Copy Markdown
ContributorAuthor

I'll merge once bot confirms there are no regressions. Which I already confirmed locally anyway: zero changes since last commit.

@MihuBot

Copy link
Copy Markdown

@danmoseley

Copy link
Copy Markdown
ContributorAuthor

Mihubot shows 4 regressions, but running (again) locally shows they're noise of some sort --

BenchmarkOptionsPR-latest (ns)Negated-fix (ns)Ratio
MatchesSetCompiled25,29425,2051.00
MatchesSetIgnoreCase, Compiled25,33224,6350.97
MatchesWordsCompiled1,0391,0220.98
MatchesWordsIgnoreCase, Compiled1,1371,1361.00

above against 2nd last commit vs last commit.

BenchmarkOptionsMain (ns)PR latest (ns)Ratio
MatchesSetCompiled24,55825,0401.02
MatchesSetIgnoreCase, Compiled25,04524,8960.99
MatchesWordsCompiled1,7799600.54
MatchesWordsIgnoreCase, Compiled1,1611,1170.96

this is base vs latest commit. Mihubot is noise. good to merge.

@danmoseley
danmoseley enabled auto-merge (squash) February 24, 2026 23:56
@danmoseley
danmoseley merged commit b613202 into dotnet:mainFeb 25, 2026
88 of 90 checks passed
@danmoseley
danmoseley deleted the regex-redux/leading-strings-frequency branch February 26, 2026 03:19
iremyux pushed a commit to iremyux/dotnet-runtime that referenced this pull request Mar 2, 2026
…otnet#124736)
I thought it would be interesting to see whether AI could take another
look at the [commented out search
strategy](https://github.com/dotnet/runtime/blob/99b76018b6e4/src/libraries/System.Text.RegularExpressions/src/System/Text/RegularExpressions/RegexFindOptimizations.cs#L152-L161)
originally introduced by @stephentoub in dotnet#98791 to see whether we can
enable it and keep the wins without the regressions that caused it to be
commented out.
AI tried various experiments, and got to a dead end. I recalled the
frequency table approach that Ripgrep uses (credit to @BurntSushi).
Turns out that fixes the regressions entirely. This means our engine now
has assumptions built in about char frequencies in ASCII (only) text.
That's an approach that's been proven in ripgrep, one of the fastest
engines, for 10 years, and it turns out to work OK for regex-redux as
well because a, c, g, t are relatively high frequency in English anyway.
Code unchanged if pattern has anything other than ASCII (see benchmark
results below).
This gives us a nice win on regex-redux, a few other wins in existing
tests, and no regressions.
Note: a char frequency table already existed in `RegexPrefixAnalyzer.cs`
for ranking which fixed-distance character sets are most selective. Our
new table serves a different purpose: deciding whether to use
`LeadingStrings` vs `FixedDistanceSets` at all. The two are
complementary.
====
When a regex has multiple alternation prefixes (e.g. `a|b|c|...`), this
change decides whether to use `SearchValues<string>`
(Teddy/Aho-Corasick) or fall through to `FixedDistanceSets`
(`IndexOfAny`) based on the frequency of the starting characters.
High-frequency starters (common letters like lowercase vowels) benefit
from multi-string search; low-frequency starters (uppercase, digits,
rare consonants) are already excellent `IndexOfAny` filters. Non-ASCII
starters bail out (no frequency data), preserving baseline behavior.
## Benchmark results (444 benchmarks, BDN A/B with --statisticalTest
3ms)
| Benchmark | Baseline | PR | Ratio | Verdict |
|-----------|----------|-----|-------|---------|
| RegexRedux_1 (Compiled) | 25.77ms | 14.27ms | **1.81x faster** |
Faster |
| Leipzig Tom.*river (Compiled) | 6.13ms | 1.87ms | **3.28x faster** |
Faster |
| RegexRedux_5 (Compiled) | 2.83ms | 2.35ms | **1.20x faster** | Faster
|
| Sherlock, BinaryData, BoostDocs, Mariomkas, SliceSlice | -- | -- | --
| Same |
| LeadingStrings_NonAscii (all variants) | -- | -- | -- | Same |
| LeadingStrings_BinaryData (all variants) | -- | -- | -- | Same |
Leipzig win is because the pattern is
`Tom.{10,25}river|river.{10,25}Tom` so there is a short prefix that is
common in the text; with this change it notices `r` is common and `T`
fairly common in English text, so it switches to `SearchValues` which
looks for `Tom` and `river` simultaneously, causing far fewer false
starts.
regex-redux win is because it was previously looking for short, very
common prefixes naively, and now (specifically because the pattern chars
are common) it changed to use `SearchValues` (ie Aho-Corasick/Teddy) to
search for the longer strings simultaneously.
No regressions detected. All MannWhitney tests report Same for
non-improved benchmarks.
## Key design decisions
- **Frequency table**: First 128 entries of Rust's `BYTE_FREQUENCIES`
from @BurntSushi's aho-corasick crate
- **Threshold**: Average rank >= 200 triggers `LeadingStrings`; below
200 falls through to `FixedDistanceSets`
- **Non-ASCII**: Returns false (no frequency data), so the heuristic
does not engage and behavior is unchanged
Companion benchmarks: dotnet/performance#5126
## New benchmark results (not yet in dotnet/performance, won't be picked
up by PR bot)
These benchmarks are from the companion PR
dotnet/performance#5126.
```
BenchmarkDotNet v0.16.0-custom.20260127.101, Windows 11 (10.0.26100.7840/24H2/2024Update/HudsonValley)
Intel Core i9-14900K 3.20GHz, 1 CPU, 32 logical and 24 physical cores
```
| Benchmark | Options | Baseline | PR | Ratio | MannWhitney(3ms) |
|-----------|---------|----------|-----|-------|------------------|
| LeadingStrings_BinaryData | None | 4,483 us | 4,365 us | 0.97 | Same |
| LeadingStrings_BinaryData | Compiled | 2,188 us | 2,184 us | 1.00 |
Same |
| LeadingStrings_BinaryData | NonBacktracking | 3,734 us | 3,725 us |
1.00 | Same |
| LeadingStrings_NonAscii Count | None | 913 us | 956 us | 1.05 | Same |
| LeadingStrings_NonAscii Count | Compiled | 244 us | 243 us | 1.00 |
Same |
| LeadingStrings_NonAscii CountIgnoreCase | None | 1,758 us | 1,714 us |
0.98 | Same |
| LeadingStrings_NonAscii CountIgnoreCase | Compiled | 258 us | 250 us |
0.97 | Same |
| LeadingStrings_NonAscii Count | NonBacktracking | 392 us | 398 us |
1.02 | Same |
| LeadingStrings_NonAscii CountIgnoreCase | NonBacktracking | 409 us |
431 us | 1.05 | Same |
Binary didn't regress even though it's ASCII with non English
frequencies because the pattern has chars that are not particularly
common in English, so it uses the old codepath. It's hard to hypothesize
about what one might search for in a binary file; searching for a bunch
of leading lower case ASCII chars might regress somewhat. We've never
particularly tested on binary before, and I don't recall seeing any bugs
mentioning binary, so I don't think this is particularly interesting.
NonASCII didn't regress since as previously mentioned, the non ASCII
leading chars in the pattern (presumably likely for any searching of non
ASCII text) causes it to choose the existing codepath.
All MannWhitney tests report Same -- no regressions on binary or
non-ASCII input.
---------
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
danmoseley pushed a commit that referenced this pull request Mar 18, 2026
`TryGetOrdinalCaseInsensitiveString` iterates the direct children of a
`Concatenate` node to extract an ordinal case-insensitive prefix string.
It handles `One`, `Multi`, `Set`, `Empty`, and zero-width assertions —
but when it encounters a `Capture` node, it breaks out of the loop,
never examining the content inside.
For a pattern like `\b(in)\b` with `IgnoreCase`, the regex tree after
lowering is:
```
Capture(0) → Concatenate(Boundary, Capture(1) → Concatenate(Set([Ii]), Set([Nn])), Boundary)
```
`FindPrefixOrdinalCaseInsensitive` descends through `Capture(0)` and
calls `TryGetOrdinalCaseInsensitiveString` on the inner `Concatenate`.
At child index 1 (`Capture(1)`), the method breaks — it never finds
`"in"`. The pattern falls through to the slower `FixedDistanceSets` path
(or, after #124736, uses the multi-string ordinal `SearchValues` path
with 4 case variants).
This change unwraps `Capture` nodes transparently and recurses into
nested `Concatenate` children, matching the behavior already present in
`FindPrefixesCore`. This allows `\b(in)\b` with `IgnoreCase` to use the
optimal `LeadingString_OrdinalIgnoreCase_LeftToRight` strategy with a
single `"in"` string and `OrdinalIgnoreCase` comparison.
Follows up on a codegen diff observed in #124736.
<details>
<summary>Source-generated code diff for
<code>[GeneratedRegex(@"\b(in)\b",
RegexOptions.IgnoreCase)]</code></summary>
```diff
private bool TryFindNextPossibleStartingPosition(ReadOnlySpan&lt;char&gt; inputSpan)
{
int pos = base.runtextpos;
// Any possible match is at least 2 characters.
if (pos &lt;= inputSpan.Length - 2)
{
- // The pattern has multiple strings that could begin the match. Search for any of them.
- // If none can be found, there's no match.
- int i = inputSpan.Slice(pos).IndexOfAny(Utilities.s_indexOfAnyStrings_Ordinal_...);
+ // The pattern has the literal "in" ordinal case-insensitive at the beginning of the pattern. Find the next occurrence.
+ // If it can't be found, there's no match.
+ int i = inputSpan.Slice(pos).IndexOfAny(Utilities.s_indexOfString_in_OrdinalIgnoreCase);
if (i &gt;= 0)
{
base.runtextpos = pos + i;
return true;
}
}
base.runtextpos = inputSpan.Length;
return false;
}
```
```diff
-/// Supports searching for the specified strings.
-internal static readonly SearchValues&lt;string&gt; s_indexOfAnyStrings_Ordinal_... =
- SearchValues.Create(["IN", "iN", "In", "in"], StringComparison.Ordinal);
+/// Supports searching for the string "in".
+internal static readonly SearchValues&lt;string&gt; s_indexOfString_in_OrdinalIgnoreCase =
+ SearchValues.Create(["in"], StringComparison.OrdinalIgnoreCase);
```
</details>
---------
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@github-actionsgithub-actionsBot locked and limited conversation to collaborators Mar 28, 2026
Sign up for freeto subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants

@danmoseley@MihuBot@stephentoub@MihaZupan@grbell-ms