Repository navigation
array_unshift(): Shift packed arrays in place - #24475
Open
mehmetcansahin wants to merge 1 commit into
Open
mehmetcansahin wants to merge 1 commit into
mehmetcansahin wants to merge 1 commit into
Conversation
Packed arrays without holes and without iterators are shifted in place when rebuilding would allocate a table of the same size. Otherwise the array is still rebuilt, so unused slots are released.
devnexen
approved these changes
Oct 21, 2026
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.
This PR speeds up
array_unshift()on packed arrays by shifting values in place withmemmove()instead of allocating and rebuilding the table. The fast path applies when there are no holes or active iterators and rebuilding would use the same table size. Other paths, including table growth and shrinking, are unchanged.Benchmarks
Prepending one value on Apple M1, release/NTS build (
php -n), using the median of seven runs per build, alternating base and PR:Tested with the array suite on release and debug ASan/UBSan builds.
Benchmark script and methodology
Base
1c9242e7fec, this PRa667208a4da; Apple clang 21,-O2 -DNDEBUG, OPcache off. Each process calibrates to at least 60 ms and reports the lowest average time over five batches. Packed cases include thearray_pop()calls that keep the size constant; copy cases include copy-on-write separation. Negative change means faster.Run as
php -n bench.php '<case>' 60000000.