[Perl/perl5] 7d1844: Perl_av_extend_guts - add a bump to small AV, smal...

[email protected] (Richard Leach via perl5-changes) Mon, 27 Jul 2026 01:07:41 -0700
Newsgroups perl.perl5.changes
Message-ID <Perl/perl5/push/refs/heads/blead/[email protected]>
  Branch: refs/heads/blead
  Home:   https://github.com/Perl/perl5
  Commit: 7d184441cd91cc60d096a4fb399a68c6e5916522
      https://github.com/Perl/perl5/commit/7d184441cd91cc60d096a4fb399a68c6e5916522
  Author: Richard Leach <[email protected]>
  Date:   2026-07-27 (Mon, 27 Jul 2026)

  Changed paths:
    M av.c

  Log Message:
  -----------
  Perl_av_extend_guts - add a bump to small AV, small growth calls

The `*maxp / 5` growth factor does not significantly contribute when
`*maxp` is small. If the number of additional elements being requested
is tiny, but repeated - such as when elements are pushed to an arry
one-at-a-time, then there is a risk of reallocation churn. (The same
array is resized to add just 1-2 new elements, multiple times over.)

This shows up in `Perl_sv_add_backref`, where any AV that is grown
from the original allocation once has a good chance of then being
grown multiple times more via `av_extend(av, AvFILLp(av)+1);`.
For example, instrumenting that function in blead_ and running
`./perl -Ilib -e 'use strict; use warnings'` shows:
```
Reallocating for main: 3 to 4
Reallocating for main: 4 to 5
Reallocating for main: 5 to 6
Reallocating for main: 7 to 8
Reallocating for main: 9 to 10
Reallocating for main: 11 to 12
Reallocating for UNIVERSAL: 3 to 4
Reallocating for UNIVERSAL: 4 to 5
Reallocating for version: 3 to 4
Reallocating for version: 4 to 5
Reallocating for version: 5 to 6
Reallocating for version: 7 to 8
Reallocating for version: 9 to 10
Reallocating for version: 11 to 12
Reallocating for version: 14 to 15
Reallocating for version: 17 to 18
Reallocating for version: 21 to 22
Reallocating for version: 26 to 27
Reallocating for utf8: 3 to 4
Reallocating for utf8: 4 to 5
Reallocating for utf8: 5 to 6
Reallocating for main: 14 to 15
Reallocating for re: 3 to 4
Reallocating for main: 17 to 18
Reallocating for Tie::Hash::NamedCapture: 3 to 4
Reallocating for Tie::Hash::NamedCapture: 4 to 5
Reallocating for Tie::Hash::NamedCapture: 5 to 6
Reallocating for Tie::Hash::NamedCapture: 7 to 8
Reallocating for Tie::Hash::NamedCapture: 9 to 10
Reallocating for builtin: 3 to 4
Reallocating for builtin: 4 to 5
Reallocating for builtin: 5 to 6
Reallocating for builtin: 7 to 8
Reallocating for builtin: 9 to 10
Reallocating for builtin: 11 to 12
Reallocating for builtin: 14 to 15
Reallocating for builtin: 17 to 18
Reallocating for Internals: 3 to 4
Reallocating for main: 21 to 22
Reallocating for main: 26 to 27
Reallocating for main: 32 to 33
Reallocating for main: 39 to 40
Reallocating for strict: 3 to 4
Reallocating for strict: 4 to 5
Reallocating for strict: 5 to 6
Reallocating for strict: 7 to 8
Reallocating for strict: 9 to 10
Reallocating for warnings: 3 to 4
Reallocating for warnings: 4 to 5
Reallocating for warnings: 5 to 6
Reallocating for warnings: 7 to 8
Reallocating for warnings: 9 to 10
Reallocating for warnings: 11 to 12
Reallocating for warnings: 14 to 15
Reallocating for warnings: 17 to 18
Reallocating for warnings: 21 to 22
Reallocating for main: 47 to 48
Reallocating for warnings: 26 to 27
Reallocating for warnings: 32 to 33
Reallocating for warnings: 39 to 40
Reallocating for warnings: 47 to 48
```
This commit tweaks the growth of small-array, small-increase calls
within `Perl_av_entend_guts`, reducing this churn:
```
Reallocating for main: 3 to 11
Reallocating for main: 11 to 19
Reallocating for UNIVERSAL: 3 to 11
Reallocating for version: 3 to 11
Reallocating for version: 11 to 19
Reallocating for version: 21 to 29
Reallocating for utf8: 3 to 11
Reallocating for re: 3 to 11
Reallocating for Internals: 3 to 11
Reallocating for Tie::Hash::NamedCapture: 3 to 11
Reallocating for builtin: 3 to 11
Reallocating for builtin: 11 to 19
Reallocating for main: 21 to 29
Reallocating for main: 33 to 41
Reallocating for main: 40 to 48
Reallocating for strict: 3 to 11
Reallocating for warnings: 3 to 11
Reallocating for warnings: 11 to 19
Reallocating for warnings: 21 to 29
Reallocating for main: 49 to 57
Reallocating for warnings: 33 to 41
Reallocating for warnings: 40 to 48
```

Similar churn is also seen from other sources (not individually identified)
and would occur in Perl code wherever a new/small array is grown one
element at a time, using code like:
`push @array, $element;`



To unsubscribe from these emails, change your notification settings at https://github.com/Perl/perl5/settings/notifications