Skip to content

Commit 0e149a5

Browse files
committed
perf: remove new_raws
1 parent f830b45 commit 0e149a5

File tree

1 file changed

+19
-33
lines changed

1 file changed

+19
-33
lines changed

crates/oxc_transformer/src/plugins/styled_components.rs

Lines changed: 19 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -844,8 +844,6 @@ fn is_valid_styled_component_source(source: &str) -> bool {
844844
/// - Remove next expression from `expressions`.
845845
/// - Remove next quasi from `quasis`.
846846
/// - Append the next quasi onto end of current one.
847-
/// 4. Collect the remaining minified quasis into `new_raws`.
848-
/// 5. Finally, set `raw` for each remaining quasi to the `Atom`s in `new_raws`.
849847
///
850848
/// # Example
851849
///
@@ -886,27 +884,21 @@ fn minify_template_literal<'a>(lit: &mut TemplateLiteral<'a>, ast: AstBuilder<'a
886884
// Parentheses depth. `((x)` -> 1, `(x))` -> -1.
887885
let mut paren_depth: isize = 0;
888886

889-
// New `raw` values for quasis.
890-
// Note: May end up shorter than original `quasis`, because some quasis may be discarded.
891-
let mut new_raws = Vec::with_capacity(quasis.len());
892-
893887
// Current minified quasi being built
894888
let mut output = Vec::new();
895-
// `true` if `output` needs to be pushed to `new_raws`
896-
let mut push_output = false;
897889

898890
// TODO: Update span end of `TemplateElement` when it has next one appended to end of it.
899891
// TODO: What about `cooked`? Shouldn't we alter that too?
900-
quasis.retain_mut(|quasi| {
901-
let mut bytes = quasi.value.raw.as_str().as_bytes();
892+
let mut quasi_index = 0;
893+
while quasi_index < quasis.len() {
894+
let mut bytes = quasis[quasi_index].value.raw.as_str().as_bytes();
902895

903-
let mut retain_quasi = true;
904896
if let Some(is_block_comment) = comment_type {
905897
// Remove this quasi and the preceding expression.
906898
// Note: This branch cannot be taken on first iteration.
907899
// TODO: Remove scopes, symbols, and references for removed `Expression`.
908-
retain_quasi = false;
909-
expressions.remove(new_raws.len());
900+
quasis.remove(quasi_index);
901+
expressions.remove(quasi_index - 1);
910902

911903
// Find end of comment
912904
let start_index = if is_block_comment {
@@ -935,15 +927,17 @@ fn minify_template_literal<'a>(lit: &mut TemplateLiteral<'a>, ast: AstBuilder<'a
935927

936928
comment_type = None;
937929

938-
// Don't push `output` to `new_raws` and clear it. Keep appending to existing `output`.
939-
} else if push_output {
940-
// `if push_output` check to avoid pushing empty `output` on first iteration.
941-
// SAFETY: Output is all picked from the original `raw` values and is guaranteed to be valid UTF-8
942-
let output_str = unsafe { std::str::from_utf8_unchecked(&output) };
943-
new_raws.push(ast.atom(output_str));
944-
output.clear();
930+
// Don't touch `output`. This quasi continues appending to existing `output`.
945931
} else {
946-
push_output = true;
932+
// If not on first quasi, set `raw` for previous quasi to `output`
933+
if quasi_index > 0 {
934+
// SAFETY: Output is all picked from the original `raw` values and is guaranteed to be valid UTF-8.
935+
let output_str = unsafe { std::str::from_utf8_unchecked(&output) };
936+
quasis[quasi_index - 1].value.raw = ast.atom(output_str);
937+
output.clear();
938+
}
939+
940+
quasi_index += 1;
947941
}
948942

949943
let mut i = 0;
@@ -1057,20 +1051,12 @@ fn minify_template_literal<'a>(lit: &mut TemplateLiteral<'a>, ast: AstBuilder<'a
10571051
output.push(cur_byte);
10581052
i += 1;
10591053
}
1054+
}
10601055

1061-
retain_quasi
1062-
});
1063-
1064-
// SAFETY: Output is all picked from the original `raw` values and is guaranteed to be valid UTF-8
1056+
// Update last quasi.
1057+
// SAFETY: Output is all picked from the original `raw` values and is guaranteed to be valid UTF-8.
10651058
let output_str = unsafe { std::str::from_utf8_unchecked(&output) };
1066-
new_raws.push(ast.atom(output_str));
1067-
1068-
debug_assert!(new_raws.len() == quasis.len());
1069-
debug_assert!(quasis.len() == expressions.len() + 1);
1070-
1071-
for (quasi, new_raw) in quasis.iter_mut().zip(new_raws) {
1072-
quasi.value.raw = new_raw;
1073-
}
1059+
quasis.last_mut().unwrap().value.raw = ast.atom(output_str);
10741060
}
10751061

10761062
#[cfg(test)]

0 commit comments

Comments
 (0)