From 86a37d640326964ed0337d63bb54d01384d6b642 Mon Sep 17 00:00:00 2001 From: Niels Dossche <7771979+nielsdos@users.noreply.github.com> Date: Tue, 28 Oct 2025 22:43:46 +0100 Subject: [PATCH 1/3] Improve performance of array_walk() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit We never need to use refcounted copies for arguments because the copy to the call frame already increments the refcount. For the following benchmark: ```php $a = range(0, 10); for ($i = 0; $i < 1000000; $i++) array_walk($a, fn ($val, $idx, $arg) => $val + $arg, 2); ``` On an i7-4790: ``` Benchmark 1: ./sapi/cli/php x.php Time (mean ± σ): 593.0 ms ± 5.4 ms [User: 589.8 ms, System: 1.7 ms] Range (min … max): 583.3 ms … 600.9 ms 10 runs Benchmark 2: ./sapi/cli/php_old x.php Time (mean ± σ): 637.8 ms ± 4.6 ms [User: 633.9 ms, System: 2.2 ms] Range (min … max): 633.4 ms … 649.2 ms 10 runs Summary ./sapi/cli/php x.php ran 1.08 ± 0.01 times faster than ./sapi/cli/php_old x.php ``` On an i7-1185G7: ``` Benchmark 1: ./sapi/cli/php x.php Time (mean ± σ): 362.3 ms ± 2.0 ms [User: 359.9 ms, System: 1.9 ms] Range (min … max): 359.6 ms … 367.1 ms 10 runs Benchmark 2: ./sapi/cli/php_old x.php Time (mean ± σ): 385.5 ms ± 1.8 ms [User: 383.2 ms, System: 1.9 ms] Range (min … max): 381.5 ms … 387.2 ms 10 runs Summary ./sapi/cli/php x.php ran 1.06 ± 0.01 times faster than ./sapi/cli/php_old x.php ``` --- ext/standard/array.c | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/ext/standard/array.c b/ext/standard/array.c index 32d28fe70220..c5ca356427c0 100644 --- a/ext/standard/array.c +++ b/ext/standard/array.c @@ -1457,7 +1457,7 @@ static zend_result php_array_walk( /* Set up known arguments */ ZVAL_UNDEF(&args[1]); if (userdata) { - ZVAL_COPY(&args[2], userdata); + ZVAL_COPY_VALUE(&args[2], userdata); } fci.retval = &retval; @@ -1531,21 +1531,16 @@ static zend_result php_array_walk( } zval_ptr_dtor(&ref); } else { - ZVAL_COPY(&args[0], zv); + ZVAL_COPY_VALUE(&args[0], zv); /* Call the userland function */ result = zend_call_function(&fci, &context->fci_cache); if (result == SUCCESS) { zval_ptr_dtor(&retval); } - - zval_ptr_dtor(&args[0]); } - if (Z_TYPE(args[1]) != IS_UNDEF) { - zval_ptr_dtor(&args[1]); - ZVAL_UNDEF(&args[1]); - } + zval_ptr_dtor_str(&args[1]); if (result == FAILURE) { break; @@ -1565,9 +1560,6 @@ static zend_result php_array_walk( } } while (!EG(exception)); - if (userdata) { - zval_ptr_dtor(&args[2]); - } zend_hash_iterator_del(ht_iter); return result; } From dfa859f089edb024db7ed73d2d51b399cb617148 Mon Sep 17 00:00:00 2001 From: Niels Dossche <7771979+ndossche@users.noreply.github.com> Date: Sat, 1 Nov 2025 21:39:11 +0100 Subject: [PATCH 2/3] simplify --- ext/standard/array.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/ext/standard/array.c b/ext/standard/array.c index c5ca356427c0..d1a7996ea6b2 100644 --- a/ext/standard/array.c +++ b/ext/standard/array.c @@ -1535,9 +1535,7 @@ static zend_result php_array_walk( /* Call the userland function */ result = zend_call_function(&fci, &context->fci_cache); - if (result == SUCCESS) { - zval_ptr_dtor(&retval); - } + zval_ptr_dtor(&retval); } zval_ptr_dtor_str(&args[1]); From 74b63ac164dafbca8e66ffaceca31f884ef4d5b5 Mon Sep 17 00:00:00 2001 From: Niels Dossche <7771979+ndossche@users.noreply.github.com> Date: Tue, 4 Nov 2025 21:01:12 +0100 Subject: [PATCH 3/3] UPGRADING --- UPGRADING | 1 + 1 file changed, 1 insertion(+) diff --git a/UPGRADING b/UPGRADING index 858e1cffa953..b0c30bc71457 100644 --- a/UPGRADING +++ b/UPGRADING @@ -125,3 +125,4 @@ PHP 8.6 UPGRADE NOTES - Standard: . Improved performance of array_fill_keys(). + . Improved performance of array_walk().