Skip to content

Improve performance of urldecode() and rawurldecode() #18378

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 2 commits into
base: master
Choose a base branch
from

Conversation

nielsdos
Copy link
Member

There are two hot spots on my machines:

  1. We copy the string because the internal PHP API works in-place.
  2. The conversion of hex characters is slow due to going through the C locale handling.

This patch resolves the first hot spots by introducing 2 new internal APIs that avoid the redundant copy and allocate an empty string upfront. The second hotspot is resolved by having a specialised htoi handler.

For the following benchmark:

$encoded = "Hello%20World%21+This%20is%20a%20test%3A%20%40%23%24%25%5E%26*%28%29";
for ($i=0;$i<2000000;$i++) {
  rawurldecode($encoded);
  urldecode($encoded);
}

On an i7-4790:

Benchmark 1: ./sapi/cli/php x.php
  Time (mean ± σ):     364.8 ms ±   3.7 ms    [User: 359.9 ms, System: 3.3 ms]
  Range (min … max):   359.9 ms … 372.0 ms    10 runs

Benchmark 2: ./sapi/cli/php_old x.php
  Time (mean ± σ):     565.5 ms ±   4.9 ms    [User: 561.8 ms, System: 2.5 ms]
  Range (min … max):   560.7 ms … 578.2 ms    10 runs

Summary
  ./sapi/cli/php x.php ran
    1.55 ± 0.02 times faster than ./sapi/cli/php_old x.php

On an i7-1185G7:

Benchmark 1: ./sapi/cli/php x.php
  Time (mean ± σ):     708.8 ms ±   6.1 ms    [User: 701.4 ms, System: 6.3 ms]
  Range (min … max):   701.9 ms … 722.3 ms    10 runs

Benchmark 2: ./sapi/cli/php_old x.php
  Time (mean ± σ):      1.311 s ±  0.019 s    [User: 1.300 s, System: 0.008 s]
  Range (min … max):    1.281 s …  1.348 s    10 runs

Summary
  ./sapi/cli/php x.php ran
    1.85 ± 0.03 times faster than ./sapi/cli/php_old x.php

There are two hot spots on my machines:
1. We copy the string because the internal PHP API works in-place.
2. The conversion of hex characters is slow due to going through the C
   locale handling.

This patch resolves the first hot spots by introducing 2 new internal
APIs that avoid the redundant copy and allocate an empty string upfront.
The second hotspot is resolved by having a specialised htoi handler.

For the following benchmark:
```php
$encoded = "Hello%20World%21+This%20is%20a%20test%3A%20%40%23%24%25%5E%26*%28%29";
for ($i=0;$i<2000000;$i++) {
  rawurldecode($encoded);
  urldecode($encoded);
}
```

On an i7-4790:
```
Benchmark 1: ./sapi/cli/php x.php
  Time (mean ± σ):     364.8 ms ±   3.7 ms    [User: 359.9 ms, System: 3.3 ms]
  Range (min … max):   359.9 ms … 372.0 ms    10 runs

Benchmark 2: ./sapi/cli/php_old x.php
  Time (mean ± σ):     565.5 ms ±   4.9 ms    [User: 561.8 ms, System: 2.5 ms]
  Range (min … max):   560.7 ms … 578.2 ms    10 runs

Summary
  ./sapi/cli/php x.php ran
    1.55 ± 0.02 times faster than ./sapi/cli/php_old x.php
```

On an i7-1185G7:
```
Benchmark 1: ./sapi/cli/php x.php
  Time (mean ± σ):     708.8 ms ±   6.1 ms    [User: 701.4 ms, System: 6.3 ms]
  Range (min … max):   701.9 ms … 722.3 ms    10 runs

Benchmark 2: ./sapi/cli/php_old x.php
  Time (mean ± σ):      1.311 s ±  0.019 s    [User: 1.300 s, System: 0.008 s]
  Range (min … max):    1.281 s …  1.348 s    10 runs

Summary
  ./sapi/cli/php x.php ran
    1.85 ± 0.03 times faster than ./sapi/cli/php_old x.php
```
@devnexen
Copy link
Member

oh my ... that s a lot :)

@nielsdos nielsdos marked this pull request as ready for review April 21, 2025 13:28
@nielsdos nielsdos requested a review from bukka as a code owner April 21, 2025 13:28
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.

3 participants