Skip to content

Commit

Permalink
grow the tmps (mortal) stack exponentially rather than linearly
Browse files Browse the repository at this point in the history
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
  • Loading branch information
tonycoz committed Nov 27, 2023
1 parent 55b0882 commit 7a28def
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
8 changes: 6 additions & 2 deletions scope.c
Expand Up @@ -246,8 +246,12 @@ Perl_tmps_grow_p(pTHX_ SSize_t ix)
{
SSize_t extend_to = ix;
#ifndef STRESS_REALLOC
if (ix - PL_tmps_max < 128)
extend_to += (PL_tmps_max < 512) ? 128 : 512;
SSize_t grow_size = PL_tmps_max < 512 ? 128 : PL_tmps_max / 5;
if (extend_to >= SSize_t_MAX - grow_size)
/* trigger memwrap message or fail allocation */
extend_to = SSize_t_MAX-1;
else
extend_to += grow_size;
#endif
Renew(PL_tmps_stack, extend_to + 1, SV*);
PL_tmps_max = extend_to + 1;
Expand Down
1 change: 0 additions & 1 deletion t/perf/tmps.t
Expand Up @@ -48,7 +48,6 @@ my $min_small = min($basetimes{$small_size}->@*);
my $min_large = min($basetimes{$large_size}->@*);
my $ratio = $large_size / $small_size;

our $TODO = "tmps stack grows in O(n**2) time";
my $worst = $min_small * $ratio * 2;
note "worst allowed $worst";
cmp_ok($min_large, '<', $worst,
Expand Down

0 comments on commit 7a28def

Please sign in to comment.