Skip to content

Improve http_build_query() performance #18401

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 21, 2025
Merged

Conversation

nielsdos
Copy link
Member

@nielsdos nielsdos commented Apr 22, 2025

See individual commits.

@nielsdos nielsdos requested a review from arnaud-lb May 20, 2025 17:32
Copy link
Member

@arnaud-lb arnaud-lb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me apart from one nit

nielsdos added 4 commits May 21, 2025 18:48
This allows us to avoid a call to `zend_ini_str` which took 6% of the
profile on my i7-4790 for a call to `http_build_query`. Now we can just
grab the value from the globals.
In other files this can avoid some length recomputations.
This avoids some assembly code bloat.
This avoids temporary allocations and some copies.

For this benchmark:
```php
for ($i=0;$i<2000000;$i++) {
  http_build_query([999999 => 'foo', 'aaab' => 'def', 'aaaaa'=>1, 'aaaaaaaa' => 'a']);
}
```

On an i7-4790:
```
Benchmark 1: ./sapi/cli/php ../buildquery.php
  Time (mean ± σ):     298.9 ms ±   7.3 ms    [User: 295.6 ms, System: 2.3 ms]
  Range (min … max):   293.6 ms … 314.0 ms    10 runs

Benchmark 2: ./sapi/cli/php_old ../buildquery.php
  Time (mean ± σ):     594.8 ms ±   8.6 ms    [User: 590.8 ms, System: 2.4 ms]
  Range (min … max):   586.3 ms … 616.1 ms    10 runs

Summary
  ./sapi/cli/php ../buildquery.php ran
    1.99 ± 0.06 times faster than ./sapi/cli/php_old ../buildquery.php
```

For this benchmark:
```php
for ($i=0;$i<2000000;$i++) {
  http_build_query(['test' => 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa']);
}
```

On an i7-4790:
```
Benchmark 1: ./sapi/cli/php ../buildquery.php
  Time (mean ± σ):     188.4 ms ±   6.7 ms    [User: 184.6 ms, System: 2.9 ms]
  Range (min … max):   182.0 ms … 205.4 ms    14 runs

Benchmark 2: ./sapi/cli/php_old ../buildquery.php
  Time (mean ± σ):     323.9 ms ±   8.7 ms    [User: 319.8 ms, System: 2.7 ms]
  Range (min … max):   318.0 ms … 341.2 ms    10 runs

Summary
  ./sapi/cli/php ../buildquery.php ran
    1.72 ± 0.08 times faster than ./sapi/cli/php_old ../buildquery.php
```
@nielsdos nielsdos merged commit cd43536 into php:master May 21, 2025
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants