Skip to content

<regex>: Optimize matcher layout#6162

Merged
StephanTLavavej merged 1 commit into
microsoft:mainfrom
muellerj2:regex-optimize-matcher-layout
Mar 24, 2026
Merged

<regex>: Optimize matcher layout#6162
StephanTLavavej merged 1 commit into
microsoft:mainfrom
muellerj2:regex-optimize-matcher-layout

Conversation

@muellerj2

Copy link
Copy Markdown

This PR saves 8 bytes (x64 and x86) in the matcher by roughly ordering the members descendingly according to alignment. (I say roughly because some types are user-defined types, so their placement is a bit of a guess.)

Test program:

#include <iostream>
#include <regex>
#include <string>

using namespace std;

int main() {
    cout << "size const char*: " << sizeof(_Matcher3<const char*, char, regex_traits<char>, const char*, void>) << "\n";
    cout << "size string: " << sizeof(_Matcher3<string::iterator, char, regex_traits<char>, string::iterator, void>)
         << "\n";
    cout << "size const wchar_t*: "
         << sizeof(_Matcher3<const wchar_t*, wchar_t, regex_traits<wchar_t>, const wchar_t*, void>) << "\n";
    cout << "size wstring: " << sizeof(_Matcher3<wstring::iterator, wchar_t, regex_traits<wchar_t>, wstring::iterator, void>)
         << "\n";
}
Size comparison x64

Before:

size const char*: 256
size string: 256
size const wchar_t*: 256
size wstring: 256

After:

size const char*: 248
size string: 248
size const wchar_t*: 248
size wstring: 248
Size comparison x86

Before:

size const char*: 152
size string: 152
size const wchar_t*: 152
size wstring: 152

After:

size const char*: 144
size string: 144
size const wchar_t*: 144
size wstring: 144

@muellerj2 muellerj2 requested a review from a team as a code owner March 15, 2026 22:25
@github-project-automation github-project-automation Bot moved this to Initial Review in STL Code Reviews Mar 15, 2026
@StephanTLavavej StephanTLavavej self-assigned this Mar 16, 2026
@StephanTLavavej StephanTLavavej added performance Must go faster regex meow is a substring of homeowner labels Mar 16, 2026
@StephanTLavavej

Copy link
Copy Markdown
Member

I also love that this consolidates the data members into a single place!

Layout looks good:

D:\GitHub\STL\out\x64>type meow.cpp
#include <regex>
using namespace std;
int main() {
    regex r{"a+"};
    (void) regex_match("a", r);
}

D:\GitHub\STL\out\x64>cl /EHsc /nologo /W4 /std:c++latest /MT /Od /c meow.cpp
meow.cpp

D:\GitHub\STL\out\x64>cl /EHsc /nologo /W4 /std:c++latest /MT /Od /c /d1reportSingleClassLayout_Matcher3 meow.cpp
meow.cpp

class std::_Matcher3<char const *,char,class std::regex_traits<char>,char const *,void> size(248):
        +---
 0      | _Complexity_limit
 8      | ?$_Tgt_state_v2_t@PEBDV?$allocator@PEBD@std@@ _Tgt_state
64      | ?$_Tgt_state_v2_t@PEBDV?$allocator@PEBD@std@@ _Res
120     | ?$_Rx_fixed_size_buffer@U?$_Loop_vals_v3_t@_J@std@@V?$allocator@U?$_Loop_vals_v3_t@_J@std@@@2@ _Loop_vals
144     | ?$_Rx_small_vector@V?$_Rx_state_frame_t@PEBD@std@@V?$allocator@V?$_Rx_state_frame_t@PEBD@std@@@2@ _Frames
176     | _Frames_count
184     | _Frames_limit
192     | _Start
200     | _Traits
208     | _Begin
216     | _End
224     | syntax_option_type _Sflags
228     | match_flag_type _Mflags
232     | _Ncap
236     | _Char_class_w
238     | _Char_class_s
240     | _Char_class_d
242     | _Matched
243     | _Longest
244     | _Full
        | <alignment member> (size=3)
        +---

@StephanTLavavej StephanTLavavej removed their assignment Mar 16, 2026
@StephanTLavavej StephanTLavavej moved this from Initial Review to Ready To Merge in STL Code Reviews Mar 16, 2026
@StephanTLavavej StephanTLavavej moved this from Ready To Merge to Merging in STL Code Reviews Mar 21, 2026
@StephanTLavavej

Copy link
Copy Markdown
Member

I'm mirroring this to the MSVC-internal repo. Please notify me if any further changes are pushed, otherwise no action is required.

StephanTLavavej added a commit to StephanTLavavej/STL that referenced this pull request Mar 21, 2026
StephanTLavavej added a commit to StephanTLavavej/STL that referenced this pull request Mar 21, 2026
@StephanTLavavej StephanTLavavej merged commit df0da77 into microsoft:main Mar 24, 2026
49 checks passed
@github-project-automation github-project-automation Bot moved this from Merging to Done in STL Code Reviews Mar 24, 2026
@StephanTLavavej

Copy link
Copy Markdown
Member

Thanks for getting everything tidied up for 14.51! 🎉 😻 🐈

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