-
Notifications
You must be signed in to change notification settings - Fork 336
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
perf: remove heap allocation in parse_host #1021
Open
dsherret
wants to merge
8
commits into
servo:main
Choose a base branch
from
dsherret:perf_heap_allocation_parse_host
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+116
−44
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
18640a0
perf: remove heap allocation in parse_host
dsherret 4b12c9b
make compile with no_std
dsherret 639243a
more comments
dsherret b4d4154
add size hint for Iterator
dsherret 2b26bfe
Merge branch 'main' into perf_heap_allocation_parse_host
dsherret 4880780
Merge branch 'main' into perf_heap_allocation_parse_host
dsherret 362683b
move function down to idna crate
dsherret a18ac4c
format
dsherret File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,7 +10,6 @@ | |
use alloc::borrow::Cow; | ||
use alloc::borrow::ToOwned; | ||
use alloc::string::String; | ||
use alloc::string::ToString; | ||
use alloc::vec::Vec; | ||
use core::cmp; | ||
use core::fmt::{self, Formatter}; | ||
|
@@ -30,8 +29,8 @@ | |
Ipv6(Ipv6Addr), | ||
} | ||
|
||
impl From<Host<String>> for HostInternal { | ||
fn from(host: Host<String>) -> HostInternal { | ||
impl From<Host<Cow<'_, str>>> for HostInternal { | ||
fn from(host: Host<Cow<'_, str>>) -> HostInternal { | ||
match host { | ||
Host::Domain(ref s) if s.is_empty() => HostInternal::None, | ||
Host::Domain(_) => HostInternal::Domain, | ||
|
@@ -80,15 +79,34 @@ | |
/// | ||
/// <https://url.spec.whatwg.org/#host-parsing> | ||
pub fn parse(input: &str) -> Result<Self, ParseError> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I didn't add, change, or remove from the public API, but please verify this as well in case I missed something. |
||
Host::<Cow<str>>::parse_cow(input.into()).map(|i| i.into_owned()) | ||
} | ||
|
||
/// <https://url.spec.whatwg.org/#concept-opaque-host-parser> | ||
pub fn parse_opaque(input: &str) -> Result<Self, ParseError> { | ||
Host::<Cow<str>>::parse_opaque_cow(input.into()).map(|i| i.into_owned()) | ||
} | ||
} | ||
|
||
impl<'a> Host<Cow<'a, str>> { | ||
pub(crate) fn parse_cow(input: Cow<'a, str>) -> Result<Self, ParseError> { | ||
if input.starts_with('[') { | ||
if !input.ends_with(']') { | ||
return Err(ParseError::InvalidIpv6Address); | ||
} | ||
return parse_ipv6addr(&input[1..input.len() - 1]).map(Host::Ipv6); | ||
} | ||
let domain: Cow<'_, [u8]> = percent_decode(input.as_bytes()).into(); | ||
let domain: Cow<'a, [u8]> = match domain { | ||
Cow::Owned(v) => Cow::Owned(v), | ||
// if borrowed then we can use the original cow | ||
Cow::Borrowed(_) => match input { | ||
Cow::Borrowed(input) => Cow::Borrowed(input.as_bytes()), | ||
Cow::Owned(input) => Cow::Owned(input.into_bytes()), | ||
}, | ||
}; | ||
|
||
let domain = Self::domain_to_ascii(&domain)?; | ||
let domain = idna::domain_to_ascii_from_cow(domain, idna::AsciiDenyList::URL)?; | ||
|
||
if domain.is_empty() { | ||
return Err(ParseError::EmptyHost); | ||
|
@@ -98,12 +116,11 @@ | |
let address = parse_ipv4addr(&domain)?; | ||
Ok(Host::Ipv4(address)) | ||
} else { | ||
Ok(Host::Domain(domain.to_string())) | ||
Ok(Host::Domain(domain)) | ||
} | ||
} | ||
|
||
// <https://url.spec.whatwg.org/#concept-opaque-host-parser> | ||
pub fn parse_opaque(input: &str) -> Result<Self, ParseError> { | ||
pub(crate) fn parse_opaque_cow(input: Cow<'a, str>) -> Result<Self, ParseError> { | ||
if input.starts_with('[') { | ||
if !input.ends_with(']') { | ||
return Err(ParseError::InvalidIpv6Address); | ||
|
@@ -137,14 +154,21 @@ | |
Err(ParseError::InvalidDomainCharacter) | ||
} else { | ||
Ok(Host::Domain( | ||
utf8_percent_encode(input, CONTROLS).to_string(), | ||
match utf8_percent_encode(&input, CONTROLS).into() { | ||
Cow::Owned(v) => Cow::Owned(v), | ||
// if we're borrowing, then we can return the original Cow | ||
Cow::Borrowed(_) => input, | ||
}, | ||
)) | ||
} | ||
} | ||
|
||
/// convert domain with idna | ||
fn domain_to_ascii(domain: &[u8]) -> Result<Cow<'_, str>, ParseError> { | ||
idna::domain_to_ascii_cow(domain, idna::AsciiDenyList::URL).map_err(Into::into) | ||
pub(crate) fn into_owned(self) -> Host<String> { | ||
match self { | ||
Host::Domain(s) => Host::Domain(s.into_owned()), | ||
Host::Ipv4(ip) => Host::Ipv4(ip), | ||
Host::Ipv6(ip) => Host::Ipv6(ip), | ||
} | ||
} | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@hsivonen Please check if you find this new public API of
idna
to be acceptable.