Skip to content

<regex>: Accelerate greedy loops of small or negated empty character classes - #6540

Open
Julian Müller (muellerj3) wants to merge 7 commits into
microsoft:mainfrom
muellerj2:regex-accelerate-greedy-small-class-loops
Open

Julian Müller (muellerj3) wants to merge 7 commits into
microsoft:mainfrom
muellerj2:regex-accelerate-greedy-small-class-loops

Conversation

@muellerj3

@muellerj3 Julian Müller (muellerj3) commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Towards #5971. This special-cases loops over the simplest kinds of character classes: The negated empty character class [^], which matches everything, and a character class that is fully described by a bitmap over the first 256 code points. Thus, this essentially handles the character classes for character type char except for those involving some collation feature. It does not handle all relevant character classes for wchar_t, e.g., those derived from a named character class such as [[:alpha:]] or \s.

[^]* is by far the fastest way to write a loop now, beating even .*, as the matcher can just jump ahead to the end of the input string in constant time. This is why the complexity counter does not have to account for the length of the input consumed by [^]*.

It turns out that handling the negation of the character class outside of the for loop in _Find_first_char_not_in_char_class noticeably improves performance (increasing the speedup from ~9 to ~12 on my machine), so the subtype labels for these character classes also differentiate by negation of the character class in addition to icase and collate mode.

The subtype of a negated class in collate mode is missing because it can't happen currently: A loop over a negated class is never marked as simple in _Calculate_loop_simplicity() in collate mode because we haven't fully settled the semantics of such classes yet. But it feels like a strange wart that this subtype is missing, so maybe we should commit to specific semantics in time for 14.53. I will open an issue about this over the next few days.

_Rx_translate_identity is essentially a copy of std::identity. But std::identity is only available starting from C++20, while <regex> is supported in all standard modes. Feel free to change this if you prefer another solution.

With this PR, we would have essentially run out of labels for special loop cases. Since it's certain we need more to speed up character classes for type wchar_t, I doubled them by reserving another bit. This doesn't break ABI because there hasn't been an ABI-stable release yet that includes these subtypes.

(I wonder if there is a better solution than bool as a template parameter of _Find_first_char_not_in_char_class(). It feels a bit strange that true means negated and not that this means the class is positive/non-negated, but this is consistent with some other pieces of code in <regex>. I considered using an enum class with underlying type bool instead, but then I wondered if this is worth it for bool. I also remember that some compilers, Clang included, don't handle such types correctly in some corner cases.)

Benchmarks

regex_match
benchmark before [ns] after [ns] speedup
bm_match_sequence_of_as/"a*" ecma/1000 164.969 160.435 1.03
bm_match_sequence_of_as/"a*" ecma/10000 449.916 464.914 0.97
bm_match_sequence_of_as/"a*" ecma/100000 3417.97 3299.39 1.04
bm_match_sequence_of_as/"a*" posix longest/100 1143.97 1025.39 1.12
bm_match_sequence_of_as/"a*" posix longest/200 2267.02 1883.37 1.20
bm_match_sequence_of_as/"a*" posix longest/400 4080.63 3683.04 1.11
bm_match_sequence_of_as/"a*" collate/1000 163.225 156.948 1.04
bm_match_sequence_of_as/"a*" collate/10000 449.916 470.843 0.96
bm_match_sequence_of_as/"a*" collate/100000 3369.15 3369.15 1.00
bm_match_sequence_of_as/"A*" icase/1000 3278.46 3529.57 0.93
bm_match_sequence_of_as/"A*" icase/10000 31389.5 35295.1 0.89
bm_match_sequence_of_as/"A*" icase/100000 307665 334762 0.92
bm_match_sequence_of_as/"a*?"/100 2085.66 1949.64 1.07
bm_match_sequence_of_as/"a*?"/200 4171.31 3770.22 1.11
bm_match_sequence_of_as/"a*?"/400 7847.38 6975.45 1.12
bm_match_sequence_of_as/"(?:a)*"/1000 163.923 163.923 1.00
bm_match_sequence_of_as/"(?:a)*"/10000 497.349 455.148 1.09
bm_match_sequence_of_as/"(?:a)*"/100000 3299.39 3299.39 1.00
bm_match_sequence_of_as/"(?:aa)*"/1000 892.857 906.808 0.98
bm_match_sequence_of_as/"(?:aa)*"/10000 7672.99 7672.99 1.00
bm_match_sequence_of_as/"(?:aa)*"/100000 76729.9 73242.2 1.05
bm_match_sequence_of_as/"(?:aa)*" collate/1000 878.906 924.247 0.95
bm_match_sequence_of_as/"(?:aa)*" collate/10000 7672.99 7672.99 1.00
bm_match_sequence_of_as/"(?:aa)*" collate/100000 76729.9 75334.8 1.02
bm_match_sequence_of_as/"(?:Aa)*" icase/1000 3515.62 3599.32 0.98
bm_match_sequence_of_as/"(?:Aa)*" icase/10000 35295.1 36062.4 0.98
bm_match_sequence_of_as/"(?:Aa)*" icase/100000 352926 344905 1.02
bm_match_sequence_of_as/"(a)*"/100 2786.7 2699.5 1.03
bm_match_sequence_of_as/"(a)*"/200 5000 5000 1.00
bm_match_sequence_of_as/"(a)*"/400 10009.8 9835.34 1.02
bm_match_sequence_of_as/"(a)*?"/100 3529.58 3222.66 1.10
bm_match_sequence_of_as/"(a)*?"/200 6556.92 6417.41 1.02
bm_match_sequence_of_as/"(a)*?"/400 13113.8 12241.9 1.07
bm_match_sequence_of_as/"(?:b|a)*"/100 3222.66 3138.95 1.03
bm_match_sequence_of_as/"(?:b|a)*"/200 6975.45 6417.41 1.09
bm_match_sequence_of_as/"(?:b|a)*"/400 13811.3 12276.8 1.12
bm_match_sequence_of_as/"(b|a)*"/100 6417.41 5719.87 1.12
bm_match_sequence_of_as/"(b|a)*"/200 13113.8 11997.8 1.09
bm_match_sequence_of_as/"(b|a)*"/400 27791.2 52315.8 0.53
bm_match_sequence_of_as/"(a)(?:b|a)*"/100 3369.15 3149.42 1.07
bm_match_sequence_of_as/"(a)(?:b|a)*"/200 6801.06 6277.9 1.08
bm_match_sequence_of_as/"(a)(?:b|a)*"/400 13497.4 12555.8 1.07
bm_match_sequence_of_as/"(a)(b|a)*"/100 6138.39 5998.88 1.02
bm_match_sequence_of_as/"(a)(b|a)*"/200 13392.9 14753 0.91
bm_match_sequence_of_as/"(a)(b|a)*"/400 26994.9 51562.5 0.52
bm_match_sequence_of_as/"(a)(?:b|a)*c"/100 4003.91 3836.5 1.04
bm_match_sequence_of_as/"(a)(?:b|a)*c"/200 8579.76 8544.92 1.00
bm_match_sequence_of_as/"(a)(?:b|a)*c"/400 18031.6 15694.8 1.15
bm_match_sequence_of_as/".*" ecma/1000 239.955 240.653 1.00
bm_match_sequence_of_as/".*" ecma/10000 952.148 962.611 0.99
bm_match_sequence_of_as/".*" ecma/100000 8370.54 8370.5 1.00
bm_match_sequence_of_as/".*" posix any/1000 150.67 153.46 0.98
bm_match_sequence_of_as/".*" posix any/10000 380.859 393.415 0.97
bm_match_sequence_of_as/".*" posix any/100000 2727.4 2699.5 1.01
bm_match_sequence_of_as/".*" posix longest/100 1116.07 1025.39 1.09
bm_match_sequence_of_as/".*" posix longest/200 2134.49 1883.37 1.13
bm_match_sequence_of_as/".*" posix longest/400 4143.41 3690 1.12
bm_match_sequence_of_as/"[^]*"/1000 10253.9 131.138 78.19
bm_match_sequence_of_as/"[^]*"/10000 95214.8 131.836 722.22
bm_match_sequence_of_as/"[^]*"/100000 1024930 131.836 7774.28
bm_match_sequence_of_as/"[ab]*"/1000 10463.1 962.611 10.87
bm_match_sequence_of_as/"[ab]*"/10000 100442 8021.76 12.52
bm_match_sequence_of_as/"[ab]*"/100000 976562 81961.5 11.91
bm_match_sequence_of_as/"[^bc]*"/1000 10672.4 941.685 11.33
bm_match_sequence_of_as/"[^bc]*"/10000 104980 8021.76 13.09
bm_match_sequence_of_as/"[^bc]*"/100000 1049800 81961.5 12.81
bm_match_sequence_of_as/R"(\w*)"/1000 9765.62 941.685 10.37
bm_match_sequence_of_as/R"(\w*)"/10000 102539 7951.97 12.89
bm_match_sequence_of_as/R"(\w*)"/100000 1000980 80217.6 12.48
bm_match_sequence_of_as/"[AB]*" icase/1000 12834.8 3529.57 3.64
bm_match_sequence_of_as/"[AB]*" icase/10000 126953 35295.1 3.60
bm_match_sequence_of_as/"[AB]*" icase/100000 1283480 345346 3.72
bm_match_sequence_of_as/"[ab]*" collate/1000 10253.9 962.611 10.65
bm_match_sequence_of_as/"[ab]*" collate/10000 100442 8370.5 12.00
bm_match_sequence_of_as/"[ab]*" collate/100000 1049800 81961.5 12.81
bm_match_sequence_of_9a1b/"(?:a*b)*"/100 5000 5000 1.00
bm_match_sequence_of_9a1b/"(?:a*b)*"/200 10044.6 9416.81 1.07
bm_match_sequence_of_9a1b/"(?:a*b)*"/400 19949.5 29820 0.67

@muellerj3
Julian Müller (muellerj3) requested a review from a team as a code owner November 23, 2026 09:40
Copilot AI balanced review requested due to automatic review settings November 23, 2026 09:40
@github-project-automation github-project-automation Bot moved this to Copilot Review in STL Code Reviews Nov 23, 2026
@azure-pipelines

This comment was marked as resolved.

This comment was marked as resolved.

Copilot AI balanced review requested due to automatic review settings November 23, 2026 10:03

This comment was marked as resolved.

@muellerj3

This comment was marked as resolved.

…he code points of a and b when narrowing to unsigned char
@StephanTLavavej Stephan T. Lavavej (StephanTLavavej) added performance Must go faster regex meow is a substring of homeowner labels Nov 24, 2026
Copilot AI balanced review requested due to automatic review settings November 24, 2026 21:52

This comment was marked as resolved.

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Copilot AI balanced review requested due to automatic review settings November 24, 2026 22:02

This comment was marked as resolved.

Copilot AI balanced review requested due to automatic review settings November 24, 2026 22:20

Copilot AI 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.

🟢 Approval recommended

There are no active review issues for the character-class loop optimizations.

No open issues 🎉

Review effort: Balanced

@StephanTLavavej Stephan T. Lavavej (StephanTLavavej) moved this from Copilot Review to Maintainer Review in STL Code Reviews Nov 24, 2026
@muellerj3

Copy link
Copy Markdown
Contributor Author

I addressed the Copilot remark on missing test coverage. I also deliberately changed the unicode characters with code points >= 256 to be matched to character class [ab]: If the code points are narrowed to unsigned char, they will equal those of a and b and thus trigger a wrong match if the conversion is handled in the wrong way.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

performance Must go faster regex meow is a substring of homeowner

Projects

Status: Maintainer Review

Development

Successfully merging this pull request may close these issues.

3 participants