Skip to content

ext/standard: Skip empty inputs and optimize packed src in array_merge() - #486

Closed
iliaal wants to merge 1 commit into
masterfrom
opt/array-merge-empty-packed
Closed

iliaal wants to merge 1 commit into
masterfrom
opt/array-merge-empty-packed

Conversation

@iliaal

@iliaal iliaal commented Nov 27, 2026

Copy link
Copy Markdown
Owner

array_merge() and array_merge_recursive() always copied args[0] even when it was empty, preventing packed preservation on leading empty arrays and invoking php_array_merge() on empty trailing arguments.

Return an empty array immediately when total count is zero, skip leading and subsequent empty inputs, and add a packed-source loop in php_array_merge() for merging packed arrays into non-packed destinations. The argc == 2 shortcut is left untouched to stay independent of phpGH-24404.

On ARM64 (Neoverse-N1, GCC 13.3 -O2 NTS): leading empty + packed 3.11x (-67.79%, -53.52% insns), all empty 1.50x (-33.07%, -24.36% insns), mixed + packed 1.05x (-4.43%, -2.14% insns).

…y_merge()

array_merge() and array_merge_recursive() copied the first argument even
when it was empty, so a leading empty array sent every later packed argument
through the hash insertion path, and empty arguments still went through
php_array_merge(). Return an empty array when all arguments are empty, start
from the first non-empty argument, skip empty ones, and append packed
sources to a hash destination without checking for string keys. A temporary
argument is still reused in place only when it is the first argument,
because that path keeps the argument's next free index (see phpGH-24404).

Neoverse-N1, GCC 13.3 -O2 NTS: empty array followed by two 1,000-element
lists 3.12x, three empty arrays 2.11x, a string-keyed array followed by two
lists 1.09x (-5.1% instructions), two lists unchanged (+0.2% instructions).
@iliaal
iliaal force-pushed the opt/array-merge-empty-packed branch from 798f658 to 75babc3 Compare November 27, 2026 16:46
@iliaal

iliaal commented Nov 27, 2026

Copy link
Copy Markdown
Owner Author

Submitted upstream as php/php-src phpGH-25846: php#25846

@iliaal iliaal closed this Nov 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant