Skip to content

Commit fca3651

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 18102ec commit fca3651

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed

ext/standard/array.c

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6749,8 +6749,6 @@ PHP_FUNCTION(array_map)
67496749
if (n_arrays == 1) {
67506750
zend_ulong num_key;
67516751
zend_string *str_key;
6752-
zval *zv, arg;
6753-
int ret;
67546752

67556753
if (Z_TYPE(arrays[0]) != IS_ARRAY) {
67566754
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)
67676765
array_init_size(return_value, maxlen);
67686766
zend_hash_real_init(Z_ARRVAL_P(return_value), HT_IS_PACKED(Z_ARRVAL(arrays[0])));
67696767

6770-
ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL(arrays[0]), num_key, str_key, zv) {
6771-
fci.retval = &result;
6772-
fci.param_count = 1;
6773-
fci.params = &arg;
6768+
fci.retval = &result;
6769+
fci.param_count = 1;
67746770

6775-
ZVAL_COPY(&arg, zv);
6776-
ret = zend_call_function(&fci, &fci_cache);
6777-
i_zval_ptr_dtor(&arg);
6778-
if (ret != SUCCESS || Z_TYPE(result) == IS_UNDEF) {
6771+
ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL(arrays[0]), num_key, str_key, fci.params) {
6772+
zend_result ret = zend_call_function(&fci, &fci_cache);
6773+
ZEND_ASSERT(ret == SUCCESS);
6774+
ZEND_IGNORE_VALUE(ret);
6775+
if (Z_TYPE(result) == IS_UNDEF) {
67796776
zend_array_destroy(Z_ARR_P(return_value));
67806777
RETURN_NULL();
67816778
}
@@ -6885,7 +6882,11 @@ PHP_FUNCTION(array_map)
68856882
fci.param_count = n_arrays;
68866883
fci.params = params;
68876884

6888-
if (zend_call_function(&fci, &fci_cache) != SUCCESS || Z_TYPE(result) == IS_UNDEF) {
6885+
zend_result ret = zend_call_function(&fci, &fci_cache);
6886+
ZEND_ASSERT(ret == SUCCESS);
6887+
ZEND_IGNORE_VALUE(ret);
6888+
6889+
if (Z_TYPE(result) == IS_UNDEF) {
68896890
efree(array_pos);
68906891
zend_array_destroy(Z_ARR_P(return_value));
68916892
for (i = 0; i < n_arrays; i++) {

0 commit comments

Comments
 (0)