[php-src] PR #14546: zend_compile: Optimize `sprintf()` into a rope

From: Date: Wed, 12 Jun 2024 12:23:03 +0000
Subject: [php-src] PR #14546: zend_compile: Optimize `sprintf()` into a rope
Groups: php.git-pulls 
Request: Send a blank email to git-pulls+get-26646@lists.php.net to get a copy of this message
Pull Request: https://github.com/php/php-src/pull/14546 Author: TimWolla This optimization will compile sprintf() using only %s placeholders into a rope at compile time, effectively making those calls equivalent to the use of string interpolation, with the added benefit of supporting arbitrary expressions instead of just expressions starting with a $. For a synthetic test using: ```php <?php $a = 'foo'; $b = 'bar'; for ($i = 0; $i < 100_000_000; $i++) { sprintf("%s-%s", $a, $b); } ``` This optimization yields a 2.1× performance improvement: $ hyperfine 'sapi/cli/php -d zend_extension=php-src/modules/opcache.so -d opcache.enable_cli=1 test.php' \ '/tmp/unoptimized -d zend_extension=php-src/modules/opcache.so -d opcache.enable_cli=1 test.php' Benchmark 1: sapi/cli/php -d zend_extension=php-src/modules/opcache.so -d opcache.enable_cli=1 test.php Time (mean ± σ): 1.869 s ± 0.033 s [User: 1.865 s, System: 0.003 s] Range (min … max): 1.840 s … 1.945 s 10 runs Benchmark 2: /tmp/unoptimized -d zend_extension=php-src/modules/opcache.so -d opcache.enable_cli=1 test.php Time (mean ± σ): 4.011 s ± 0.034 s [User: 4.006 s, System: 0.005 s] Range (min … max): 3.964 s … 4.079 s 10 runs Summary sapi/cli/php -d zend_extension=php-src/modules/opcache.so -d opcache.enable_cli=1 test.php ran 2.15 ± 0.04 times faster than /tmp/unoptimized -d zend_extension=php-src/modules/opcache.so -d opcache.enable_cli=1 test.php This optimization comes with a small and probably insignificant behavioral change: If one of the values cannot be (cleanly) converted to a string, for example when attempting to insert an object that is not Stringable, the resulting Exception will naturally not show the sprintf() call in the resulting stack trace, because there is no call to sprintf(). Nevertheless it will correctly point out the line of the sprintf() call as the source of the Exception, pointing the user towards the correct location. ----------- I'd like to thank Ilija for his preliminary review and great feedback during development of this :smiley:

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