[php-src] PR #12651: Improve browscap get_browser performance
| From: | nielsdos | Date: | Fri, 10 Nov 2023 22:35:26 +0000 |
| Subject: | [php-src] PR #12651: Improve browscap get_browser performance | ||
| Groups: | php.git-pulls | ||
| Request: | Send a blank email to git-pulls+get-24148@lists.php.net to get a copy of this message | ||
Pull Request: https://github.com/php/php-src/pull/12651
Author: nielsdos
This set of patches improves the browscap support performance. The changes are split in atomic
commits to make review easier. Sometimes the commits also contain a description. The most important
change here is moving away from regexes and using a specialised wildcard matching algorithm (best
case O(n), worst case O(n²)). This gives the most speed-up especially as compilation and conversion
to regexes is prevented. The algorithm has the same execution time complexity as the regex.
With the following 3 test scenarios:
### (1) Unknown data lookup
```php
for ($i = 0; $i < 10000; $i++) {
get_browser("Unknown Browser 1.0");
}
```
### (2) Test all known user agents we test for
```php
$user_agents = file('ext/standard/tests/misc/user_agents.txt', FILE_IGNORE_NEW_LINES);
foreach ($user_agents as $ua) {
get_browser($ua, true);
}
```
### (3) Result construction
```php
for ($i = 0; $i < 200000; $i++) {
get_browser("Chromium 123.0");
}
```
## Benchmark results
For (1):
```
Benchmark 1: ./sapi/cli/php bench.php
Time (mean ± σ): 331.0 ms ± 3.9 ms [User: 325.9 ms, System: 4.2 ms]
Range (min … max): 325.7 ms … 339.3 ms 10 runs
Benchmark 2: ./sapi/cli/php_old bench.php
Time (mean ± σ): 478.2 ms ± 4.3 ms [User: 472.6 ms, System: 4.2 ms]
Range (min … max): 472.9 ms … 486.1 ms 10 runs
Summary
./sapi/cli/php bench.php ran
1.44 ± 0.02 times faster than ./sapi/cli/php_old bench.php
```
For (2):
```
Benchmark 1: ./sapi/cli/php bench.php
Time (mean ± σ): 243.6 ms ± 3.9 ms [User: 237.2 ms, System: 5.8 ms]
Range (min … max): 239.5 ms … 252.2 ms 12 runs
Benchmark 2: ./sapi/cli/php_old bench.php
Time (mean ± σ): 618.5 ms ± 4.1 ms [User: 603.4 ms, System: 12.7 ms]
Range (min … max): 614.2 ms … 626.4 ms 10 runs
Summary
./sapi/cli/php bench.php ran
2.54 ± 0.04 times faster than ./sapi/cli/php_old bench.php
```
For (3):
```
Benchmark 1: ./sapi/cli/php bench.php
Time (mean ± σ): 89.3 ms ± 4.6 ms [User: 85.4 ms, System: 3.5 ms]
Range (min … max): 85.5 ms … 101.5 ms 33 runs
Benchmark 2: ./sapi/cli/php_old bench.php
Time (mean ± σ): 144.9 ms ± 4.7 ms [User: 139.4 ms, System: 5.1 ms]
Range (min … max): 140.8 ms … 162.2 ms 20 runs
Summary
./sapi/cli/php bench.php ran
1.62 ± 0.10 times faster than ./sapi/cli/php_old bench.php
```