[php-src] PR #11166: Implement implicit move optimisation

From: Date: Sun, 30 Apr 2023 15:37:09 +0000
Subject: [php-src] PR #11166: Implement implicit move optimisation
Groups: php.git-pulls 
Request: Send a blank email to git-pulls+get-22413@lists.php.net to get a copy of this message
Pull Request: https://github.com/php/php-src/pull/11166 Author: nielsdos ## Introduction This implements a more generic version of the in-place modification I first tried in https://github.com/php/php-src/pull/11060. We decided to limit that PR to RC1 optimisations of some array functions only, and not do the in-place $variable optimisation in that way because of issues. This patch overcomes those issues and builds on the previous patch. With this patch, any internal function that supports RC1 optimisations automatically gets the optimisation for in-place variable modifications. Contrary to the previous approach, this is compatible with exceptions. Furthermore, this approach also allows userland functions to benefit from this optimisation. e.g. the following code will not take a copy of the array with this patch, whereas previously it would due to the copy-on-write characteristic of arrays: ``` function foo($array) { $array[1] = 1; } function bar() { $array = ...; $array = foo($array); } ``` Right now the impact on the benchmark suite isn't that high. The reason is that only a handful of functions within PHP optimise for RC1 cases, and the array sizes for those cases are fairly small. When more support for these cases are added, the benefit from this patch will increase. I've added a micro benchmark for array operations that shows the effect of this optimisation. ## Implementation The optimiser already tracks which SSA variables have a value that doesn't matter with the no_val field. By changing ZEND_SEND_VAR to redefine op1, we automatically know if the variable will ever be used again without being overwritten by looking at the no_val field. If the no_val field is set, the variable may hold a string/array and the refcount may be 1, we set a flag on the ZEND_SEND_VAR(_EX) opline to indicate that it may avoid a copy. The flag is stored in extended_value. There are two new VM type spec handlers for this that check for the flag: one for ZEND_SEND_VAR and one for ZEND_SEND_VAR_EX. ## Limitations * The optimisation isn't performed on arguments passed to other functions. This is because the optimisation would be externally visible through backtraces, which is undesirable. Unfortunately, this is also the case where a lot of optimisation opportunity lies. Nonetheless, even with this limitation it seems like the optimisation can help a lot. * The optimisation does not apply to functions using indirect variable access (e.g. variable-variables, compact()) and vararg functions. ## Benchmark results Zend/array_micro_bench.php *without* this optimisation: ``` empty_loop 0.000 array_merge 3.405 3.405 array_merge temp 2.796 2.796 array_merge nest 2.720 2.719 array_unique 2.014 2.014 user_func 0.039 0.039 ------------------------ Total 10.975 ``` Zend/array_micro_bench.php *with* this optimisation: ``` empty_loop 0.000 array_merge 0.002 0.002 array_merge temp 1.181 1.181 array_merge nest 2.710 2.710 array_unique 1.973 1.973 user_func 0.024 0.024 ------------------------ Total 5.891 ``` As you can see there is virtually no difference for the array_merge nest case. This is expected: the optimisation cannot be done because the original array is still required for the addition *after* the array_merge function is executed. However, for the other cases there is a notable difference. The performance of array_unique is dominated by sorting, so we don't see a very big win there, but still a win. For user_func we see a small win, but the total time is not a lot to begin with because the array is small. I can see a nice benefit from this optimisation in data-heavy processes (e.g. ETL processes). ## Acknowledgements This optimisation is inspired by C++'s & Rust's "move semantics", hence the name: implicitly adding moves. During the development of this patch I had some conversations about the implementation with @iluuu1994 , so I would like to thank him as well.

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