Skip to content

Commit 519bbb5

Browse files
committed
perf(url): avoid trivial reallocs
1 parent 48fcbe1 commit 519bbb5

File tree

3 files changed

+14
-8
lines changed

3 files changed

+14
-8
lines changed

idna/src/uts46.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -554,7 +554,7 @@ impl Config {
554554

555555
/// http://www.unicode.org/reports/tr46/#ToASCII
556556
pub fn to_ascii(self, domain: &str) -> Result<String, Errors> {
557-
let mut result = String::new();
557+
let mut result = String::with_capacity(domain.len());
558558
let mut codec = Idna::new(self);
559559
codec.to_ascii(domain, &mut result).map(|()| result)
560560
}

url/benches/parse_url.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,12 @@ fn short(bench: &mut Bencher) {
1212
bench.iter(|| black_box(url).parse::<Url>().unwrap());
1313
}
1414

15-
benchmark_group!(benches, short);
15+
fn long(bench: &mut Bencher) {
16+
let url = "https://example.com/parkbench?tre=es&st=uff";
17+
18+
bench.bytes = url.len() as u64;
19+
bench.iter(|| black_box(url).parse::<Url>().unwrap());
20+
}
21+
22+
benchmark_group!(benches, short, long);
1623
benchmark_main!(benches);

url/src/parser.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1216,13 +1216,11 @@ impl<'a> Parser<'a> {
12161216
}
12171217
}
12181218
}
1219-
// Going from &str to String to &str to please the 1.33.0 borrow checker
1220-
let before_slash_string = if ends_with_slash {
1221-
self.serialization[segment_start..self.serialization.len() - 1].to_owned()
1219+
let segment_before_slash = if ends_with_slash {
1220+
&self.serialization[segment_start..self.serialization.len() - 1]
12221221
} else {
1223-
self.serialization[segment_start..self.serialization.len()].to_owned()
1222+
&self.serialization[segment_start..self.serialization.len()]
12241223
};
1225-
let segment_before_slash: &str = &before_slash_string;
12261224
match segment_before_slash {
12271225
// If buffer is a double-dot path segment, shorten url’s path,
12281226
".." | "%2e%2e" | "%2e%2E" | "%2E%2e" | "%2E%2E" | "%2e." | "%2E." | ".%2e"
@@ -1412,7 +1410,8 @@ impl<'a> Parser<'a> {
14121410
scheme_end: u32,
14131411
mut input: Input<'i>,
14141412
) -> Option<Input<'i>> {
1415-
let mut query = String::new(); // FIXME: use a streaming decoder instead
1413+
let len = input.chars.as_str().len();
1414+
let mut query = String::with_capacity(len); // FIXME: use a streaming decoder instead
14161415
let mut remaining = None;
14171416
while let Some(c) = input.next() {
14181417
if c == '#' && self.context == Context::UrlParser {

0 commit comments

Comments
 (0)