From fca36515b915d1a933792c0dca3b8d23c2a7ccca Mon Sep 17 00:00:00 2001 From: Niels Dossche <7771979+nielsdos@users.noreply.github.com> Date: Wed, 26 Mar 2025 23:29:47 +0100 Subject: [PATCH] Improve performance of array_map() The refcounting and destruction is not necessary because zend_call_function will make a copy anyway. And zend_call_function only returns FAILURE if EG(active) is false in which case array_map shouldn't have been called in the first place. --- ext/standard/array.c | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/ext/standard/array.c b/ext/standard/array.c index d26e2381802..89b775dc2f8 100644 --- a/ext/standard/array.c +++ b/ext/standard/array.c @@ -6749,8 +6749,6 @@ PHP_FUNCTION(array_map) if (n_arrays == 1) { zend_ulong num_key; zend_string *str_key; - zval *zv, arg; - int ret; if (Z_TYPE(arrays[0]) != IS_ARRAY) { zend_argument_type_error(2, "must be of type array, %s given", zend_zval_value_name(&arrays[0])); @@ -6767,15 +6765,14 @@ PHP_FUNCTION(array_map) array_init_size(return_value, maxlen); zend_hash_real_init(Z_ARRVAL_P(return_value), HT_IS_PACKED(Z_ARRVAL(arrays[0]))); - ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL(arrays[0]), num_key, str_key, zv) { - fci.retval = &result; - fci.param_count = 1; - fci.params = &arg; + fci.retval = &result; + fci.param_count = 1; - ZVAL_COPY(&arg, zv); - ret = zend_call_function(&fci, &fci_cache); - i_zval_ptr_dtor(&arg); - if (ret != SUCCESS || Z_TYPE(result) == IS_UNDEF) { + ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL(arrays[0]), num_key, str_key, fci.params) { + zend_result ret = zend_call_function(&fci, &fci_cache); + ZEND_ASSERT(ret == SUCCESS); + ZEND_IGNORE_VALUE(ret); + if (Z_TYPE(result) == IS_UNDEF) { zend_array_destroy(Z_ARR_P(return_value)); RETURN_NULL(); } @@ -6885,7 +6882,11 @@ PHP_FUNCTION(array_map) fci.param_count = n_arrays; fci.params = params; - if (zend_call_function(&fci, &fci_cache) != SUCCESS || Z_TYPE(result) == IS_UNDEF) { + zend_result ret = zend_call_function(&fci, &fci_cache); + ZEND_ASSERT(ret == SUCCESS); + ZEND_IGNORE_VALUE(ret); + + if (Z_TYPE(result) == IS_UNDEF) { efree(array_pos); zend_array_destroy(Z_ARR_P(return_value)); for (i = 0; i < n_arrays; i++) {