PHP array_shift(): Optimize tail move on hole-free packed arrays - #23517
Draft
mehmetcansahin wants to merge 1 commit into
Draft
PHP array_shift(): Optimize tail move on hole-free packed arrays#23517mehmetcansahin wants to merge 1 commit into
mehmetcansahin wants to merge 1 commit into
Conversation
When a packed array has no holes and no active iterators, the slot vacated by the shifted element is the only hole, so the tail can be moved down with a single memmove instead of testing every slot for IS_UNDEF and comparing source and destination indexes. A tail of a single element is moved directly instead, avoiding the memmove call overhead on tiny arrays.
mehmetcansahin
force-pushed
the
array-shift-loop
branch
from
August 30, 2026 22:12
fc910a3 to
0d2cf57
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
For a packed array without holes, removing the first element leaves a contiguous tail. When no iterator is active,
array_shift()now moves that tail with a singlememmove()instead of testing every slot forIS_UNDEF; a one element tail is moved directly to avoid the call overhead. Packed arrays with holes, arrays with active iterators, and hash arrays keep the existing paths.Benchmarks
Draining a packed int array (
$a = $base; while ($a) array_shift($a);):Two-element regression check (
$a = [0, 1]; array_shift($a);x 100M, exercises the direct-copy specialization): 3.528s → 3.438 s, no tiny-array slowdown observed.Benchmark scripts
Draining benchmark:
Two-element regression check: