Skip to content

Commit 99fa7c4

Browse files
author
Cameron McHenry
committed
refactor(mangler): use external allocator
1 parent 9fda4f5 commit 99fa7c4

File tree

6 files changed

+36
-22
lines changed

6 files changed

+36
-22
lines changed

crates/oxc/src/compiler.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,8 @@ pub trait CompilerInterface {
208208

209209
/* Mangler */
210210

211-
let mangler = self.mangle_options().map(|options| self.mangle(&mut program, options));
211+
let mangler =
212+
self.mangle_options().map(|options| self.mangle(&allocator, &mut program, options));
212213

213214
/* Codegen */
214215

@@ -279,8 +280,13 @@ pub trait CompilerInterface {
279280
Compressor::new(allocator, options).build(program);
280281
}
281282

282-
fn mangle(&self, program: &mut Program<'_>, options: MangleOptions) -> Scoping {
283-
Mangler::new().with_options(options).build(program)
283+
fn mangle(
284+
&self,
285+
allocator: &Allocator,
286+
program: &mut Program<'_>,
287+
options: MangleOptions,
288+
) -> Scoping {
289+
Mangler::new(allocator).with_options(options).build(program)
284290
}
285291

286292
fn codegen(

crates/oxc_mangler/src/lib.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -155,15 +155,15 @@ type Slot = usize;
155155
/// - slot 1: `top_level_b`, `foo_a`, `bar_a`
156156
/// - slot 2: `foo`
157157
/// - slot 3: `bar`
158-
#[derive(Default)]
159-
pub struct Mangler {
158+
pub struct Mangler<'m> {
160159
options: MangleOptions,
160+
allocator: &'m Allocator,
161161
}
162162

163-
impl Mangler {
163+
impl<'m> Mangler<'m> {
164164
#[must_use]
165-
pub fn new() -> Self {
166-
Self::default()
165+
pub fn new(allocator: &'m Allocator) -> Self {
166+
Self { options: MangleOptions::default(), allocator }
167167
}
168168

169169
#[must_use]
@@ -216,10 +216,8 @@ impl Mangler {
216216
let (keep_name_names, keep_name_symbols) =
217217
Mangler::collect_keep_name_symbols(self.options.keep_names, scoping, ast_nodes);
218218

219-
let allocator = Allocator::default();
220-
221219
// All symbols with their assigned slots. Keyed by symbol id.
222-
let mut slots = Vec::from_iter_in(iter::repeat_n(0, scoping.symbols_len()), &allocator);
220+
let mut slots = Vec::from_iter_in(iter::repeat_n(0, scoping.symbols_len()), self.allocator);
223221

224222
// Stores the lived scope ids for each slot. Keyed by slot number.
225223
let mut slot_liveness: std::vec::Vec<FixedBitSet> = vec![];
@@ -300,13 +298,13 @@ impl Mangler {
300298
&keep_name_symbols,
301299
total_number_of_slots,
302300
&slots,
303-
&allocator,
301+
self.allocator,
304302
);
305303

306304
let root_unresolved_references = scoping.root_unresolved_references();
307305
let root_bindings = scoping.get_bindings(scoping.root_scope_id());
308306

309-
let mut reserved_names = Vec::with_capacity_in(total_number_of_slots, &allocator);
307+
let mut reserved_names = Vec::with_capacity_in(total_number_of_slots, self.allocator);
310308

311309
let mut count = 0;
312310
for _ in 0..total_number_of_slots {

crates/oxc_minifier/examples/mangler.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,26 +24,32 @@ fn main() -> std::io::Result<()> {
2424
let source_text = std::fs::read_to_string(path)?;
2525
let source_type = SourceType::from_path(path).unwrap();
2626

27+
let mut allocator = Allocator::default();
2728
let options = MangleOptions {
2829
top_level: source_type.is_module(),
2930
keep_names: MangleOptionsKeepNames { function: keep_names, class: keep_names },
3031
debug,
3132
};
32-
let printed = mangler(&source_text, source_type, options);
33+
let printed = mangler(&allocator, &source_text, source_type, options);
3334
println!("{printed}");
3435

3536
if twice {
36-
let printed2 = mangler(&printed, source_type, options);
37+
allocator.reset();
38+
let printed2 = mangler(&allocator, &printed, source_type, options);
3739
println!("{printed2}");
3840
println!("same = {}", printed == printed2);
3941
}
4042

4143
Ok(())
4244
}
4345

44-
fn mangler(source_text: &str, source_type: SourceType, options: MangleOptions) -> String {
45-
let allocator = Allocator::default();
46-
let ret = Parser::new(&allocator, source_text, source_type).parse();
47-
let symbol_table = Mangler::new().with_options(options).build(&ret.program);
46+
fn mangler(
47+
allocator: &Allocator,
48+
source_text: &str,
49+
source_type: SourceType,
50+
options: MangleOptions,
51+
) -> String {
52+
let ret = Parser::new(allocator, source_text, source_type).parse();
53+
let symbol_table = Mangler::new(allocator).with_options(options).build(&ret.program);
4854
Codegen::new().with_scoping(Some(symbol_table)).build(&ret.program).code
4955
}

crates/oxc_minifier/src/lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,9 @@ impl Minifier {
6363
.with_scope_tree_child_ids(true)
6464
.build(program)
6565
.semantic;
66-
Mangler::default().with_options(options).build_with_semantic(&mut semantic, program);
66+
Mangler::new(allocator)
67+
.with_options(options)
68+
.build_with_semantic(&mut semantic, program);
6769
semantic.into_scoping()
6870
});
6971
MinifierReturn { scoping }

crates/oxc_minifier/tests/mangler/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ fn mangle(source_text: &str, options: MangleOptions) -> String {
1111
let source_type = SourceType::mjs();
1212
let ret = Parser::new(&allocator, source_text, source_type).parse();
1313
let program = ret.program;
14-
let symbol_table = Mangler::new().with_options(options).build(&program);
14+
let symbol_table = Mangler::new(&allocator).with_options(options).build(&program);
1515
Codegen::new().with_scoping(Some(symbol_table)).build(&program).code
1616
}
1717

tasks/benchmark/benches/minifier.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,16 @@ fn bench_mangler(criterion: &mut Criterion) {
5757
let source_type = SourceType::from_path(&file.file_name).unwrap();
5858
let source_text = file.source_text.as_str();
5959
let mut allocator = Allocator::default();
60+
let mut mangler_allocator = Allocator::default();
6061
group.bench_function(id, |b| {
6162
b.iter_with_setup_wrapper(|runner| {
6263
allocator.reset();
64+
mangler_allocator.reset();
6365
let program = Parser::new(&allocator, source_text, source_type).parse().program;
6466
let mut semantic =
6567
SemanticBuilder::new().with_scope_tree_child_ids(true).build(&program).semantic;
6668
runner.run(|| {
67-
Mangler::new().build_with_semantic(&mut semantic, &program);
69+
Mangler::new(&mangler_allocator).build_with_semantic(&mut semantic, &program);
6870
});
6971
});
7072
});

0 commit comments

Comments
 (0)