[php-src] Issue #9794: Array unpack inside array literal is slower than array_merge()
| From: | tstarling | Date: | Fri, 21 Oct 2022 03:15:40 +0000 |
| Subject: | [php-src] Issue #9794: Array unpack inside array literal is slower than array_merge() | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-242651@lists.php.net to get a copy of this message | ||
Issue: https://github.com/php/php-src/issues/9794
Author: tstarling
### Description
With PHP 8.1.11:
```
php > $a = range(1,100); $b = range(1,100); $t = microtime(true); for ( $i = 0; $i < 100000;
$i++ ) { $c = [...$a, ...$b]; } print microtime(true)-$t;
0.13379812240601
php > $a = range(1,100); $b = range(1,100); $t = microtime(true); for ( $i = 0; $i < 100000;
$i++ ) { $c = array_merge($a,$b); } print microtime(true)-$t;
0.039999008178711
```
I suspect this is because ZEND_ADD_ARRAY_UNPACK lacks the optimisation for packed arrays that is
present in array_merge():
```
if ((HT_FLAGS(dest) & HASH_FLAG_PACKED) && (HT_FLAGS(src) & HASH_FLAG_PACKED)) {
zend_hash_extend(dest, zend_hash_num_elements(dest) + zend_hash_num_elements(src), 1);
ZEND_HASH_FILL_PACKED(dest) {
```
etc.