Skip to content

Commit caa2e11

Browse files
committed
refactor(transformer/styled-components): do not allocate temp data into arena
1 parent e93e4c8 commit caa2e11

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

crates/oxc_transformer/src/plugins/styled_components.rs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,15 @@
5656
//! - Documentation: <https://styled-components.com/docs/tooling#babel-plugin>
5757
5858
use std::{
59+
borrow::Cow,
5960
hash::{Hash, Hasher},
6061
iter::once,
6162
};
6263

6364
use rustc_hash::{FxHashSet, FxHasher};
6465
use serde::Deserialize;
6566

66-
use oxc_allocator::{StringBuilder, TakeIn, Vec as ArenaVec};
67+
use oxc_allocator::{TakeIn, Vec as ArenaVec};
6768
use oxc_ast::{AstBuilder, NONE, ast::*};
6869
use oxc_data_structures::inline_string::InlineString;
6970
use oxc_semantic::SymbolId;
@@ -894,12 +895,12 @@ impl<'a> CssMinifier<'a> {
894895
let minifier = Self::new(ast);
895896

896897
let css = if quasis.len() == 1 {
897-
&quasis[0].value.raw
898+
Cow::Borrowed(quasis[0].value.raw.as_str())
898899
} else {
899-
minifier.inject_unique_placeholders(quasis)
900+
Cow::Owned(Self::inject_unique_placeholders(quasis))
900901
};
901902

902-
minifier.minify_css(css)
903+
minifier.minify_css(&css)
903904
}
904905

905906
/// Injects unique placeholders into a series of `quasis` to represent expressions.
@@ -915,23 +916,25 @@ impl<'a> CssMinifier<'a> {
915916
/// `["width: ", "px; color: ", ";"]`, and expressions `[width, color]`,
916917
/// this function will produce the string:
917918
/// `"width: __PLACEHOLDER_0__px; color: __PLACEHOLDER_1__;"`
918-
fn inject_unique_placeholders(&self, quasis: &[TemplateElement]) -> &str {
919+
fn inject_unique_placeholders(quasis: &[TemplateElement]) -> String {
919920
let estimated_capacity: usize = quasis.iter().map(|s| s.value.raw.len()).sum::<usize>()
920921
+ (quasis.len() - 1)
921922
* (Self::PLACEHOLDER_PREFIX.len() + Self::PLACEHOLDER_SUFFIX.len() + 2); // 2 for digits
922923

923-
let mut result = StringBuilder::with_capacity_in(estimated_capacity, self.ast.allocator);
924+
let mut result = String::with_capacity(estimated_capacity);
924925

925926
for (index, val) in quasis.iter().enumerate() {
926927
result.push_str(&val.value.raw);
927928
if index < quasis.len() - 1 {
928-
result.push_str(Self::PLACEHOLDER_PREFIX);
929-
result.push_str(itoa::Buffer::new().format(index));
930-
result.push_str(Self::PLACEHOLDER_SUFFIX);
929+
result.extend([
930+
Self::PLACEHOLDER_PREFIX,
931+
itoa::Buffer::new().format(index),
932+
Self::PLACEHOLDER_SUFFIX,
933+
]);
931934
}
932935
}
933936

934-
result.into_str()
937+
result
935938
}
936939

937940
/// Tries to parse a placeholder like `__PLACEHOLDER_0__` from a byte slice.

0 commit comments

Comments
 (0)