Branch: refs/heads/blead
Home: https://github.com/Perl/perl5
Commit: c1e2bc4fad43edd192c16dbca20c61bc4f5d98a8
https://github.com/Perl/perl5/commit/c1e2bc4fad43edd192c16dbca20c61bc4f5d98a8
Author: Richard Leach <[email protected]>
Date: 2026-07-17 (Fri, 17 Jul 2026)
Changed paths:
M pod/perlreapi.pod
M regcomp.c
M regexec.c
M regexp.h
Log Message:
-----------
Perl_regexec_flags - keep and reuse a spare *offs buffer once allocated
When executing a regular expression that was the origin of the previous
successful match, the results of that previous match must not be
overwritten in case the current attempt at matching is unsuccessful.
(That is to say, `(PL_curpm && (PM_GETRE(PL_curpm) == rx))` and the
contents of special punctuation variables such as `%+` and `$1` etc.
must not be clobbered if the current regex fails to match.)
Perl does this by moving the the associated heap allocated
`regexp_paren_pair` chunk to one side and working on an identically-sized
`swap` chunk. Currently, the working chunk is allocated on each entry to
`Perl_regexec_flags` and the previous chunk freed prior to exit.
(If the current match is unsuccessful, the contents of the previous
chunk are copied to the new chunk.)
To illustrate, every iteration of the following code snippet (after the
first) will allocate and free a `regexp_paren_pair` chunk.
while ($x =~ /(.)/g) { ... }
While allocation is unavoidable in (probably uncommon) cases where the
regex is re-entrant, for common cases such as the above snippet, only two
allocations are needed - the previous one and the current one - and the
heap management overhead is undesirable.
This commit adds a lazily-allocated spare chunk when first needed, then
keeps it around for future reuse. If additional chunks are needed, they
will still be freshly allocated and then later freed as happens now.
The commit also changes chunk initialization: instead of zeroing it,
the `start` and `end` members of each struct are set to `-1`, which
matches up to what `S_regtry` does. (See the comments in that function
beginning with `XXXX What this code is doing here?!!!` for context.)
To unsubscribe from these emails, change your notification settings at
https://github.com/Perl/perl5/settings/notifications