Skip to content

Commit

Permalink
Perl_clear_defarray: faster array creation via new macro+function
Browse files Browse the repository at this point in the history
  • Loading branch information
richardleach committed May 22, 2021
1 parent e49633c commit 7faeda8
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 5 deletions.
2 changes: 1 addition & 1 deletion av.c
Expand Up @@ -418,7 +418,7 @@ Perl_av_new_alloc(pTHX_ SSize_t size, bool zeroflag)
AV * const av = newAV();
SV** ary;
PERL_ARGS_ASSERT_AV_NEW_ALLOC;
assert(size > 1);
assert(size > 0);

Newx(ary, size, SV*); /* Newx performs the memwrap check */
AvALLOC(av) = ary;
Expand Down
7 changes: 3 additions & 4 deletions pp_hot.c
Expand Up @@ -4966,17 +4966,16 @@ PP(pp_leavesub)
void
Perl_clear_defarray(pTHX_ AV* av, bool abandon)
{
const SSize_t fill = AvFILLp(av);

PERL_ARGS_ASSERT_CLEAR_DEFARRAY;

if (LIKELY(!abandon && SvREFCNT(av) == 1 && !SvMAGICAL(av))) {
av_clear(av);
AvREIFY_only(av);
}
else {
AV *newav = newAV();
av_extend(newav, fill);
const SSize_t size = AvFILLp(av) + 1;
/* The ternary gives consistency with av_extend() */
AV *newav = newAV_alloc_x(size < 4 ? 4 : size);
AvREIFY_only(newav);
PAD_SVl(0) = MUTABLE_SV(newav);
SvREFCNT_dec_NN(av);
Expand Down

0 comments on commit 7faeda8

Please sign in to comment.