Skip to content

perf(uri): faster authority scan and scheme split - #887

Open
g4lb wants to merge 2 commits into
hyperium:masterfrom
g4lb:perf-uri-authority-scan
Open

g4lb wants to merge 2 commits into
hyperium:masterfrom
g4lb:perf-uri-authority-scan

Conversation

@g4lb

@g4lb g4lb commented Nov 16, 2026

Copy link
Copy Markdown

This PR makes two changes in URI parsing. The output does not change.

  • parse_full uses advance to remove a standard scheme, and truncate to remove the :// of another scheme. This removes the two TODO comments. The old code called split_to and dropped the result. split_to clones the Bytes, but advance does not.
  • validate_authority_bytes uses one 256-entry table. A plain host or userinfo byte needs one lookup. The other bytes go through the same checks as before. The function stays a const fn.
Input Before After Change
Uri: http://localhost:8080/health 39.9 ns 36.1 ns -9.6%
Uri: https://www.example.com/ 36.7 ns 34.3 ns -6.7%
Uri: https://user:pass@example.com:8443/path/to/resource?x=1#frag 56.5 ns 51.1 ns -9.6%
Uri: http://[::1]:3000/graphql 39.0 ns 37.3 ns -4.3%
Uri: ws://echo.example.com/chat 46.3 ns 42.8 ns -7.6%
Uri: www.example.com:443 27.2 ns 24.4 ns -10.6%
Uri::from_static("https://www.example.com/a?b=c") 23.9 ns 18.9 ns -21.0%
Authority: www.example.com:443 19.6 ns 16.5 ns -15.9%
Authority: user:pass@host:8080 20.7 ns 17.1 ns -17.2%
Authority: [2001:db8:85a3::8a2e:370:7334] 32.8 ns 22.6 ns -31.1%
Uri: 20 URIs of different forms 814 ns 753 ns -7.5%
Authority: 7 authorities of different forms 199 ns 141 ns -29.4%
Authority: @:@:@:[::]:1 16.8 ns 17.7 ns +5.5%

The Uri rows use str::parse. The Authority rows use Authority::try_from. The last row has only special bytes. The invalid authority [::1 was also slower: +3.2% (0.1 ns), and +5.4% in a different run. I found no other slower inputs. The origin-form inputs /, * and /path?q=1 did not change (less than 1%).

I used 12 interleaved rounds of criterion and a Mann-Whitney test (Apple Silicon, rustc 1.98.0). I did not measure other architectures.

Checks:

  • cargo test, cargo clippy --all-targets (no new warnings) and cargo fmt --check pass.
  • cargo check passes on rustc 1.57 without dev-dependencies, as in CI, and on wasm32-unknown-unknown.
  • Miri passes on the uri tests.
  • A const that uses Authority::from_static compiles on rustc 1.57.
  • Old and new outputs matched on 1M random inputs. They also matched in 19.5M other checks. These checks use all authorities of 0 to 3 bytes and all byte values at each position of 53 templates. They also use 1M changed copies of real URIs.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant