Skip to content
This repository has been archived by the owner on Jun 1, 2023. It is now read-only.

Commit

Permalink
SvGROW: no overallocation
Browse files Browse the repository at this point in the history
e.g. adding ".pm" to "warnings": 8 + 3 = 11. needing a len=12.
SvLEN is already 12, but sv_grow realloced to 8+3+3=14+1=15->16.

various people decided over time in the front and in the back to add just
another one for safety.  sv_grow adds the one for \0 already, so there's no
need in the front.  only the SvGROW macro logic, which checks against SvLEN.
But even this had it off-by-one.

memory numbers (DEBUGGING):
miniperl -e0            262890 => 262833 byte
miniperl -Ilib -E0      425636 => 423040 byte
perl Config ...         797578 => 796932 byte

$ PERL_HASH_SEED=0 valgrind --tool=massif --massif-out-file=massif.perl.p0
./perl -Ilib -MConfig -Mwarnings -Mstrict -V:ccflags ; grep -h mem_heap_B=
massif.perl.p0 |cut -c12-|sort -rn|head -n1

With #141 smallstrings the first goes down to 262689 bytes.
  • Loading branch information
Reini Urban committed Nov 18, 2016
1 parent bba1b16 commit 351589b
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
7 changes: 4 additions & 3 deletions sv.c
Original file line number Diff line number Diff line change
Expand Up @@ -5473,11 +5473,12 @@ Perl_sv_catpvn_flags(pTHX_ SV *const dsv, const char *sstr, const STRLEN slen, c
sv_utf8_upgrade_flags_grow(dsv, 0, slen + 1);
dlen = SvCUR(dsv);
}
else SvGROW(dsv, dlen + slen + 3);
else
SvGROW(dsv, dlen + slen);
if (sstr == dstr)
sstr = SvPVX_const(dsv);
Move(sstr, SvPVX(dsv) + dlen, slen, char);
SvCUR_set(dsv, SvCUR(dsv) + slen);
SvCUR_set(dsv, dlen + slen);
}
else {
/* We inline bytes_to_utf8, to avoid an extra malloc. */
Expand All @@ -5489,7 +5490,7 @@ Perl_sv_catpvn_flags(pTHX_ SV *const dsv, const char *sstr, const STRLEN slen, c
bytes *and* utf8, which would indicate a bug elsewhere. */
assert(sstr != dstr);

SvGROW(dsv, dlen + slen * 2 + 3);
SvGROW(dsv, dlen + slen * 2);
d = (U8 *)SvPVX(dsv) + dlen;

while (sstr < send) {
Expand Down
8 changes: 4 additions & 4 deletions sv.h
Original file line number Diff line number Diff line change
Expand Up @@ -2262,14 +2262,14 @@ See also C<L</PL_sv_yes>> and C<L</PL_sv_no>>.

#ifdef PERL_ANY_COW
# define SvGROW(sv,len) \
(SvIsCOW(sv) || SvLEN(sv) < ((len)+2) ? sv_grow(sv,(len)+2) : SvPVX(sv))
(SvIsCOW(sv) || SvLEN(sv) <= (len) ? sv_grow(sv,(len)) : SvPVX(sv))
# define SvGROW_mutable(sv,len) \
(SvLEN(sv) < ((len)+2) ? sv_grow(sv,(len)+2) : SvPVX_mutable(sv))
(SvLEN(sv) <= (len) ? sv_grow(sv,(len)) : SvPVX_mutable(sv))
#else
# define SvGROW(sv,len) \
(SvLEN(sv) < ((len)+1) ? sv_grow(sv,(len)+1) : SvPVX(sv))
(SvLEN(sv) <= (len) ? sv_grow(sv,(len)) : SvPVX(sv))
# define SvGROW_mutable(sv,len) \
(SvLEN(sv) < ((len)+1) ? sv_grow(sv,(len)+1) : SvPVX_mutable(sv))
(SvLEN(sv) <= (len) ? sv_grow(sv,(len)) : SvPVX_mutable(sv))
#endif
#define Sv_Grow sv_grow

Expand Down

0 comments on commit 351589b

Please sign in to comment.