Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 14 additions & 18 deletions av.c
Original file line number Diff line number Diff line change
Expand Up @@ -266,9 +266,6 @@ S_adjust_index(pTHX_ AV *av, const MAGIC *mg, SSize_t *keyp)
SV**
Perl_av_fetch(pTHX_ AV *av, SSize_t key, I32 lval)
{
SSize_t neg;
SSize_t size;

PERL_ARGS_ASSERT_AV_FETCH;

if (UNLIKELY(SvRMAGICAL(av))) {
Expand All @@ -291,24 +288,23 @@ Perl_av_fetch(pTHX_ AV *av, SSize_t key, I32 lval)
}
}

neg = (key < 0);
size = AvFILLp(av) + 1;
key += neg * size; /* handle negative index without using branch */
/* recalculating key early helps branch prediction */
SSize_t neg = key < 0;
SSize_t size = AvFILLp(av) + 1;
key += neg * size;

/* the cast from SSize_t to Size_t allows both (key < 0) and (key >= size)
* to be tested as a single condition */
if ((Size_t)key >= (Size_t)size) {
if (UNLIKELY(neg))
return NULL;
goto emptiness;
}

if (!AvARRAY(av)[key]) {
emptiness:
return lval ? av_store(av,key,newSV_type(SVt_NULL)) : NULL;
}

return &AvARRAY(av)[key];
if ((Size_t)key >= (Size_t)size)
/* Only non-negative out of bounds keys are eligible for lvalue
* treatment, so adjust lval by setting value to 0 if the key was
* negative */
lval *= !neg;
else if (AvARRAY(av)[key])
return &AvARRAY(av)[key];

/* value does not exist in the array */
return lval ? av_store(av, key, newSV_type(SVt_NULL)) : NULL;
}

/*
Expand Down
Loading