Skip to content

Commit 7ed27b7

Browse files
authored
perf(isolated-declarations): use FxHashSet instead of Vec to speed up the contain (#4074)
1 parent 6e5447e commit 7ed27b7

File tree

1 file changed

+12
-12
lines changed
  • crates/oxc_isolated_declarations/src

1 file changed

+12
-12
lines changed

crates/oxc_isolated_declarations/src/lib.rs

+12-12
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ impl<'a> IsolatedDeclarations<'a> {
121121
let mut new_stmts = Vec::new();
122122
let mut variables_declarations = VecDeque::new();
123123
let mut variable_transformed_indexes = VecDeque::new();
124-
let mut transformed_indexes = Vec::new();
124+
let mut transformed_indexes = FxHashSet::default();
125125
// 1. Collect all declarations, module declarations
126126
// 2. Transform export declarations
127127
// 3. Collect all bindings / reference from module declarations
@@ -134,18 +134,18 @@ impl<'a> IsolatedDeclarations<'a> {
134134
variables_declarations.push_back(
135135
self.ast.copy(&decl.declarations).into_iter().collect::<Vec<_>>(),
136136
);
137-
variable_transformed_indexes.push_back(Vec::default());
137+
variable_transformed_indexes.push_back(FxHashSet::default());
138138
}
139139
Declaration::UsingDeclaration(decl) => {
140140
variables_declarations.push_back(
141141
self.ast.copy(&decl.declarations).into_iter().collect::<Vec<_>>(),
142142
);
143-
variable_transformed_indexes.push_back(Vec::default());
143+
variable_transformed_indexes.push_back(FxHashSet::default());
144144
}
145145
Declaration::TSModuleDeclaration(decl) => {
146146
if decl.kind.is_global() {
147147
self.scope.visit_ts_module_declaration(decl);
148-
transformed_indexes.push(new_stmts.len());
148+
transformed_indexes.insert(new_stmts.len());
149149
}
150150
}
151151
_ => {}
@@ -155,7 +155,7 @@ impl<'a> IsolatedDeclarations<'a> {
155155
match_module_declaration!(Statement) => {
156156
match stmt.to_module_declaration() {
157157
ModuleDeclaration::ExportDefaultDeclaration(decl) => {
158-
transformed_indexes.push(new_stmts.len());
158+
transformed_indexes.insert(new_stmts.len());
159159
if let Some((var_decl, new_decl)) =
160160
self.transform_export_default_declaration(decl)
161161
{
@@ -165,7 +165,7 @@ impl<'a> IsolatedDeclarations<'a> {
165165
new_stmts.push(Statement::VariableDeclaration(
166166
self.ast.alloc(var_decl),
167167
));
168-
transformed_indexes.push(new_stmts.len());
168+
transformed_indexes.insert(new_stmts.len());
169169
}
170170

171171
self.scope.visit_export_default_declaration(&new_decl);
@@ -180,7 +180,7 @@ impl<'a> IsolatedDeclarations<'a> {
180180
}
181181

182182
ModuleDeclaration::ExportNamedDeclaration(decl) => {
183-
transformed_indexes.push(new_stmts.len());
183+
transformed_indexes.insert(new_stmts.len());
184184
if let Some(new_decl) = self.transform_export_named_declaration(decl) {
185185
self.scope.visit_declaration(
186186
new_decl.declaration.as_ref().unwrap_or_else(|| unreachable!()),
@@ -198,7 +198,7 @@ impl<'a> IsolatedDeclarations<'a> {
198198
// We must transform this in the end, because we need to know all references
199199
}
200200
module_declaration => {
201-
transformed_indexes.push(new_stmts.len());
201+
transformed_indexes.insert(new_stmts.len());
202202
self.scope.visit_module_declaration(module_declaration);
203203
}
204204
}
@@ -241,13 +241,13 @@ impl<'a> IsolatedDeclarations<'a> {
241241

242242
if let Some(decl) = self.transform_variable_declarator(declarator, true) {
243243
self.scope.visit_variable_declarator(&decl);
244-
cur_transformed_indexes.push(ii);
244+
cur_transformed_indexes.insert(ii);
245245
*declarator = decl;
246246
}
247247
}
248248
} else if let Some(decl) = self.transform_declaration(decl, true) {
249249
self.scope.visit_declaration(&decl);
250-
transformed_indexes.push(i);
250+
transformed_indexes.insert(i);
251251
*stmt = Statement::from(decl);
252252
}
253253
}
@@ -280,7 +280,7 @@ impl<'a> IsolatedDeclarations<'a> {
280280
),
281281
);
282282
new_ast_stmts.push(Statement::VariableDeclaration(variables_declaration));
283-
transformed_indexes.push(index);
283+
transformed_indexes.insert(index);
284284
}
285285
}
286286
Statement::UsingDeclaration(decl) => {
@@ -302,7 +302,7 @@ impl<'a> IsolatedDeclarations<'a> {
302302
),
303303
);
304304
new_ast_stmts.push(Statement::VariableDeclaration(variable_declaration));
305-
transformed_indexes.push(index);
305+
transformed_indexes.insert(index);
306306
}
307307
}
308308
Statement::ImportDeclaration(decl) => {

0 commit comments

Comments
 (0)