[php-src] PR #21550: Optimize string allocation in `uri_parser_rfc3986` when dealing with IPv6 hosts
| From: | LamentXU123 | Date: | Fri, 27 Mar 2026 11:40:46 +0000 |
| Subject: | [php-src] PR #21550: Optimize string allocation in `uri_parser_rfc3986` when dealing with IPv6 hosts | ||
| Groups: | php.git-pulls | ||
| Request: | Send a blank email to git-pulls+get-35388@lists.php.net to get a copy of this message | ||
Pull Request: https://github.com/php/php-src/pull/21550
Author: LamentXU123
Currently,
php_uri_parser_rfc3986_host_read and
php_uri_parser_rfc3986_path_read use the smart_str API to build the host and path
strings. I think smart_str_append* introduces unnecessary overhead here, since it use
repetitive boundary checks and dynamic memory reallocations.
This PR optimize string allocation in uri_parser_rfc3986 by replacing
smart_str with pre-calculated zend_string_alloc. Focusing on two main
functions, php_uri_parser_rfc3986_host_read() and
php_uri_parser_rfc3986_path_read(), which effect getHost() and
getrawHost()
for php_uri_parser_rfc3986_host_read()
This PR constructs the IPv6/IPFuture host directly using a fixed-length zend_string (formatted as [
+ hostText + ] + \0). This replaces the previous smart_str appending process.
for php_uri_parser_rfc3986_path_read()
This PR first traverses the segments to pre-calculate the total length (total_len). This includes
the leading / and segment delimiters. Then it performs a single zend_string_alloc() and populates
the path content and delimiters using memcpy, and finally append the \0 terminator.
Benchmark script: [bench.php](https://github.com/user-attachments/files/26302728/bench.php)
for getHost()
```
└─$ php ../../bench.php --old ~/Desktop/php/php-raw/php-src-master/sapi/cli/php --new
~/Desktop/php/opt-3/php-src-master/sapi/cli/php --iters 20000000 --mode host
-------------------------------------------------------
| Test | old avg(ns) | new avg(ns) | diff(%) |
-------------------------------------------------------
| ipv6_short | 714 | 656 | 8.12% |
-------------------------------------------------------
| ipv6_full | 806 | 746 | 7.44% |
-------------------------------------------------------
| ipv6_loopback | 743 | 671 | 9.69% |
-------------------------------------------------------
| ipv6_mixed | 944 | 861 | 8.79% |
-------------------------------------------------------
```
for getrawHost()
```
└─$ php ../../bench.php --old ~/Desktop/php/php-raw/php-src-master/sapi/cli/php --new
~/Desktop/php/opt-3/php-src-master/sapi/cli/php --iters 20000000 --mode raw_host
-------------------------------------------------------
| Test | old avg(ns) | new avg(ns) | diff(%) |
-------------------------------------------------------
| ipv6_short | 557 | 509 | 8.62% |
-------------------------------------------------------
| ipv6_full | 621 | 572 | 7.89% |
-------------------------------------------------------
| ipv6_loopback | 570 | 550 | 3.51% |
-------------------------------------------------------
| ipv6_mixed | 667 | 679 | -1.80% |
-------------------------------------------------------
```