Skip to content

Array unpack inside array literal is slower than array_merge() #9794

Closed
@tstarling

Description

@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.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions