[php-src] PR #19658: re-use `sprintf()` optimisation for `printf()`
| From: | realFlowControl | Date: | Mon, 01 Sep 2025 11:21:11 +0000 |
| Subject: | [php-src] PR #19658: re-use `sprintf()` optimisation for `printf()` | ||
| Groups: | php.git-pulls | ||
| Request: | Send a blank email to git-pulls+get-33022@lists.php.net to get a copy of this message | ||
Pull Request: https://github.com/php/php-src/pull/19658
Author: realFlowControl
This PR piggybacks on https://github.com/php/php-src/pull/14546
test.php:
```php
<?php
$a = 'foo';
$b = 'bar';
ob_start();
for ($i = 0; $i < 100_000_000; $i++) {
printf("%s-%s", $a, $b);
ob_clean();
}
ob_end_clean();
```
See hyperfine run, where php-orig is without this patch:
```
hyperfine './php-orig -d opcache.enable_cli=1 test.php' './sapi/cli/php -d
opcache.enable_cli=1 test.php'
Benchmark 1: ./php-orig -d opcache.enable_cli=1 test.php
Time (mean ± σ): 10.490 s ± 0.053 s [User: 10.421 s, System: 0.038 s]
Range (min … max): 10.443 s … 10.621 s 10 runs
Benchmark 2: ./sapi/cli/php -d opcache.enable_cli=1 test.php
Time (mean ± σ): 8.207 s ± 0.135 s [User: 8.100 s, System: 0.035 s]
Range (min … max): 8.077 s … 8.489 s 10 runs
```
The generated opcodes before optimisation:
```
./sapi/cli/php -d opcache.enable_cli=1 -d opcache.opt_debug_level=0x10000 test.php
$_main:
; (lines=21, args=0, vars=3, tmps=13)
; (before optimizer)
; /usr/local/src/php/php-src/test.php:1-12
; return [] RANGE[0..0]
0000 ASSIGN CV0($a) string("foo")
0001 ASSIGN CV1($b) string("bar")
0002 INIT_FCALL 0 80 string("ob_start")
0003 DO_ICALL
0004 ASSIGN CV2($i) int(0)
0005 JMP 0016
0006 T8 = ROPE_INIT 3 CV0($a)
0007 T8 = ROPE_ADD 1 T8 string("-")
0008 T7 = ROPE_END 2 T8 CV1($b)
0009 T10 = COPY_TMP T7
0010 ECHO T7
0011 T11 = STRLEN T10
0012 FREE T11
0013 INIT_FCALL 0 80 string("ob_clean")
0014 DO_ICALL
0015 PRE_INC CV2($i)
0016 T14 = IS_SMALLER CV2($i) int(100000000)
0017 JMPNZ T14 0006
0018 INIT_FCALL 0 80 string("ob_end_clean")
0019 DO_ICALL
0020 RETURN int(1)
```
... and after optimisation:
```
./sapi/cli/php -d opcache.enable_cli=1 -d opcache.opt_debug_level=0x20000 test.php
$_main:
; (lines=18, args=0, vars=3, tmps=3)
; (after optimizer)
; /usr/local/src/php/php-src/test.php:1-12
0000 ASSIGN CV0($a) string("foo")
0001 ASSIGN CV1($b) string("bar")
0002 INIT_FCALL 0 80 string("ob_start")
0003 DO_ICALL
0004 ASSIGN CV2($i) int(0)
0005 JMP 0013
0006 T4 = ROPE_INIT 3 CV0($a)
0007 T4 = ROPE_ADD 1 T4 string("-")
0008 T3 = ROPE_END 2 T4 CV1($b)
0009 ECHO T3
0010 INIT_FCALL 0 80 string("ob_clean")
0011 DO_ICALL
0012 PRE_INC CV2($i)
0013 T3 = IS_SMALLER CV2($i) int(100000000)
0014 JMPNZ T3 0006
0015 INIT_FCALL 0 80 string("ob_end_clean")
0016 DO_ICALL
0017 RETURN int(1)
```