Improve performance of unpack() with nameless repetitions#18803
Merged
Conversation
We can avoid creating temporary strings, and then reparsing them into
numbers with zend_symtable_update() by using zend_hash_index_update()
directly.
For the following benchmark on an i7-4790:
```php
$file = str_repeat('A', 100000);
for ($i=0;$i<100;$i++) unpack('C*',$file);
```
I get:
```
Benchmark 1: ./sapi/cli/php y.php
Time (mean ± σ): 85.8 ms ± 1.8 ms [User: 74.5 ms, System: 10.4 ms]
Range (min … max): 83.8 ms … 92.4 ms 33 runs
Benchmark 2: ./sapi/cli/php_old y.php
Time (mean ± σ): 318.3 ms ± 2.7 ms [User: 306.7 ms, System: 9.9 ms]
Range (min … max): 314.9 ms … 321.6 ms 10 runs
Summary
./sapi/cli/php y.php ran
3.71 ± 0.08 times faster than ./sapi/cli/php_old y.php
```
On an i7-1185G7 I get:
```
Benchmark 1: ./sapi/cli/php test.php
Time (mean ± σ): 60.1 ms ± 0.7 ms [User: 47.8 ms, System: 12.0 ms]
Range (min … max): 59.2 ms … 63.8 ms 48 runs
Warning: Statistical outliers were detected. Consider re-running this benchmark on a quiet system without any interferences from other programs. It might help to use the '--warmup' or '--prepare' options.
Benchmark 2: ./sapi/cli/php_old test.php
Time (mean ± σ): 220.8 ms ± 2.2 ms [User: 209.6 ms, System: 10.7 ms]
Range (min … max): 218.5 ms … 224.5 ms 13 runs
Summary
./sapi/cli/php test.php ran
3.67 ± 0.06 times faster than ./sapi/cli/php_old test.php
```
Closed
Contributor
|
nice to see the benchmark was useful. thanks for looking into it. 🙏 |
Member
Author
|
Looks like there's a missing null check somewhere, I'll look tomorrow or this evening. |
Girgias
approved these changes
Jun 10, 2025
Member
Girgias
left a comment
There was a problem hiding this comment.
One minor suggestion, but this looks good to me.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
We can avoid creating temporary strings, and then reparsing them into numbers with zend_symtable_update() by using zend_hash_index_update() directly.
For the following benchmark on an i7-4790:
I get:
On an i7-1185G7 I get: