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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

nielsdos
Copy link
Member

@nielsdos nielsdos commented Apr 22, 2025

See individual commits.

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 marked this pull request as ready for review April 26, 2025 13:32
@@ -100,7 +100,7 @@ MBSTRING_API SAPI_TREAT_DATA_FUNC(mbstr_treat_data)
case PARSE_POST:
case PARSE_GET:
case PARSE_STRING:
separator = (char *) estrdup(PG(arg_separator).input);
separator = (char *) estrndup(ZSTR_VAL(PG(arg_separator).input), ZSTR_LEN(PG(arg_separator).input));
Copy link
Contributor

Choose a reason for hiding this comment

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

I think doesn't need a char * cast.

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.

4 participants