perf(minifier): preallocate mangler's semantic data (#8451)

This commit is contained in:
Boshen 2025-01-12 16:06:47 +00:00
parent 6e64eef37c
commit 372eb09569
2 changed files with 26 additions and 8 deletions

View file

@ -83,9 +83,19 @@ impl Mangler {
}
#[must_use]
pub fn build<'a>(mut self, program: &'a Program<'a>) -> Mangler {
pub fn build(self, program: &Program<'_>) -> Mangler {
let semantic = SemanticBuilder::new().build(program).semantic;
let (symbol_table, scope_tree) = semantic.into_symbol_table_and_scope_tree();
self.build_with_symbols_and_scopes(symbol_table, &scope_tree, program)
}
#[must_use]
pub fn build_with_symbols_and_scopes(
mut self,
symbol_table: SymbolTable,
scope_tree: &ScopeTree,
program: &Program<'_>,
) -> Mangler {
let (exported_names, exported_symbols) = if self.options.top_level {
Mangler::collect_exported_symbols(program)
} else {
@ -94,7 +104,7 @@ impl Mangler {
// Mangle the symbol table by computing slots from the scope tree.
// A slot is the occurrence index of a binding identifier inside a scope.
let (mut symbol_table, scope_tree) = semantic.into_symbol_table_and_scope_tree();
let mut symbol_table = symbol_table;
// Total number of slots for all scopes
let mut total_number_of_slots: Slot = 0;
@ -136,7 +146,7 @@ impl Mangler {
let frequencies = self.tally_slot_frequencies(
&symbol_table,
&exported_symbols,
&scope_tree,
scope_tree,
total_number_of_slots,
&slots,
);

View file

@ -12,6 +12,7 @@ mod tester;
use oxc_allocator::Allocator;
use oxc_ast::ast::Program;
use oxc_mangler::Mangler;
use oxc_semantic::SemanticBuilder;
pub use oxc_mangler::MangleOptions;
@ -43,11 +44,18 @@ impl Minifier {
}
pub fn build<'a>(self, allocator: &'a Allocator, program: &mut Program<'a>) -> MinifierReturn {
Compressor::new(allocator, self.options.compress).build(program);
let mangler = self
.options
.mangle
.map(|options| Mangler::default().with_options(options).build(program));
let semantic = SemanticBuilder::new().build(program).semantic;
let stats = semantic.stats();
let (symbols, scopes) = semantic.into_symbol_table_and_scope_tree();
Compressor::new(allocator, self.options.compress)
.build_with_symbols_and_scopes(symbols, scopes, program);
let mangler = self.options.mangle.map(|options| {
let semantic = SemanticBuilder::new().with_stats(stats).build(program).semantic;
let (symbols, scopes) = semantic.into_symbol_table_and_scope_tree();
Mangler::default()
.with_options(options)
.build_with_symbols_and_scopes(symbols, &scopes, program)
});
MinifierReturn { mangler }
}
}