Skip to content
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(url): minimize String::push calls in parse_scheme #789

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion url/src/lib.rs
Expand Up @@ -2243,7 +2243,7 @@ impl Url {
#[allow(clippy::result_unit_err, clippy::suspicious_operation_groupings)]
pub fn set_scheme(&mut self, scheme: &str) -> Result<(), ()> {
let mut parser = Parser::for_setter(String::new());
let remaining = parser.parse_scheme(parser::Input::new(scheme))?;
let remaining = parser::Input::new(parser.parse_scheme(scheme, None)?);
let new_scheme_type = SchemeType::from(&parser.serialization);
let old_scheme_type = SchemeType::from(self.scheme());
// If url’s scheme is a special scheme and buffer is not a special scheme, then return.
Expand Down
61 changes: 52 additions & 9 deletions url/src/parser.rs
Expand Up @@ -361,11 +361,14 @@ impl<'a> Parser<'a> {

/// https://url.spec.whatwg.org/#concept-basic-url-parser
pub fn parse_url(mut self, input: &str) -> ParseResult<Url> {
let input = Input::with_log(input, self.violation_fn);
if let Ok(remaining) = self.parse_scheme(input.clone()) {
return self.parse_with_scheme(remaining);
if let Ok(input) = self.parse_scheme(input, self.violation_fn) {
let input = Input {
chars: input.chars(),
};
return self.parse_with_scheme(input);
}

let input = Input::with_log(input, self.violation_fn);
// No-scheme state
if let Some(base_url) = self.base_url {
if input.starts_with('#') {
Expand All @@ -385,28 +388,68 @@ impl<'a> Parser<'a> {
}
}

pub fn parse_scheme<'i>(&mut self, mut input: Input<'i>) -> Result<Input<'i>, ()> {
pub fn parse_scheme<'i>(
&mut self,
original_input: &'i str,
vfn: Option<&dyn Fn(SyntaxViolation)>,
) -> Result<&'i str, ()> {
let input = original_input.trim_matches(c0_control_or_space);
if let Some(vfn) = vfn {
if input.len() < original_input.len() {
vfn(SyntaxViolation::C0SpaceIgnored)
}
if input.chars().any(|c| matches!(c, '\t' | '\n' | '\r')) {
vfn(SyntaxViolation::TabOrNewlineIgnored)
}
}

if input.is_empty() || !input.starts_with(ascii_alpha) {
return Err(());
}
debug_assert!(self.serialization.is_empty());
while let Some(c) = input.next() {
let mut i = 0;
let mut v: Option<Vec<usize>> = None;
for c in input.chars() {
match c {
'\t' | '\n' | '\r' => {
if v.is_none() {
v = Some(vec![]);
}

v.as_mut().unwrap().push(i);
i += 1;
}
'a'..='z' | 'A'..='Z' | '0'..='9' | '+' | '-' | '.' => {
self.serialization.push(c.to_ascii_lowercase())
i += 1;
}
':' => {
if let Some(ref mut v) = v {
let bytes = input.as_bytes();
for i in 0..i {
if !v.contains(&i) {
self.serialization
.push((bytes[i] as char).to_ascii_lowercase());
}
}
} else {
self.serialization
.push_str(&input[..i].to_ascii_lowercase());
}
return Ok(&input[i + 1..]);
}
':' => return Ok(input),
_ => {
self.serialization.clear();
return Err(());
}
}
}

// EOF before ':'
if self.context == Context::Setter {
Ok(input)
self.serialization
.push_str(&input[..i].to_ascii_lowercase());
Ok(&input[i..])
} else {
self.serialization.clear();
Err(())
}
}
Expand Down