Skip to content

Commit 3001ef3

Browse files
committed
regcomp.c: Improve free-ing up unused inversion list space
The invlist_trim() function wasn't freeing up space if the new space needed was small. This now frees up all but the required minimum.
1 parent e7d603c commit 3001ef3

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

regcomp.c

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,10 @@ EXTERN_C const struct regexp_engine my_reg_engine;
105105
#define MIN(a,b) ((a) < (b) ? (a) : (b))
106106
#endif
107107

108+
#ifndef MAX
109+
#define MAX(a,b) ((a) > (b) ? (a) : (b))
110+
#endif
111+
108112
/* this is a chain of data about sub patterns we are processing that
109113
need to be handled separately/specially in study_chunk. Its so
110114
we can simulate recursion without losing state. */
@@ -8430,16 +8434,18 @@ S_invlist_set_previous_index(SV* const invlist, const IV index)
84308434
PERL_STATIC_INLINE void
84318435
S_invlist_trim(SV* invlist)
84328436
{
8437+
/* Free the not currently-being-used space in an inversion list */
8438+
8439+
/* But don't free up the space needed for 0 UV that is always at the
8440+
* beginning of the list, nor the trailing NUL */
8441+
const UV min_size = TO_INTERNAL_SIZE(1) + 1;
8442+
84338443
PERL_ARGS_ASSERT_INVLIST_TRIM;
84348444

84358445
assert(SvTYPE(invlist) == SVt_INVLIST);
84368446

8437-
/* Change the length of the inversion list to how many entries it currently
8438-
* has. But don't shorten it so that it would free up the required
8439-
* initial 0 UV (and a trailing NUL byte) */
8440-
if (SvCUR(invlist) > TO_INTERNAL_SIZE(1) + 1) {
8441-
SvPV_shrink_to_cur(invlist);
8442-
}
8447+
SvPV_renew(invlist, MAX(min_size, SvCUR(invlist) + 1));
8448+
84438449
}
84448450

84458451
PERL_STATIC_INLINE void

0 commit comments

Comments
 (0)