Skip to content

perf: make parse_scheme slightly faster #1025

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

Merged
merged 3 commits into from
Feb 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions url/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -398,15 +398,15 @@ impl<'a> Parser<'a> {
}

pub fn parse_scheme<'i>(&mut self, mut input: Input<'i>) -> Result<Input<'i>, ()> {
if input.is_empty() || !input.starts_with(ascii_alpha) {
// starts_with will also fail for empty strings so we can skip that comparison for perf
if !input.starts_with(ascii_alpha) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a comment that starts_with will also fail for empty strings?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a comment and unit test.

return Err(());
}
debug_assert!(self.serialization.is_empty());
while let Some(c) = input.next() {
match c {
'a'..='z' | 'A'..='Z' | '0'..='9' | '+' | '-' | '.' => {
self.serialization.push(c.to_ascii_lowercase())
}
'a'..='z' | '0'..='9' | '+' | '-' | '.' => self.serialization.push(c),
'A'..='Z' => self.serialization.push(c.to_ascii_lowercase()),
':' => return Ok(input),
_ => {
self.serialization.clear();
Expand Down
8 changes: 8 additions & 0 deletions url/tests/unit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1031,6 +1031,14 @@ fn test_set_scheme_to_file_with_host() {
assert_eq!(result, Err(()));
}

#[test]
fn test_set_scheme_empty_err() {
let mut url: Url = "http://localhost:6767/foo/bar".parse().unwrap();
let result = url.set_scheme("");
assert_eq!(url.to_string(), "http://localhost:6767/foo/bar");
assert_eq!(result, Err(()));
}

#[test]
fn no_panic() {
let mut url = Url::parse("arhttpsps:/.//eom/dae.com/\\\\t\\:").unwrap();
Expand Down
Loading