This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
av_fetch(): use less branches.
authorDavid Mitchell <davem@iabyn.com>
Wed, 17 Aug 2016 11:06:27 +0000 (12:06 +0100)
committerDavid Mitchell <davem@iabyn.com>
Wed, 17 Aug 2016 12:39:24 +0000 (13:39 +0100)
commitf4d8be8b395cdacf05f8c461fb7e9ce0366ff56f
tree87ffdc5f105230dcea6446fcb2ea3da5852c0ee7
parent11b62bc4c2460b23807b5c84c6e8b463068cf886
av_fetch(): use less branches.

The code that handles negative array indexes and out-of-bounds
negative indices used to require:

    2 conditions for a +ve index
    3 conditions for a -ve index

After this commit, for the common case where the index is in bounds,
it requires a single condition regardless of sign. For the less common
case of out-of-bounds, it requires 2 conditions.

Also, the one condition is more branch-predict friendly - it's whether
the index is in bounds or not. Previously the first test was whether
key < 0, and in code that does mixed signs, such as $a[0] + $a[-1],
branch prediction could be tricky.

It achieves this at the expense of a more complex expression for the key.
av.c