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.
This commit is contained in:
Niels Dossche 2025-07-20 15:55:59 +02:00
parent 1eadf553f1
commit 4762d46427

View file

@ -7046,9 +7046,10 @@ PHP_FUNCTION(array_chunk)
/* If reached the chunk size, add it to the result array, and reset the /* If reached the chunk size, add it to the result array, and reset the
* pointer. */ * pointer. */
if (!(++current % size)) { if (++current == size) {
add_next_index_zval(return_value, &chunk); add_next_index_zval(return_value, &chunk);
ZVAL_UNDEF(&chunk); ZVAL_UNDEF(&chunk);
current = 0;
} }
} ZEND_HASH_FOREACH_END(); } ZEND_HASH_FOREACH_END();