Skip to content

Commit 0b23396

Browse files
committed
Don't make a Vec of &Part. Just use a reference to slice from Vec<Part>
This avoids allocating a new Vec and makes paring about 2% faster.
1 parent 579d8a7 commit 0b23396

1 file changed

Lines changed: 6 additions & 10 deletions

File tree

src/component.rs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ impl<R: RegExp> Component<R> {
3939
&options,
4040
encoding_callback,
4141
)?;
42-
let part_list = part_list.iter().collect::<Vec<_>>();
4342
let (regexp_string, name_list) =
4443
generate_regular_expression_and_name_list(&part_list, &options);
4544
let flags = if options.ignore_case { "ui" } else { "u" };
@@ -105,7 +104,7 @@ impl<R: RegExp> Component<R> {
105104

106105
// Ref: https://wicg.github.io/urlpattern/#generate-a-regular-expression-and-name-list
107106
fn generate_regular_expression_and_name_list(
108-
part_list: &[&Part],
107+
part_list: &[Part],
109108
options: &Options,
110109
) -> (String, Vec<String>) {
111110
let mut result = String::from("^");
@@ -179,15 +178,12 @@ fn generate_regular_expression_and_name_list(
179178
}
180179

181180
// Ref: https://wicg.github.io/urlpattern/#generate-a-pattern-string
182-
fn generate_pattern_string(part_list: &[&Part], options: &Options) -> String {
181+
fn generate_pattern_string(part_list: &[Part], options: &Options) -> String {
183182
let mut result = String::new();
184183
for (i, part) in part_list.iter().enumerate() {
185-
let prev_part: Option<&Part> = if i == 0 {
186-
None
187-
} else {
188-
part_list.get(i - 1).copied()
189-
};
190-
let next_part: Option<&Part> = part_list.get(i + 1).copied();
184+
let prev_part: Option<&Part> =
185+
if i == 0 { None } else { part_list.get(i - 1) };
186+
let next_part: Option<&Part> = part_list.get(i + 1);
191187
if part.kind == PartType::FixedText {
192188
if part.modifier == PartModifier::None {
193189
result.push_str(&escape_pattern_string(&part.value));
@@ -284,7 +280,7 @@ fn generate_pattern_string(part_list: &[&Part], options: &Options) -> String {
284280

285281
/// This function generates a matcher for a given parts list.
286282
fn generate_matcher<R: RegExp>(
287-
mut part_list: &[&Part],
283+
mut part_list: &[Part],
288284
options: &Options,
289285
flags: &str,
290286
) -> Matcher<R> {

0 commit comments

Comments
 (0)