Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,8 @@ PHP NEWS
specified number base. (Sjoerd Langkemper)
. Improved performance of in_array() and array_search() in strict mode when
searching for a string. (Alexander Danilov)
. Improved performance of array_merge() and array_merge_recursive() when
some arguments are empty arrays. (Ilia Alshanetsky)

- URI:
. Fix casing of enum cases in UriHostType and UrlHostType to match the RFC
Expand Down
2 changes: 2 additions & 0 deletions UPGRADING
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,8 @@ PHP 8.7 UPGRADE NOTES
encoding.
. Improved performance of in_array() and array_search() in strict mode when
searching for a string.
. Improved performance of array_merge() and array_merge_recursive() when
some arguments are empty arrays.

- MBString:
. Improved performance of mb_strlen for UTF-8 strings.
Expand Down
33 changes: 28 additions & 5 deletions ext/standard/array.c
Original file line number Diff line number Diff line change
Expand Up @@ -4110,6 +4110,9 @@ PHPAPI int php_array_merge(HashTable *dest, HashTable *src) /* {{{ */
zval *src_entry;
zend_string *string_key;

if (zend_hash_num_elements(src) == 0) {
return 1;
}
if (HT_IS_PACKED(dest) && HT_IS_PACKED(src)) {
zend_hash_extend(dest, zend_hash_num_elements(dest) + zend_hash_num_elements(src), 1);
ZEND_HASH_FILL_PACKED(dest) {
Expand All @@ -4122,6 +4125,15 @@ PHPAPI int php_array_merge(HashTable *dest, HashTable *src) /* {{{ */
ZEND_HASH_FILL_ADD(src_entry);
} ZEND_HASH_FOREACH_END();
} ZEND_HASH_FILL_END();
} else if (HT_IS_PACKED(src)) {
ZEND_HASH_PACKED_FOREACH_VAL(src, src_entry) {
if (UNEXPECTED(Z_ISREF_P(src_entry) &&
Z_REFCOUNT_P(src_entry) == 1)) {
src_entry = Z_REFVAL_P(src_entry);
}
Z_TRY_ADDREF_P(src_entry);
zend_hash_next_index_insert_new(dest, src_entry);
} ZEND_HASH_FOREACH_END();
} else {
ZEND_HASH_FOREACH_STR_KEY_VAL(src, string_key, src_entry) {
if (UNEXPECTED(Z_ISREF_P(src_entry) &&
Expand Down Expand Up @@ -4299,6 +4311,10 @@ static zend_always_inline void php_array_merge_wrapper(INTERNAL_FUNCTION_PARAMET
RETURN_THROWS();
}

if (count == 0) {
RETURN_EMPTY_ARRAY();
}

if (argc == 2) {
zval *ret = NULL;

Expand Down Expand Up @@ -4329,13 +4345,17 @@ static zend_always_inline void php_array_merge_wrapper(INTERNAL_FUNCTION_PARAMET
}
}

arg = args;
src = Z_ARRVAL_P(arg);
uint32_t first = 0;
while (zend_hash_num_elements(Z_ARRVAL(args[first])) == 0) {
first++;
}
arg = args + first;
src = Z_ARRVAL_P(arg);
/* copy first array if necessary */
bool in_place = false;
if (HT_IS_PACKED(src)) {
/* Note: If it has holes, it might get sequentialized */
if (HT_IS_WITHOUT_HOLES(src) && zend_may_modify_arg_in_place(arg)) {
if (first == 0 && HT_IS_WITHOUT_HOLES(src) && zend_may_modify_arg_in_place(arg)) {
dest = src;
in_place = true;
RETVAL_ARR(dest);
Expand Down Expand Up @@ -4375,12 +4395,15 @@ static zend_always_inline void php_array_merge_wrapper(INTERNAL_FUNCTION_PARAMET
} ZEND_HASH_FOREACH_END();
}
if (recursive) {
for (i = 1; i < argc; i++) {
for (i = first + 1; i < argc; i++) {
arg = args + i;
if (zend_hash_num_elements(Z_ARRVAL_P(arg)) == 0) {
continue;
}
php_array_merge_recursive(dest, Z_ARRVAL_P(arg));
}
} else {
for (i = 1; i < argc; i++) {
for (i = first + 1; i < argc; i++) {
arg = args + i;
php_array_merge(dest, Z_ARRVAL_P(arg));
}
Expand Down
71 changes: 71 additions & 0 deletions ext/standard/tests/array/array_merge_empty_packed.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
--TEST--
array_merge() and array_merge_recursive() with leading, middle, and all-empty inputs
--FILE--
<?php
var_dump(array_merge([], [], []));
var_dump(array_merge([], [1, 2], [], [3, 4]));
var_dump(array_merge([], [5 => 'a', 10 => 'b']));
$holes = [0 => 'a', 1 => 'b'];
unset($holes[0]);
var_dump(array_merge([], $holes, [2 => 'c']));
var_dump(array_merge(['k' => 1], [10, 20]));
var_dump(array_merge_recursive([], ['a' => 1], [], ['a' => 2]));
function trailing_unset() {
$a = [1, 2, 3];
unset($a[2]);
return $a;
}
$r = array_merge([], [], trailing_unset(), []);
$r[] = 4;
var_dump($r);
?>
--EXPECT--
array(0) {
}
array(4) {
[0]=>
int(1)
[1]=>
int(2)
[2]=>
int(3)
[3]=>
int(4)
}
array(2) {
[0]=>
string(1) "a"
[1]=>
string(1) "b"
}
array(2) {
[0]=>
string(1) "b"
[1]=>
string(1) "c"
}
array(3) {
["k"]=>
int(1)
[0]=>
int(10)
[1]=>
int(20)
}
array(1) {
["a"]=>
array(2) {
[0]=>
int(1)
[1]=>
int(2)
}
}
array(3) {
[0]=>
int(1)
[1]=>
int(2)
[2]=>
int(4)
}
Loading