From 4762d46427fc5b654ee4c99ff4c3b104ab01edf2 Mon Sep 17 00:00:00 2001 From: Niels Dossche <7771979+nielsdos@users.noreply.github.com> Date: Sun, 20 Jul 2025 15:55:59 +0200 Subject: [PATCH] Avoid modulo operation in loop in array_chunk For this benchmark: ```php $length = 25; for ($i=0;$i<1000;$i++) array_chunk(range(0, 10000), $length); ``` On an i7-4790, length=25 speeds up by 1.8x and length=1 by 1.27x. On an i7-1185G7, length=25 speeds up by 1.08x and length=1 by 1.02x. --- ext/standard/array.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/ext/standard/array.c b/ext/standard/array.c index 522e7f715ac..6de78c3900a 100644 --- a/ext/standard/array.c +++ b/ext/standard/array.c @@ -7046,9 +7046,10 @@ PHP_FUNCTION(array_chunk) /* If reached the chunk size, add it to the result array, and reset the * pointer. */ - if (!(++current % size)) { + if (++current == size) { add_next_index_zval(return_value, &chunk); ZVAL_UNDEF(&chunk); + current = 0; } } ZEND_HASH_FOREACH_END();