Skip to content

Commit f404720

Browse files
authoredJan 11, 2025··
perf(es/renamer): Use IndexSet for rename queue (#9866)
**Description:** `Vec::contains` was causing a time complexity issue for large input files.
1 parent 6c2bb13 commit f404720

File tree

2 files changed

+18
-8
lines changed

2 files changed

+18
-8
lines changed
 

‎.changeset/tricky-glasses-call.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
swc_core: minor
3+
swc_ecma_transforms_base: minor
4+
---
5+
6+
perf(es/renamer): Use `IndexSet` for rename queue

‎crates/swc_ecma_transforms_base/src/rename/analyzer/scope.rs

+12-8
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22

33
use std::{
44
fmt::{Display, Formatter},
5-
mem::{transmute_copy, ManuallyDrop},
5+
hash::BuildHasherDefault,
6+
mem::{take, transmute_copy, ManuallyDrop},
67
};
78

9+
use indexmap::IndexSet;
810
#[cfg(feature = "concurrent-renamer")]
911
use rayon::prelude::*;
10-
use rustc_hash::FxHashSet;
12+
use rustc_hash::{FxHashSet, FxHasher};
1113
use swc_atoms::{atom, Atom};
1214
use swc_common::{collections::AHashMap, util::take::Take, Mark, SyntaxContext};
1315
use swc_ecma_ast::*;
@@ -36,6 +38,8 @@ pub(crate) struct Scope {
3638
pub(super) children: Vec<Scope>,
3739
}
3840

41+
pub(super) type FxIndexSet<T> = IndexSet<T, BuildHasherDefault<FxHasher>>;
42+
3943
#[derive(Debug, Default)]
4044
pub(super) struct ScopeData {
4145
/// All identifiers used by this scope or children.
@@ -46,7 +50,7 @@ pub(super) struct ScopeData {
4650
/// because we merge every items in children to current scope.
4751
all: FxHashSet<Id>,
4852

49-
queue: Vec<Id>,
53+
queue: FxIndexSet<Id>,
5054
}
5155

5256
impl Scope {
@@ -62,7 +66,7 @@ impl Scope {
6266
return;
6367
}
6468

65-
self.data.queue.push(id.clone());
69+
self.data.queue.insert(id.clone());
6670
}
6771
}
6872

@@ -104,7 +108,7 @@ impl Scope {
104108
) where
105109
R: Renamer,
106110
{
107-
let queue = self.data.queue.take();
111+
let queue = take(&mut self.data.queue);
108112

109113
// let mut cloned_reverse = reverse.clone();
110114

@@ -136,7 +140,7 @@ impl Scope {
136140
to: &mut RenameMap,
137141
previous: &RenameMap,
138142
reverse: &mut ReverseMap,
139-
queue: Vec<Id>,
143+
queue: FxIndexSet<Id>,
140144
preserved: &FxHashSet<Id>,
141145
preserved_symbols: &FxHashSet<Atom>,
142146
) where
@@ -208,7 +212,7 @@ impl Scope {
208212
) where
209213
R: Renamer,
210214
{
211-
let queue = self.data.queue.take();
215+
let queue = take(&mut self.data.queue);
212216

213217
let mut cloned_reverse = reverse.next();
214218

@@ -272,7 +276,7 @@ impl Scope {
272276
to: &mut RenameMap,
273277
previous: &RenameMap,
274278
reverse: &mut ReverseMap,
275-
queue: Vec<Id>,
279+
queue: FxIndexSet<Id>,
276280
preserved: &FxHashSet<Id>,
277281
preserved_symbols: &FxHashSet<Atom>,
278282
) where

0 commit comments

Comments
 (0)
Please sign in to comment.