Skip to content
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: 8 additions & 0 deletions url/benches/parse_url.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ fn plain(bench: &mut Bencher) {
bench.iter(|| black_box(url).parse::<Url>().unwrap());
}

fn port(bench: &mut Bencher) {
let url = "https://example.com:8080";

bench.bytes = url.len() as u64;
bench.iter(|| black_box(url).parse::<Url>().unwrap());
}

fn hyphen(bench: &mut Bencher) {
let url = "https://hyphenated-example.com/";

Expand Down Expand Up @@ -95,6 +102,7 @@ benchmark_group!(
long,
fragment,
plain,
port,
hyphen,
leading_digit,
unicode_mixed,
Expand Down
34 changes: 30 additions & 4 deletions url/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -970,13 +970,17 @@

let (port, remaining) = if let Some(remaining) = remaining.split_prefix(':') {
let scheme = || default_port(&self.serialization[..scheme_end as usize]);
Parser::parse_port(remaining, scheme, self.context)?
let (port, remaining) = Parser::parse_port(remaining, scheme, self.context)?;
if let Some(port) = port {
self.serialization.push(':');
let mut buffer = [0u8; 5];
let port_str = fast_u16_to_str(&mut buffer, port);
self.serialization.push_str(port_str);
}
(port, remaining)
} else {
(None, remaining)
};
if let Some(port) = port {
write!(&mut self.serialization, ":{}", port).unwrap()
}
Ok((host_end, host.into(), port, remaining))
}

Expand Down Expand Up @@ -1744,3 +1748,25 @@
_ => false,
}
}

#[inline]
fn fast_u16_to_str(
// max 5 digits for u16 (65535)
buffer: &mut [u8; 5],
mut value: u16,
) -> &str {
let mut index = buffer.len();

loop {

Check warning on line 1760 in url/src/parser.rs

View check run for this annotation

Codecov / codecov/patch

url/src/parser.rs#L1760

Added line #L1760 was not covered by tests
index -= 1;
buffer[index] = b'0' + (value % 10) as u8;
value /= 10;
if value == 0 {
break;

Check warning on line 1765 in url/src/parser.rs

View check run for this annotation

Codecov / codecov/patch

url/src/parser.rs#L1765

Added line #L1765 was not covered by tests
}
}

// SAFETY: we know the values in the buffer from the
// current index on will be a number
unsafe { core::str::from_utf8_unchecked(&buffer[index..]) }
}
Loading