diff --git a/NEWS b/NEWS index e96c1d882c9f..04aeb992d4ee 100644 --- a/NEWS +++ b/NEWS @@ -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 diff --git a/UPGRADING b/UPGRADING index fcff11aabf0b..dbc9cd7d484d 100644 --- a/UPGRADING +++ b/UPGRADING @@ -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. diff --git a/ext/standard/array.c b/ext/standard/array.c index 11fd3f0e29a9..360a878ceb2f 100644 --- a/ext/standard/array.c +++ b/ext/standard/array.c @@ -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) { @@ -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) && @@ -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; @@ -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); @@ -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)); } diff --git a/ext/standard/tests/array/array_merge_empty_packed.phpt b/ext/standard/tests/array/array_merge_empty_packed.phpt new file mode 100644 index 000000000000..6cae71164d1c --- /dev/null +++ b/ext/standard/tests/array/array_merge_empty_packed.phpt @@ -0,0 +1,71 @@ +--TEST-- +array_merge() and array_merge_recursive() with leading, middle, and all-empty inputs +--FILE-- + '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) +}