Commit 017d1df
committed
Improve performance of urldecode() and rawurldecode()
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
```1 parent 68d5403 commit 017d1df
3 files changed
+47
-28
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
61 | 61 | | |
62 | 62 | | |
63 | 63 | | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
64 | 68 | | |
65 | 69 | | |
66 | 70 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
411 | 411 | | |
412 | 412 | | |
413 | 413 | | |
| 414 | + | |
| 415 | + | |
| 416 | + | |
| 417 | + | |
| 418 | + | |
| 419 | + | |
| 420 | + | |
414 | 421 | | |
415 | | - | |
| 422 | + | |
416 | 423 | | |
417 | 424 | | |
418 | | - | |
| 425 | + | |
419 | 426 | | |
420 | 427 | | |
421 | | - | |
422 | | - | |
423 | | - | |
| 428 | + | |
424 | 429 | | |
425 | 430 | | |
426 | | - | |
427 | | - | |
428 | | - | |
| 431 | + | |
429 | 432 | | |
430 | 433 | | |
431 | 434 | | |
| |||
571 | 574 | | |
572 | 575 | | |
573 | 576 | | |
574 | | - | |
575 | | - | |
| 577 | + | |
| 578 | + | |
576 | 579 | | |
577 | 580 | | |
578 | 581 | | |
579 | 582 | | |
580 | 583 | | |
581 | | - | |
582 | | - | |
| 584 | + | |
583 | 585 | | |
584 | | - | |
585 | | - | |
| 586 | + | |
| 587 | + | |
586 | 588 | | |
587 | | - | |
| 589 | + | |
588 | 590 | | |
589 | 591 | | |
590 | 592 | | |
591 | | - | |
| 593 | + | |
592 | 594 | | |
593 | 595 | | |
594 | 596 | | |
595 | | - | |
| 597 | + | |
596 | 598 | | |
597 | 599 | | |
598 | 600 | | |
599 | 601 | | |
600 | 602 | | |
601 | 603 | | |
602 | 604 | | |
603 | | - | |
| 605 | + | |
| 606 | + | |
| 607 | + | |
| 608 | + | |
| 609 | + | |
| 610 | + | |
| 611 | + | |
604 | 612 | | |
605 | 613 | | |
606 | 614 | | |
| |||
633 | 641 | | |
634 | 642 | | |
635 | 643 | | |
636 | | - | |
637 | | - | |
| 644 | + | |
| 645 | + | |
638 | 646 | | |
639 | 647 | | |
640 | 648 | | |
641 | 649 | | |
642 | 650 | | |
643 | | - | |
644 | | - | |
| 651 | + | |
645 | 652 | | |
646 | | - | |
647 | | - | |
| 653 | + | |
| 654 | + | |
648 | 655 | | |
649 | | - | |
650 | | - | |
| 656 | + | |
| 657 | + | |
651 | 658 | | |
652 | 659 | | |
653 | 660 | | |
654 | | - | |
| 661 | + | |
655 | 662 | | |
656 | 663 | | |
657 | 664 | | |
658 | 665 | | |
659 | 666 | | |
660 | 667 | | |
661 | 668 | | |
662 | | - | |
| 669 | + | |
| 670 | + | |
| 671 | + | |
| 672 | + | |
| 673 | + | |
| 674 | + | |
| 675 | + | |
663 | 676 | | |
664 | 677 | | |
665 | 678 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
33 | 33 | | |
34 | 34 | | |
35 | 35 | | |
| 36 | + | |
36 | 37 | | |
| 38 | + | |
37 | 39 | | |
38 | 40 | | |
39 | 41 | | |
| |||
0 commit comments