[php-src] PR #17002: Faster array initialization

From: Date: Sat, 30 Nov 2024 17:31:16 +0000
Subject: [php-src] PR #17002: Faster array initialization
Groups: php.git-pulls 
Request: Send a blank email to git-pulls+get-29844@lists.php.net to get a copy of this message
Pull Request: https://github.com/php/php-src/pull/17002 Author: iluuu1994 Small idea I had. Instead of constructing arrays with dynamic elements from scratch, clone a template from a constant though ARRAY_DUP, and then populate the missing element slots through ARRAY_SET_PLACEHOLDER. This optimization is only applied when opcache is enabled, because in that case the array template literal is immutable. This allows for a fast clone using memcpy. <details><summary>Benchmarks</summary> <p> All benchmarks are called with: ```php for ($i = 0; $i < 20_000_000; $i++) { test(4); } ``` ```php function test($a) { return [$a, $a]; } // 1% slower // This is the only slower case, we may improve heuristics to default back to INIT_ARRAY and ADD_ARRAY_ELEMENT in this case. ``` ```php function test($a) { return [$a, $a, $a]; } // 2% faster ``` ```php function test($a) { return [1, 2, 3, $a]; } // 33% faster ``` ```php function test($a) { return ['foo' => $a, 'bar' => $a]; } // 10% faster ``` ```php function test($a) { return [100 => $a, $a, $a]; } // 6% faster ``` </p> </details> Unsurprisingly, the biggest benefits can be achieved when most of the array elements are constant. Using constant keys in hashed arrays is also beneficial, since the keys are pre-filled. That said, I did not observe any significant improvements in real applications like Symfony Demo. If we plan for merge this, type inference and SCCP should be improved. Currently, during compilation, placeholders are set to null. This makes it hard to understand whether these are real constants or placeholders that are later removed. We should use some other placeholder value that can be easily distinguished, either _IS_ERROR or some new one.

« previous php.git-pulls (#29844) next »