Skip to content

Commit

Permalink
perf(es/renamer): Use IndexSet for rename queue (#9866)
Browse files Browse the repository at this point in the history
**Description:**

`Vec::contains` was causing a time complexity issue for large input files.
  • Loading branch information
kdy1 authored Jan 11, 2025
1 parent 6c2bb13 commit f404720
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
6 changes: 6 additions & 0 deletions .changeset/tricky-glasses-call.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
swc_core: minor
swc_ecma_transforms_base: minor
---

perf(es/renamer): Use `IndexSet` for rename queue
20 changes: 12 additions & 8 deletions crates/swc_ecma_transforms_base/src/rename/analyzer/scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

use std::{
fmt::{Display, Formatter},
mem::{transmute_copy, ManuallyDrop},
hash::BuildHasherDefault,
mem::{take, transmute_copy, ManuallyDrop},
};

use indexmap::IndexSet;
#[cfg(feature = "concurrent-renamer")]
use rayon::prelude::*;
use rustc_hash::FxHashSet;
use rustc_hash::{FxHashSet, FxHasher};
use swc_atoms::{atom, Atom};
use swc_common::{collections::AHashMap, util::take::Take, Mark, SyntaxContext};
use swc_ecma_ast::*;
Expand Down Expand Up @@ -36,6 +38,8 @@ pub(crate) struct Scope {
pub(super) children: Vec<Scope>,
}

pub(super) type FxIndexSet<T> = IndexSet<T, BuildHasherDefault<FxHasher>>;

#[derive(Debug, Default)]
pub(super) struct ScopeData {
/// All identifiers used by this scope or children.
Expand All @@ -46,7 +50,7 @@ pub(super) struct ScopeData {
/// because we merge every items in children to current scope.
all: FxHashSet<Id>,

queue: Vec<Id>,
queue: FxIndexSet<Id>,
}

impl Scope {
Expand All @@ -62,7 +66,7 @@ impl Scope {
return;
}

self.data.queue.push(id.clone());
self.data.queue.insert(id.clone());
}
}

Expand Down Expand Up @@ -104,7 +108,7 @@ impl Scope {
) where
R: Renamer,
{
let queue = self.data.queue.take();
let queue = take(&mut self.data.queue);

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

Expand Down Expand Up @@ -136,7 +140,7 @@ impl Scope {
to: &mut RenameMap,
previous: &RenameMap,
reverse: &mut ReverseMap,
queue: Vec<Id>,
queue: FxIndexSet<Id>,
preserved: &FxHashSet<Id>,
preserved_symbols: &FxHashSet<Atom>,
) where
Expand Down Expand Up @@ -208,7 +212,7 @@ impl Scope {
) where
R: Renamer,
{
let queue = self.data.queue.take();
let queue = take(&mut self.data.queue);

let mut cloned_reverse = reverse.next();

Expand Down Expand Up @@ -272,7 +276,7 @@ impl Scope {
to: &mut RenameMap,
previous: &RenameMap,
reverse: &mut ReverseMap,
queue: Vec<Id>,
queue: FxIndexSet<Id>,
preserved: &FxHashSet<Id>,
preserved_symbols: &FxHashSet<Atom>,
) where
Expand Down

0 comments on commit f404720

Please sign in to comment.