Re: array_shift() and reindexing

From: Date: Thu, 12 Jul 2012 13:33:22 +0000
Subject: Re: array_shift() and reindexing
References: 1 2  Groups: php.internals 
Request: Send a blank email to internals+get-61165@lists.php.net to get a copy of this message
Op 12-07-12 15:18, Johannes Schlüter schreef:
On Thu, 2012-07-12 at 13:43 +0200, Jille Timmermans wrote:
Hello, array_shift() currently reindexes the array after shifting one element. The reindexing has quite some impact on it's performance. I would like to suggest an extra parameter to array_shift() which can be used to prevent reindexing. From a few quick (and sloppy!) tests I get these results:
    `reset($array); list($key, $value) = each($array);
unset($array[$key]);` is ~50-150 times faster than array_shift().
    Calling array_reverse() and using array_pop() is ~2000-5000 times faster.
If you agree this would be an useful addition I will create a patch.
I haven't checked this, but is the difference only due to the re-hashing or also due to the fact that that array_shift() is taking the array by reference and therefore making a copy during the call if other cow-references (refcount>1, isref=0) exist? If I disable the reindexing code array_shift() becomes ~300 times faster.
-- Jille

« previous php.internals (#61165) next »