Skip to content

Commit fea90cf

Browse files
committed
grow the tmps (mortal) stack exponentially rather than linearly
As with the value stack and the save stack, this gives us constant amortized growth per element. After this patch the profiler shows the "SvPV_shrink_to_cur(sv)" and "sv = sv_2mortal(newSV(80))" calls in do_readline as the hotspots for the io unheated test case, using 55% of the measured time in total. Fixes #21654
1 parent 7fbd97b commit fea90cf

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

scope.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -246,8 +246,12 @@ Perl_tmps_grow_p(pTHX_ SSize_t ix)
246246
{
247247
SSize_t extend_to = ix;
248248
#ifndef STRESS_REALLOC
249-
if (ix - PL_tmps_max < 128)
250-
extend_to += (PL_tmps_max < 512) ? 128 : 512;
249+
SSize_t grow_size = PL_tmps_max < 512 ? 128 : PL_tmps_max / 2;
250+
if (extend_to > SSize_t_MAX - grow_size - 1)
251+
/* trigger memwrap message or fail allocation */
252+
extend_to = SSize_t_MAX-1;
253+
else
254+
extend_to += grow_size;
251255
#endif
252256
Renew(PL_tmps_stack, extend_to + 1, SV*);
253257
PL_tmps_max = extend_to + 1;

0 commit comments

Comments
 (0)