[php-src] PR #18204: [PoC] Implement some internal functions in plain PHP

From: Date: Mon, 31 Mar 2025 14:56:20 +0000
Subject: [php-src] PR #18204: [PoC] Implement some internal functions in plain PHP
Groups: php.git-pulls 
Request: Send a blank email to git-pulls+get-31260@lists.php.net to get a copy of this message
Pull Request: https://github.com/php/php-src/pull/18204 Author: arnaud-lb This is a very quick PoC of @staabm's idea in https://github.com/php/php-src/pull/17536#issuecomment-2604261583. This PR implements array_find() and related functions in plain PHP, and removes the C implementation. ## Idea As seen in https://github.com/php/php-src/pull/17536, internal functions implemented in C are not necessarily faster than plain PHP ones. Therefore, it may be beneficial to implement internal functions in plain PHP code. Aside from performance, this would have the following benefits: - Users can read the code of internal functions / IDEs can navigate to that code - Simpler implementation - Decrease maintenance cost - Less bugs - Reduce friction when changing the engine or internal APIs - Reduce internal recursion ## Implementation This PR is mostly a mean to demonstrate the concept, and not a proper implementation. For instance, I use opcache.preload to load array.php. Although a proper implementation would use a similar mechanism. The .php files could be either embed in the binary, or installed separately on the file system. Both have pros and cons. ## Benchmarks I'm using the same benchmark script as in https://github.com/php/php-src/pull/17536. Plain PHP implementations are 1.08 times faster than master on my machine: ``` hyperfine '/tmp/base/sapi/cli/php -n -dzend_extension=opcache.so -dopcache.enable_cli=1 /tmp/bench.php' '/tmp/internal-php/sapi/cli/php -n -dzend_extension=opcache.so -dopcache.enable_cli=1 -dopcache.preload=ext/standard/array.php /tmp/bench.php' Benchmark 1: /tmp/base/sapi/cli/php -n -dzend_extension=opcache.so -dopcache.enable_cli=1 /tmp/bench.php Time (mean ± σ): 413.3 ms ± 1.7 ms [User: 408.0 ms, System: 4.5 ms] Range (min … max): 411.3 ms … 416.1 ms 10 runs Benchmark 2: /tmp/internal-php/sapi/cli/php -n -dzend_extension=opcache.so -dopcache.enable_cli=1 -dopcache.preload=ext/standard/array.php /tmp/bench.php Time (mean ± σ): 382.0 ms ± 3.3 ms [User: 376.7 ms, System: 4.5 ms] Range (min … max): 379.2 ms … 390.5 ms 10 runs Summary /tmp/internal-php/sapi/cli/php -n -dzend_extension=opcache.so -dopcache.enable_cli=1 -dopcache.preload=ext/standard/array.php /tmp/bench.php ran 1.08 ± 0.01 times faster than /tmp/base/sapi/cli/php -n -dzend_extension=opcache.so -dopcache.enable_cli=1 /tmp/bench.php ``` When JIT is enabled in both branches, this branch is 2.30 times faster than master: ``` hyperfine '/tmp/base/sapi/cli/php -n -dzend_extension=opcache.so -dopcache.enable_cli=1 -dopcache.jit=tracing -dopcache.jit_buffer_size=100m /tmp/bench.php' '/tmp/internal-php/sapi/cli/php -n -dzend_extension=opcache.so -dopcache.enable_cli=1 -dopcache.jit=tracing -dopcache.jit_buffer_size=100m -dopcache.preload=ext/standard/array.php /tmp/bench.php' Benchmark 1: /tmp/base/sapi/cli/php -n -dzend_extension=opcache.so -dopcache.enable_cli=1 -dopcache.jit=tracing -dopcache.jit_buffer_size=100m /tmp/bench.php Time (mean ± σ): 360.8 ms ± 2.0 ms [User: 356.2 ms, System: 3.9 ms] Range (min … max): 357.9 ms … 363.3 ms 10 runs Benchmark 2: /tmp/internal-php/sapi/cli/php -n -dzend_extension=opcache.so -dopcache.enable_cli=1 -dopcache.jit=tracing -dopcache.jit_buffer_size=100m -dopcache.preload=ext/standard/array.php /tmp/bench.php Time (mean ± σ): 156.0 ms ± 0.8 ms [User: 150.2 ms, System: 5.3 ms] Range (min … max): 154.7 ms … 157.8 ms 19 runs Summary /tmp/internal-php/sapi/cli/php -n -dzend_extension=opcache.so -dopcache.enable_cli=1 -dopcache.jit=tracing -dopcache.jit_buffer_size=100m -dopcache.preload=ext/standard/array.php /tmp/bench.php ran 2.31 ± 0.02 times faster than /tmp/base/sapi/cli/php -n -dzend_extension=opcache.so -dopcache.enable_cli=1 -dopcache.jit=tracing -dopcache.jit_buffer_size=100m /tmp/bench.php ``` This is because JIT is able to inline the callback into array_find() in this benchmark, eliminating the function call overhead entirely, which is not possible with the C implementation. (JIT heuristics would probably blacklist some trace entry points after array_find() is used with many different callbacks, but this is something we can tune.) In general, plain PHP code gives more optimizations opportunities to the JIT.

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