Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 18bc668

Browse files
authored
perf(rust): don't use regex on legitimize_identifier_name (#1477)
1 parent e40b61b commit 18bc668

4 files changed

Lines changed: 40 additions & 13 deletions

File tree

crates/rolldown/src/ast_scanner/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use rolldown_common::{
2020
use rolldown_error::BuildError;
2121
use rolldown_oxc_utils::{BindingIdentifierExt, BindingPatternExt};
2222
use rolldown_rstr::{Rstr, ToRstr};
23+
use rolldown_utils::ecma_script::legitimize_identifier_name;
2324
use rolldown_utils::path_ext::PathExt;
2425
use rustc_hash::FxHashMap;
2526
use std::sync::Arc;
@@ -260,6 +261,7 @@ impl<'me> AstScanner<'me> {
260261
.module_request
261262
.as_path()
262263
.representative_file_name();
264+
let importee_repr = legitimize_identifier_name(&importee_repr);
263265
format!("{importee_repr}_default").into()
264266
} else {
265267
export_name.into()

crates/rolldown/src/module_loader/normal_module_task.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use rolldown_error::BuildError;
1313
use rolldown_oxc_utils::OxcAst;
1414
use rolldown_plugin::{HookResolveIdExtraOptions, SharedPluginDriver};
1515
use rolldown_resolver::ResolveError;
16-
use rolldown_utils::path_ext::PathExt;
16+
use rolldown_utils::{ecma_script::legitimize_identifier_name, path_ext::PathExt};
1717
use sugar_path::SugarPath;
1818

1919
use super::{task_context::TaskContext, Msg};
@@ -246,6 +246,7 @@ impl NormalModuleTask {
246246
let (ast_scopes, mut ast_symbols) = make_ast_scopes_and_symbols(ast);
247247
let file_path: ResourceId = Arc::<str>::clone(&self.resolved_path.path).into();
248248
let repr_name = file_path.as_path().representative_file_name();
249+
let repr_name = legitimize_identifier_name(&repr_name);
249250

250251
let scanner = AstScanner::new(
251252
self.module_id,

crates/rolldown_utils/src/ecma_script.rs

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,45 @@
1-
use regex::Regex;
1+
use oxc::syntax::identifier;
22
use std::borrow::Cow;
33

44
pub fn is_validate_identifier_name(name: &str) -> bool {
55
oxc::syntax::identifier::is_identifier_name(name)
66
}
77

88
pub fn legitimize_identifier_name(name: &str) -> Cow<str> {
9-
static VALID_RE: once_cell::sync::Lazy<Regex> =
10-
once_cell::sync::Lazy::new(|| Regex::new(r"[^a-zA-Z0-9_$]").unwrap());
9+
let mut legitimized = String::new();
10+
let mut have_seen_invalid_char = false;
1111

12-
VALID_RE.replace_all(name, "_")
12+
let mut chars_indices = name.char_indices();
13+
14+
if let Some((_, first_char)) = chars_indices.next() {
15+
if identifier::is_identifier_start(first_char) {
16+
// Nothing we need to do
17+
} else {
18+
legitimized.push('_');
19+
have_seen_invalid_char = true;
20+
}
21+
}
22+
23+
for (idx, char) in chars_indices {
24+
if identifier::is_identifier_part(char) {
25+
if have_seen_invalid_char {
26+
legitimized.push(char);
27+
}
28+
} else {
29+
if !have_seen_invalid_char {
30+
// See a invalid char for the first time
31+
have_seen_invalid_char = true;
32+
legitimized.push_str(&name[..idx]);
33+
}
34+
legitimized.push('_');
35+
}
36+
}
37+
38+
if have_seen_invalid_char {
39+
Cow::Owned(legitimized)
40+
} else {
41+
Cow::Borrowed(name)
42+
}
1343
}
1444

1545
#[test]

crates/rolldown_utils/src/path_ext.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ use std::{borrow::Cow, ffi::OsStr, path::Path};
22

33
use sugar_path::SugarPath;
44

5-
use crate::ecma_script::legitimize_identifier_name;
6-
75
pub trait PathExt {
86
fn expect_to_str(&self) -> &str;
97

@@ -26,6 +24,7 @@ impl PathExt for std::path::Path {
2624
.into_owned()
2725
}
2826

27+
/// It doesn't ensure the file name is a valid identifier in JS.
2928
fn representative_file_name(&self) -> Cow<str> {
3029
let file_name =
3130
self.file_stem().map_or_else(|| self.to_string_lossy(), |stem| stem.to_string_lossy());
@@ -45,12 +44,7 @@ impl PathExt for std::path::Path {
4544
_ => file_name,
4645
};
4746

48-
let legal = legitimize_identifier_name(&file_name);
49-
match legal {
50-
// No changes. Just return the original file name.
51-
Cow::Borrowed(_) => file_name,
52-
Cow::Owned(v) => Cow::Owned(v),
53-
}
47+
file_name
5448
}
5549
}
5650

0 commit comments

Comments
 (0)