LibURL: Avoid expensive IDNA::to_ascii() for all-ASCII domain strings

20% of CPU usage when loading https://utah.edu/ was spent doing these
ASCII conversions in URL parsing.
This commit is contained in:
Andreas Kling 2024-04-05 21:26:52 +02:00 committed by Andrew Kaster
parent 520f6ac92a
commit d568b15acf
Notes: sideshowbarker 2024-07-17 06:40:21 +09:00

View file

@ -580,6 +580,15 @@ static ErrorOr<String> domain_to_ascii(StringView domain, bool be_strict)
{
// 1. Let result be the result of running Unicode ToASCII with domain_name set to domain, UseSTD3ASCIIRules set to beStrict, CheckHyphens set to false, CheckBidi set to true, CheckJoiners set to true, Transitional_Processing set to false, and VerifyDnsLength set to beStrict. [UTS46]
// 2. If result is a failure value, domain-to-ASCII validation error, return failure.
// OPTIMIZATION: Fast path for all-ASCII domain strings.
if (all_of(domain, is_ascii)) {
// 3. If result is the empty string, domain-to-ASCII validation error, return failure.
if (domain.is_empty())
return Error::from_string_literal("Empty domain");
return String::from_utf8_without_validation(domain.bytes());
}
Unicode::IDNA::ToAsciiOptions const options {
Unicode::IDNA::CheckHyphens::No,
Unicode::IDNA::CheckBidi::Yes,