`<regex>`: Avoid allocating during matching when regex defines no captures by muellerj2 · Pull Request #5518 · microsoft/STL · GitHub
Skip to content

<regex>: Avoid allocating during matching when regex defines no captures#5518

Merged
StephanTLavavej merged 1 commit into
microsoft:mainfrom
muellerj2:regex-no-allocations-during-matching-of-regexes-without-captures
May 22, 2025
Merged

<regex>: Avoid allocating during matching when regex defines no captures#5518
StephanTLavavej merged 1 commit into
microsoft:mainfrom
muellerj2:regex-no-allocations-during-matching-of-regexes-without-captures

Conversation

@muellerj2

@muellerj2 muellerj2 commented May 17, 2025

Copy link
Copy Markdown

There is no need to allocate the capture group vectors in the matcher state when the only capture group is 0, because capture group 0 is just the whole match and we already know the bounds of the whole match (start is the position where _Matcher2::_Match was called, end is the value of _Cur in the matcher state).

I can't come up with any additional test cases that should be added for this change and I think the existing test coverage should already catch it if I made a mistake here.

Benchmark on my machine:

Benchmark Before After Speedup
bm_lorem_search/"bibe"/2 37667 ns 29157 ns 1.29
bm_lorem_search/"bibe"/3 78474 ns 64174 ns 1.22
bm_lorem_search/"bibe"/4 147524 ns 114397 ns 1.29
bm_lorem_search/"(bibe)"/2 67188 ns 50000 ns 1.34
bm_lorem_search/"(bibe)"/3 146484 ns 144385 ns 1.01
bm_lorem_search/"(bibe)"/4 311440 ns 291561 ns 1.07
bm_lorem_search/"(bibe)+"/2 89979 ns 92072 ns 0.98
bm_lorem_search/"(bibe)+"/3 184168 ns 180303 ns 1.02
bm_lorem_search/"(bibe)+"/4 376989 ns 352926 ns 1.07
bm_lorem_search/"(?:bibe)+"/2 94164 ns 59375 ns 1.59
bm_lorem_search/"(?:bibe)+"/3 191812 ns 109863 ns 1.75
bm_lorem_search/"(?:bibe)+"/4 359869 ns 240626 ns 1.50

@muellerj2 muellerj2 requested a review from a team as a code owner May 17, 2025 17:27
@github-project-automation github-project-automation Bot moved this to Initial Review in STL Code Reviews May 17, 2025
@StephanTLavavej StephanTLavavej added performance Must go faster regex meow is a substring of homeowner labels May 18, 2025
@StephanTLavavej StephanTLavavej self-assigned this May 18, 2025
@StephanTLavavej StephanTLavavej removed their assignment May 20, 2025
@StephanTLavavej StephanTLavavej moved this from Initial Review to Ready To Merge in STL Code Reviews May 20, 2025
@StephanTLavavej StephanTLavavej moved this from Ready To Merge to Merging in STL Code Reviews May 22, 2025
@StephanTLavavej

Copy link
Copy Markdown
Member

I'm mirroring this to the MSVC-internal repo - please notify me if any further changes are pushed.

@StephanTLavavej StephanTLavavej merged commit 87a97d2 into microsoft:main May 22, 2025
40 checks passed
@github-project-automation github-project-automation Bot moved this from Merging to Done in STL Code Reviews May 22, 2025
@StephanTLavavej

Copy link
Copy Markdown
Member

@muellerj2 muellerj2 deleted the regex-no-allocations-during-matching-of-regexes-without-captures branch May 31, 2025 21:44
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

None yet

Development

Successfully merging this pull request may close these issues.

3 participants