Skip to content

Commit 0a4705a

Browse files
authored
perf: most specifiers don't have escaped characters (#636)
1 parent 87101ec commit 0a4705a

File tree

1 file changed

+5
-7
lines changed

1 file changed

+5
-7
lines changed

src/specifier.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,15 @@ impl<'a> Specifier<'a> {
3737
let mut fragment_start: Option<usize> = None;
3838

3939
let mut prev = specifier.chars().next().unwrap();
40-
let mut escaped_indexes = vec![];
40+
// Optimize for the common case: most specifiers don't have escaped characters
41+
let mut escaped_indexes: Option<Vec<usize>> = None;
4142
for (i, c) in specifier.char_indices().skip(skip) {
4243
if c == '?' && query_start.is_none() {
4344
query_start = Some(i);
4445
}
4546
if c == '#' {
4647
if prev == '\0' {
47-
escaped_indexes.push(i - 1);
48+
escaped_indexes.get_or_insert_with(Vec::new).push(i - 1);
4849
} else {
4950
fragment_start = Some(i);
5051
break;
@@ -63,17 +64,14 @@ impl<'a> Specifier<'a> {
6364
_ => (specifier, None, None),
6465
};
6566

66-
let path = if escaped_indexes.is_empty() {
67-
Cow::Borrowed(path)
68-
} else {
69-
// Remove the `\0` characters for a legal path.
67+
let path = escaped_indexes.map_or(Cow::Borrowed(path), |escaped_indexes| {
7068
Cow::Owned(
7169
path.chars()
7270
.enumerate()
7371
.filter_map(|(i, c)| (!escaped_indexes.contains(&i)).then_some(c))
7472
.collect::<String>(),
7573
)
76-
};
74+
});
7775

7876
(path, query, fragment)
7977
}

0 commit comments

Comments
 (0)