Skip to content

Commit 99e7605

Browse files
committed
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.
1 parent 99f72fa commit 99e7605

File tree

1 file changed

+9
-11
lines changed

1 file changed

+9
-11
lines changed

ext/standard/array.c

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6738,8 +6738,6 @@ PHP_FUNCTION(array_map)
67386738
if (n_arrays == 1) {
67396739
zend_ulong num_key;
67406740
zend_string *str_key;
6741-
zval *zv, arg;
6742-
int ret;
67436741

67446742
if (Z_TYPE(arrays[0]) != IS_ARRAY) {
67456743
zend_argument_type_error(2, "must be of type array, %s given", zend_zval_value_name(&arrays[0]));
@@ -6756,15 +6754,13 @@ PHP_FUNCTION(array_map)
67566754
array_init_size(return_value, maxlen);
67576755
zend_hash_real_init(Z_ARRVAL_P(return_value), HT_IS_PACKED(Z_ARRVAL(arrays[0])));
67586756

6759-
ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL(arrays[0]), num_key, str_key, zv) {
6760-
fci.retval = &result;
6761-
fci.param_count = 1;
6762-
fci.params = &arg;
6757+
fci.retval = &result;
6758+
fci.param_count = 1;
67636759

6764-
ZVAL_COPY(&arg, zv);
6765-
ret = zend_call_function(&fci, &fci_cache);
6766-
i_zval_ptr_dtor(&arg);
6767-
if (ret != SUCCESS || Z_TYPE(result) == IS_UNDEF) {
6760+
ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL(arrays[0]), num_key, str_key, fci.params) {
6761+
zend_result ret = zend_call_function(&fci, &fci_cache);
6762+
ZEND_ASSERT(ret == SUCCESS);
6763+
if (Z_TYPE(result) == IS_UNDEF) {
67686764
zend_array_destroy(Z_ARR_P(return_value));
67696765
RETURN_NULL();
67706766
}
@@ -6874,7 +6870,9 @@ PHP_FUNCTION(array_map)
68746870
fci.param_count = n_arrays;
68756871
fci.params = params;
68766872

6877-
if (zend_call_function(&fci, &fci_cache) != SUCCESS || Z_TYPE(result) == IS_UNDEF) {
6873+
zend_call_function(&fci, &fci_cache);
6874+
6875+
if (Z_TYPE(result) == IS_UNDEF) {
68786876
efree(array_pos);
68796877
zend_array_destroy(Z_ARR_P(return_value));
68806878
for (i = 0; i < n_arrays; i++) {

0 commit comments

Comments
 (0)