Skip to content

Commit

Permalink
perf(mangler): only collect exported symbols when top_level is false
Browse files Browse the repository at this point in the history
  • Loading branch information
sapphi-red committed Dec 22, 2024
1 parent c2a99eb commit fe885dc
Showing 1 changed file with 28 additions and 21 deletions.
49 changes: 28 additions & 21 deletions crates/oxc_mangler/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,27 +86,11 @@ impl Mangler {
pub fn build<'a>(mut self, program: &'a Program<'a>) -> Mangler {
let semantic = SemanticBuilder::new().build(program).semantic;

let (exported_names, exported_symbols): (FxHashSet<CompactStr>, FxHashSet<SymbolId>) =
program
.body
.iter()
.filter_map(|statement| {
let Statement::ExportNamedDeclaration(v) = statement else { return None };
v.declaration.as_ref()
})
.flat_map(|decl| {
if let Declaration::VariableDeclaration(decl) = decl {
itertools::Either::Left(
decl.declarations
.iter()
.filter_map(|decl| decl.id.get_binding_identifier()),
)
} else {
itertools::Either::Right(decl.id().into_iter())
}
})
.map(|id| (id.name.to_compact_str(), id.symbol_id()))
.collect();
let (exported_names, exported_symbols) = if self.options.top_level {
Mangler::collect_exported_symbols(program)
} else {
Default::default()
};

// Mangle the symbol table by computing slots from the scope tree.
// A slot is the occurrence index of a binding identifier inside a scope.
Expand Down Expand Up @@ -260,6 +244,29 @@ impl Mangler {
frequencies.sort_unstable_by_key(|x| std::cmp::Reverse(x.frequency));
frequencies
}

fn collect_exported_symbols(program: &Program) -> (FxHashSet<CompactStr>, FxHashSet<SymbolId>) {
program
.body
.iter()
.filter_map(|statement| {
let Statement::ExportNamedDeclaration(v) = statement else { return None };
v.declaration.as_ref()
})
.flat_map(|decl| {
if let Declaration::VariableDeclaration(decl) = decl {
itertools::Either::Left(
decl.declarations
.iter()
.filter_map(|decl| decl.id.get_binding_identifier()),
)
} else {
itertools::Either::Right(decl.id().into_iter())
}
})
.map(|id| (id.name.to_compact_str(), id.symbol_id()))
.collect()
}
}

fn is_special_name(name: &str) -> bool {
Expand Down

0 comments on commit fe885dc

Please sign in to comment.