Skip to content

Commit 7de451f

Browse files
iliaalTimWolla
andauthored
uri: Do not copy and normalize already-normalized URIs for uri_parser_rfc3986 (#21726)
When Uri\Rfc3986\Uri::parse() produces a URI already in canonical form (the common case: http/https URLs with no uppercase host, no percent-encoding in unreserved ranges, no ".." path segments), get_normalized_uri() no longer deep-copies the parsed struct and runs a full normalization pass. It calls uriNormalizeSyntaxMaskRequiredExA once to compute the dirty mask; a zero mask means we alias the raw uri. The struct caches the dirty mask, so multiple non-raw reads on the same instance only run the scan once. Fallback: when the mask is nonzero, we copy and normalize as before, but only for the flagged components (uriNormalizeSyntaxExMmA(..., dirty_mask, ...) instead of (..., -1, ...)). Measurements on a 17-URL mix with a realistic parse-and-read workload (10 runs of 1.7M parses each, CPU pinned via taskset, same-session stash-pop A/B so both builds share machine state): baseline mean optimized mean delta parse only 0.3992s (4.26M/s) 0.4083s (4.16M/s) noise parse + 1 read 0.6687s (2.54M/s) 0.5464s (3.11M/s) -18.3% parse + 7 reads 0.8510s (2.00M/s) 0.7305s (2.33M/s) -14.2% The "parse + 1 read" row isolates the first-read cost where this change lands. The "parse + 7 reads" row shows the amortized effect under a realistic user pattern: the first getter pays the reduced normalization cost, and the remaining six getters hit the cached normalized uri and cost the same as before. hyperfine cross-check on the whole benchmark script, 15 runs each: baseline 20.471 s +/- 1.052 s [19.535 .. 22.985] optimized 17.240 s +/- 0.540 s [16.556 .. 18.190] optimized runs 1.19 +/- 0.07 times faster. All 309 tests in ext/uri/tests pass. I checked that URIs needing normalization (http://EXAMPLE.com/A/%2e%2e/c resolving to /c) still hit the full normalize path through the nonzero dirty mask. Co-authored-by: Tim Düsterhus <tim@bastelstu.be>
1 parent 8172b7e commit 7de451f

2 files changed

Lines changed: 17 additions & 4 deletions

File tree

UPGRADING

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,10 @@ PHP 8.6 UPGRADE NOTES
448448
. Improved performance of str_split().
449449

450450
- URI:
451-
. Reduced allocations when reading RFC3986 IPv6/IPFuture hosts and paths.
451+
. Reduced allocations when reading IPv6/IPFuture hosts and paths with
452+
Uri\Rfc3986\Uri.
453+
. Improved performance and memory consumption when using normalizing
454+
(non-raw) getters on already-normalized URIs with Uri\Rfc3986\Uri.
452455

453456
- Zip:
454457
. Avoid string copies in ZipArchive::addFromString().

ext/uri/uri_parser_rfc3986.c

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
struct php_uri_parser_rfc3986_uris {
2525
UriUriA uri;
2626
UriUriA normalized_uri;
27+
unsigned int normalization_mask;
2728
bool normalized_uri_initialized;
2829
};
2930

@@ -84,12 +85,21 @@ ZEND_ATTRIBUTE_NONNULL static void copy_uri(UriUriA *new_uriparser_uri, const Ur
8485

8586
ZEND_ATTRIBUTE_NONNULL static UriUriA *get_normalized_uri(php_uri_parser_rfc3986_uris *uriparser_uris) {
8687
if (!uriparser_uris->normalized_uri_initialized) {
87-
copy_uri(&uriparser_uris->normalized_uri, &uriparser_uris->uri);
88-
int result = uriNormalizeSyntaxExMmA(&uriparser_uris->normalized_uri, (unsigned int)-1, mm);
89-
ZEND_ASSERT(result == URI_SUCCESS);
88+
int mask_result = uriNormalizeSyntaxMaskRequiredExA(&uriparser_uris->uri, &uriparser_uris->normalization_mask);
89+
ZEND_ASSERT(mask_result == URI_SUCCESS);
90+
91+
if (uriparser_uris->normalization_mask != URI_NORMALIZED) {
92+
copy_uri(&uriparser_uris->normalized_uri, &uriparser_uris->uri);
93+
int result = uriNormalizeSyntaxExMmA(&uriparser_uris->normalized_uri, uriparser_uris->normalization_mask, mm);
94+
ZEND_ASSERT(result == URI_SUCCESS);
95+
}
9096
uriparser_uris->normalized_uri_initialized = true;
9197
}
9298

99+
if (uriparser_uris->normalization_mask == URI_NORMALIZED) {
100+
return &uriparser_uris->uri;
101+
}
102+
93103
return &uriparser_uris->normalized_uri;
94104
}
95105

0 commit comments

Comments
 (0)