Skip to content

Commit 4762d46

Browse files
committed
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.
1 parent 1eadf55 commit 4762d46

File tree

1 file changed

+2
-1
lines changed

1 file changed

+2
-1
lines changed

ext/standard/array.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7046,9 +7046,10 @@ PHP_FUNCTION(array_chunk)
70467046

70477047
/* If reached the chunk size, add it to the result array, and reset the
70487048
* pointer. */
7049-
if (!(++current % size)) {
7049+
if (++current == size) {
70507050
add_next_index_zval(return_value, &chunk);
70517051
ZVAL_UNDEF(&chunk);
7052+
current = 0;
70527053
}
70537054
} ZEND_HASH_FOREACH_END();
70547055

0 commit comments

Comments
 (0)