On Wed, Jan 21, 2026 at 1:37 PM Patrick Palka <[email protected]> wrote:
>
> On Wed, 21 Jan 2026, Patrick Palka wrote:
>
> > If __rep_count.second is already 1 we don't need to add a cleanup frame
> > to restore it to 1.
>
> FWIW it seems unnecessary to restore __rep_count.first and
> __rep_count.second at all here. The __rep_count stuff is for infinite
> loop detection, and I don't see why such backtracking is necessary for
> that..
Never mind, if we don't backtrack and restore the __rep_count.first
position here,
then we end up looping on
regex_match("a", regex("(a|)*b"))
So the backtracking seems necessary.
>
> >
> > This reduces the maximum size of the __frames stack by 12% for
> > regex_match(string(200000, 'a'), "(a|b|c)*").
> >
> > PR libstdc++/86164
> >
> > libstdc++-v3/ChangeLog:
> >
> > * include/bits/regex_executor.tcc: Only add a
> > restore_rep_count_val frame when necessary.
> > ---
> > libstdc++-v3/include/bits/regex_executor.tcc | 9 ++++++---
> > 1 file changed, 6 insertions(+), 3 deletions(-)
> >
> > diff --git a/libstdc++-v3/include/bits/regex_executor.tcc
> > b/libstdc++-v3/include/bits/regex_executor.tcc
> > index 0e78cb2e2c87..ca631fb77ce9 100644
> > --- a/libstdc++-v3/include/bits/regex_executor.tcc
> > +++ b/libstdc++-v3/include/bits/regex_executor.tcc
> > @@ -181,12 +181,15 @@ namespace __detail
> > auto& __rep_count = _M_rep_count[__i];
> > if (__rep_count.second == 0 || __rep_count.first != _M_current)
> > {
> > - __frames.emplace_back(_S_fopcode_restore_rep_count_val,
> > - __i, __rep_count.second);
> > + if (__rep_count.second != 1)
> > + {
> > + __frames.emplace_back(_S_fopcode_restore_rep_count_val,
> > + __i, __rep_count.second);
> > + __rep_count.second = 1;
> > + }
> > __frames.emplace_back(_S_fopcode_restore_rep_count_pos,
> > __i, __rep_count.first);
> > __rep_count.first = _M_current;
> > - __rep_count.second = 1;
> > __frames.emplace_back(_S_fopcode_next, __state._M_alt);
> > }
> > else
> > --
> > 2.53.0.rc0.25.gb5c409c40f
> >
> >