From 41db68651a03bc9a6317f26c4c8724a00e5ee067 Mon Sep 17 00:00:00 2001 From: Cong-Cong Date: Fri, 20 Sep 2024 16:33:19 +0800 Subject: [PATCH] perf: prevent duplicate create regex --- Cargo.lock | 1 + crates/core/Cargo.toml | 1 + crates/core/src/core/state.rs | 22 ++++++++++----- crates/core/src/hast_to_swc_ast/mod.rs | 21 +++++++++----- .../hast_to_swc_ast/string_to_object_style.rs | 28 +++++++++++-------- crates/core/src/hast_to_swc_ast/util.rs | 8 ++++-- 6 files changed, 53 insertions(+), 28 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 59ce0fc..efe4b37 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1936,6 +1936,7 @@ version = "0.1.3" dependencies = [ "clap", "codspeed-criterion-compat", + "lazy_static", "linked-hash-map", "linked_hash_set", "napi", diff --git a/crates/core/Cargo.toml b/crates/core/Cargo.toml index a9fae84..aed0ad1 100644 --- a/crates/core/Cargo.toml +++ b/crates/core/Cargo.toml @@ -33,6 +33,7 @@ swc_core = { version = "0.100.1", features = [ thiserror = "1.0.56" linked-hash-map = { version = "0.5.6", features = ["serde_impl"] } linked_hash_set = "0.1.4" +lazy_static = "1.5.0" [dev-dependencies] codspeed-criterion-compat = "2.7.2" diff --git a/crates/core/src/core/state.rs b/crates/core/src/core/state.rs index 927eb45..e117f32 100644 --- a/crates/core/src/core/state.rs +++ b/crates/core/src/core/state.rs @@ -1,5 +1,6 @@ use std::path::Path; +use lazy_static::lazy_static; use regex::Regex; #[cfg(feature = "node")] @@ -80,16 +81,20 @@ const IDENTIFIER: &str = r"([\p{Alpha}\p{N}_]|$)"; const SEPARATORS: &str = r"[_.\- ]+"; fn pascal_case(input: &str) -> String { - let separators_and_identifier = - Regex::new(format!("{}{}", SEPARATORS, IDENTIFIER).as_str()).unwrap(); - let numbers_and_identifier = Regex::new(format!("(\\d+){}", IDENTIFIER).as_str()).unwrap(); - let result = separators_and_identifier + lazy_static! { + static ref SEPARATORS_AND_IDENTIFIER_REGEX: Regex = + Regex::new(&format!("{}{}", SEPARATORS, IDENTIFIER)).unwrap(); + static ref NUMBERS_AND_IDENTIFIER_REGEX: Regex = + Regex::new(&format!("(\\d+){}", IDENTIFIER)).unwrap(); + } + + let result = SEPARATORS_AND_IDENTIFIER_REGEX .replace_all(input, |caps: ®ex::Captures| { let identifier = caps.get(1).unwrap().as_str(); identifier.to_uppercase() }) .to_string(); - let result = numbers_and_identifier + let result = NUMBERS_AND_IDENTIFIER_REGEX .replace_all(&result, |caps: ®ex::Captures| { let num = caps.get(1).unwrap().as_str(); let identifier = caps.get(2).unwrap().as_str(); @@ -100,8 +105,11 @@ fn pascal_case(input: &str) -> String { } fn get_component_name(file_path: &str) -> String { - let valid_char_regex = Regex::new(r"[^a-zA-Z0-9 _-]").unwrap(); - let file_name = valid_char_regex + lazy_static! { + static ref VALID_CHAR_REGEX_REGEX: Regex = Regex::new(r"[^a-zA-Z0-9 _-]").unwrap(); + } + + let file_name = VALID_CHAR_REGEX_REGEX .replace_all( Path::new(file_path) .file_prefix() diff --git a/crates/core/src/hast_to_swc_ast/mod.rs b/crates/core/src/hast_to_swc_ast/mod.rs index c6acc5e..a19f31b 100644 --- a/crates/core/src/hast_to_swc_ast/mod.rs +++ b/crates/core/src/hast_to_swc_ast/mod.rs @@ -1,5 +1,6 @@ use std::collections::HashMap; +use lazy_static::lazy_static; use regex::{Captures, Regex}; use swc_core::common::SyntaxContext; use swc_core::{ @@ -19,8 +20,10 @@ use self::string_to_object_style::*; use self::util::*; fn kebab_case(str: &str) -> String { - let kebab_regex = Regex::new(r"[A-Z\u00C0-\u00D6\u00D8-\u00DE]").unwrap(); - kebab_regex + lazy_static! { + static ref KEBAB_REGEX: Regex = Regex::new(r"[A-Z\u00C0-\u00D6\u00D8-\u00DE]").unwrap(); + } + KEBAB_REGEX .replace_all(str, |caps: &Captures| { format!("-{}", &caps[0].to_lowercase()) }) @@ -35,8 +38,10 @@ fn convert_aria_attribute(kebab_key: &str) -> String { } fn replace_spaces(s: &str) -> String { - let spaces_regex = Regex::new(r"[\t\r\n\u0085\u2028\u2029]+").unwrap(); - spaces_regex.replace_all(s, |_: &Captures| " ").to_string() + lazy_static! { + static ref SPACES_REGEX: Regex = Regex::new(r"[\t\r\n\u0085\u2028\u2029]+").unwrap(); + } + SPACES_REGEX.replace_all(s, |_: &Captures| " ").to_string() } fn get_value(attr_name: &str, value: &JsWord) -> JSXAttrValue { @@ -68,10 +73,12 @@ fn get_value(attr_name: &str, value: &JsWord) -> JSXAttrValue { } fn text(n: &swc_xml::ast::Text) -> Option { - let value = n.data.to_string(); + lazy_static! { + static ref SPACE_REGEX: Regex = Regex::new(r"^\s+$").unwrap(); + } - let space_regex = Regex::new(r"^\s+$").unwrap(); - if space_regex.is_match(&value) { + let value = n.data.to_string(); + if SPACE_REGEX.is_match(&value) { return None; } diff --git a/crates/core/src/hast_to_swc_ast/string_to_object_style.rs b/crates/core/src/hast_to_swc_ast/string_to_object_style.rs index 9db7d12..79fb5ba 100644 --- a/crates/core/src/hast_to_swc_ast/string_to_object_style.rs +++ b/crates/core/src/hast_to_swc_ast/string_to_object_style.rs @@ -1,23 +1,26 @@ +use lazy_static::lazy_static; use regex::{Captures, Regex}; use swc_core::{common::DUMMY_SP, ecma::ast::*}; use super::util::*; -const PX_REGEX: &str = r#"^\d+px$"#; -const MS_REGEX: &str = r#"^-ms-"#; -const VAR_REGEX: &str = r#"^--"#; - pub fn hyphen_to_camel_case(s: &str) -> String { - let regex = Regex::new(r#"-(.)"#).unwrap(); - regex + lazy_static! { + static ref HYPHEN_REGEX: Regex = Regex::new(r#"-(.)"#).unwrap(); + } + HYPHEN_REGEX .replace_all(s, |caps: &Captures| caps[1].to_uppercase()) .into() } // Format style key into JSX style object key. pub fn format_key(key: &str) -> PropName { - let var_regex = Regex::new(VAR_REGEX).unwrap(); - if var_regex.is_match(key) { + lazy_static! { + static ref VAR_REGEX: Regex = Regex::new(r#"^--"#).unwrap(); + static ref MS_REGEX: Regex = Regex::new(r#"^-ms-"#).unwrap(); + } + + if VAR_REGEX.is_match(key) { return PropName::Str(Str { span: DUMMY_SP, value: key.into(), @@ -26,8 +29,7 @@ pub fn format_key(key: &str) -> PropName { } let mut key = key.to_lowercase(); - let ms_regex = Regex::new(MS_REGEX).unwrap(); - if ms_regex.is_match(&key) { + if MS_REGEX.is_match(&key) { key = key[1..].into(); } @@ -35,8 +37,10 @@ pub fn format_key(key: &str) -> PropName { } fn is_convertible_pixel_value(s: &str) -> bool { - let px_regex = Regex::new(PX_REGEX).unwrap(); - px_regex.is_match(s) + lazy_static! { + static ref PX_REGEX: Regex = Regex::new(r#"^\d+px$"#).unwrap(); + } + PX_REGEX.is_match(s) } // Format style value into JSX style object value. diff --git a/crates/core/src/hast_to_swc_ast/util.rs b/crates/core/src/hast_to_swc_ast/util.rs index 62311b0..1f2c246 100644 --- a/crates/core/src/hast_to_swc_ast/util.rs +++ b/crates/core/src/hast_to_swc_ast/util.rs @@ -1,6 +1,10 @@ +use lazy_static::lazy_static; use regex::Regex; pub fn is_numeric(s: &str) -> bool { - let regex = Regex::new(r#"^(\-|\+)?\d+(\.\d+)?$"#).unwrap(); - regex.is_match(s) + lazy_static! { + static ref NUMERIC_REGEX: Regex = + Regex::new(r#"^(\-|\+)?\d+(\.\d+)?$"#).unwrap(); + } + NUMERIC_REGEX.is_match(s) }