This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
av_fetch(): optimise the negative index branch.
authorDavid Mitchell <davem@iabyn.com>
Wed, 17 Aug 2016 08:59:06 +0000 (09:59 +0100)
committerDavid Mitchell <davem@iabyn.com>
Wed, 17 Aug 2016 12:38:56 +0000 (13:38 +0100)
For a negative index one conditional is redundant, since after determining
that key < 0 and recomputing key as (AvFILLp(av) - key), key can't
be > AvFILLp(av).

av.c

diff --git a/av.c b/av.c
index a48702b..8967da5 100644 (file)
--- a/av.c
+++ b/av.c
@@ -272,9 +272,11 @@ Perl_av_fetch(pTHX_ AV *av, SSize_t key, I32 lval)
        key += AvFILLp(av) + 1;
        if (key < 0)
            return NULL;
        key += AvFILLp(av) + 1;
        if (key < 0)
            return NULL;
+        assert(key <= AvFILLp(av));
+        if (!AvARRAY(av)[key])
+            goto emptyness;
     }
     }
-
-    if (key > AvFILLp(av) || !AvARRAY(av)[key]) {
+    else if (key > AvFILLp(av) || !AvARRAY(av)[key]) {
       emptyness:
        return lval ? av_store(av,key,newSV(0)) : NULL;
     }
       emptyness:
        return lval ? av_store(av,key,newSV(0)) : NULL;
     }