From 435a89c6e495905dd17d840fa278a0a9c57a9125 Mon Sep 17 00:00:00 2001 From: Boshen <1430279+Boshen@users.noreply.github.com> Date: Tue, 15 Oct 2024 02:21:20 +0000 Subject: [PATCH] refactor(oxc): remove useless `allocator.alloc(program)` calls (#6571) --- crates/oxc_language_server/src/linter.rs | 7 ++++--- crates/oxc_linter/examples/linter.rs | 3 +-- crates/oxc_linter/src/service/runtime.rs | 3 +-- crates/oxc_linter/src/utils/jest.rs | 4 ++-- crates/oxc_minifier/examples/mangler.rs | 5 ++--- crates/oxc_minifier/tests/mod.rs | 6 +++--- crates/oxc_semantic/examples/cfg.rs | 4 ++-- crates/oxc_semantic/examples/simple.rs | 6 +++--- crates/oxc_semantic/src/jsdoc/builder.rs | 4 +--- crates/oxc_semantic/src/jsdoc/parser/jsdoc.rs | 4 +--- crates/oxc_semantic/src/jsdoc/parser/jsdoc_tag.rs | 4 +--- crates/oxc_semantic/src/lib.rs | 3 +-- crates/oxc_semantic/src/module_record/mod.rs | 6 +++--- crates/oxc_semantic/tests/integration/util/mod.rs | 3 +-- .../tests/plugins/inject_global_variables.rs | 8 ++++---- crates/oxc_transformer/tests/plugins/mod.rs | 3 +-- .../tests/plugins/replace_global_defines.rs | 8 ++++---- tasks/benchmark/benches/linter.rs | 8 ++++---- tasks/benchmark/benches/minifier.rs | 12 +++++++----- tasks/benchmark/benches/semantic.rs | 5 ++--- tasks/benchmark/benches/transformer.rs | 7 +++---- tasks/minsize/src/lib.rs | 6 +++--- tasks/rulegen/src/main.rs | 4 +--- 23 files changed, 55 insertions(+), 68 deletions(-) diff --git a/crates/oxc_language_server/src/linter.rs b/crates/oxc_language_server/src/linter.rs index 0abe9f9dc2cb8..cd165a04ede8f 100644 --- a/crates/oxc_language_server/src/linter.rs +++ b/crates/oxc_language_server/src/linter.rs @@ -267,9 +267,10 @@ impl IsolatedLintHandler { return Some(Self::wrap_diagnostics(path, &source_text, reports, start)); }; - let program = allocator.alloc(ret.program); - let semantic_ret = - SemanticBuilder::new().with_cfg(true).with_check_syntax_error(true).build(program); + let semantic_ret = SemanticBuilder::new() + .with_cfg(true) + .with_check_syntax_error(true) + .build(&ret.program); if !semantic_ret.errors.is_empty() { let reports = semantic_ret diff --git a/crates/oxc_linter/examples/linter.rs b/crates/oxc_linter/examples/linter.rs index a2522866781ab..571ec40ca3859 100644 --- a/crates/oxc_linter/examples/linter.rs +++ b/crates/oxc_linter/examples/linter.rs @@ -29,8 +29,7 @@ fn main() -> std::io::Result<()> { return Ok(()); } - let program = allocator.alloc(ret.program); - let semantic_ret = SemanticBuilder::new().build(program); + let semantic_ret = SemanticBuilder::new().build(&ret.program); let mut errors: Vec = vec![]; diff --git a/crates/oxc_linter/src/service/runtime.rs b/crates/oxc_linter/src/service/runtime.rs index 43bcdfb0e1d76..40013d519a185 100644 --- a/crates/oxc_linter/src/service/runtime.rs +++ b/crates/oxc_linter/src/service/runtime.rs @@ -279,8 +279,7 @@ impl Runtime { } } - let program = allocator.alloc(ret.program); - let semantic_ret = semantic_builder.build(program); + let semantic_ret = semantic_builder.build(&ret.program); if !semantic_ret.errors.is_empty() { return semantic_ret.errors.into_iter().map(|err| Message::new(err, None)).collect(); diff --git a/crates/oxc_linter/src/utils/jest.rs b/crates/oxc_linter/src/utils/jest.rs index 27a61b9f02c3a..864868bf5925c 100644 --- a/crates/oxc_linter/src/utils/jest.rs +++ b/crates/oxc_linter/src/utils/jest.rs @@ -317,8 +317,8 @@ mod test { let allocator = Allocator::default(); let source_type = SourceType::default(); let parser_ret = Parser::new(&allocator, "", source_type).parse(); - let program = allocator.alloc(parser_ret.program); - let semantic_ret = SemanticBuilder::new().with_cfg(true).build(program).semantic; + let semantic_ret = + SemanticBuilder::new().with_cfg(true).build(&parser_ret.program).semantic; let semantic_ret = Rc::new(semantic_ret); let build_ctx = |path: &'static str| { diff --git a/crates/oxc_minifier/examples/mangler.rs b/crates/oxc_minifier/examples/mangler.rs index 620bc522027ee..e6a5251e1d782 100644 --- a/crates/oxc_minifier/examples/mangler.rs +++ b/crates/oxc_minifier/examples/mangler.rs @@ -38,7 +38,6 @@ fn main() -> std::io::Result<()> { fn mangler(source_text: &str, source_type: SourceType, debug: bool) -> String { let allocator = Allocator::default(); let ret = Parser::new(&allocator, source_text, source_type).parse(); - let program = allocator.alloc(ret.program); - let mangler = Mangler::new().with_options(MangleOptions { debug }).build(program); - CodeGenerator::new().with_mangler(Some(mangler)).build(program).code + let mangler = Mangler::new().with_options(MangleOptions { debug }).build(&ret.program); + CodeGenerator::new().with_mangler(Some(mangler)).build(&ret.program).code } diff --git a/crates/oxc_minifier/tests/mod.rs b/crates/oxc_minifier/tests/mod.rs index b0417b98c3c95..3fe9dc3ae5519 100644 --- a/crates/oxc_minifier/tests/mod.rs +++ b/crates/oxc_minifier/tests/mod.rs @@ -17,12 +17,12 @@ pub(crate) fn test(source_text: &str, expected: &str, options: CompressOptions) fn run(source_text: &str, source_type: SourceType, options: Option) -> String { let allocator = Allocator::default(); let ret = Parser::new(&allocator, source_text, source_type).parse(); - let program = allocator.alloc(ret.program); + let mut program = ret.program; if let Some(options) = options { - Compressor::new(&allocator, options).build(program); + Compressor::new(&allocator, options).build(&mut program); } CodeGenerator::new() .with_options(CodegenOptions { single_quote: true, ..CodegenOptions::default() }) - .build(program) + .build(&program) .code } diff --git a/crates/oxc_semantic/examples/cfg.rs b/crates/oxc_semantic/examples/cfg.rs index c97fc89bfc0e9..c3e89f2e638b4 100644 --- a/crates/oxc_semantic/examples/cfg.rs +++ b/crates/oxc_semantic/examples/cfg.rs @@ -51,12 +51,12 @@ fn main() -> std::io::Result<()> { return Ok(()); } - let program = allocator.alloc(parser_ret.program); + let program = parser_ret.program; std::fs::write(ast_file_path, format!("{:#?}", &program))?; println!("Wrote AST to: {}", &ast_file_name); let semantic = - SemanticBuilder::new().with_check_syntax_error(true).with_cfg(true).build(program); + SemanticBuilder::new().with_check_syntax_error(true).with_cfg(true).build(&program); if !semantic.errors.is_empty() { let error_message: String = semantic diff --git a/crates/oxc_semantic/examples/simple.rs b/crates/oxc_semantic/examples/simple.rs index a8d2a10af0d6c..82838b6a9a9e0 100644 --- a/crates/oxc_semantic/examples/simple.rs +++ b/crates/oxc_semantic/examples/simple.rs @@ -32,13 +32,13 @@ fn main() -> std::io::Result<()> { return Ok(()); } - let program = allocator.alloc(parser_ret.program); + let program = parser_ret.program; let semantic = SemanticBuilder::new() - .build_module_record(path, program) + .build_module_record(path, &program) // Enable additional syntax checks not performed by the parser .with_check_syntax_error(true) - .build(program); + .build(&program); if !semantic.errors.is_empty() { let error_message: String = semantic diff --git a/crates/oxc_semantic/src/jsdoc/builder.rs b/crates/oxc_semantic/src/jsdoc/builder.rs index f7cbcc7cbae0e..8554b2f22e256 100644 --- a/crates/oxc_semantic/src/jsdoc/builder.rs +++ b/crates/oxc_semantic/src/jsdoc/builder.rs @@ -207,9 +207,7 @@ mod test { ) -> Semantic<'a> { let source_type = source_type.unwrap_or_default(); let ret = Parser::new(allocator, source_text, source_type).parse(); - let program = allocator.alloc(ret.program); - let semantic = SemanticBuilder::new().with_build_jsdoc(true).build(program).semantic; - semantic + SemanticBuilder::new().with_build_jsdoc(true).build(&ret.program).semantic } fn get_jsdocs<'a>( diff --git a/crates/oxc_semantic/src/jsdoc/parser/jsdoc.rs b/crates/oxc_semantic/src/jsdoc/parser/jsdoc.rs index d10bb473c00fb..ec1976b4070b1 100644 --- a/crates/oxc_semantic/src/jsdoc/parser/jsdoc.rs +++ b/crates/oxc_semantic/src/jsdoc/parser/jsdoc.rs @@ -45,9 +45,7 @@ mod test { fn build_semantic<'a>(allocator: &'a Allocator, source_text: &'a str) -> Semantic<'a> { let source_type = SourceType::default(); let ret = Parser::new(allocator, source_text, source_type).parse(); - let program = allocator.alloc(ret.program); - let semantic = SemanticBuilder::new().with_build_jsdoc(true).build(program).semantic; - semantic + SemanticBuilder::new().with_build_jsdoc(true).build(&ret.program).semantic } #[test] diff --git a/crates/oxc_semantic/src/jsdoc/parser/jsdoc_tag.rs b/crates/oxc_semantic/src/jsdoc/parser/jsdoc_tag.rs index 2567f4f42f662..6f309d4ee756d 100644 --- a/crates/oxc_semantic/src/jsdoc/parser/jsdoc_tag.rs +++ b/crates/oxc_semantic/src/jsdoc/parser/jsdoc_tag.rs @@ -194,9 +194,7 @@ mod test { fn build_semantic<'a>(allocator: &'a Allocator, source_text: &'a str) -> Semantic<'a> { let source_type = SourceType::default(); let ret = Parser::new(allocator, source_text, source_type).parse(); - let program = allocator.alloc(ret.program); - let semantic = SemanticBuilder::new().with_build_jsdoc(true).build(program).semantic; - semantic + SemanticBuilder::new().with_build_jsdoc(true).build(&ret.program).semantic } #[test] diff --git a/crates/oxc_semantic/src/lib.rs b/crates/oxc_semantic/src/lib.rs index 54bd7ebae3f88..09a883476db0a 100644 --- a/crates/oxc_semantic/src/lib.rs +++ b/crates/oxc_semantic/src/lib.rs @@ -252,8 +252,7 @@ mod tests { ) -> Semantic<'s> { let parse = oxc_parser::Parser::new(allocator, source, source_type).parse(); assert!(parse.errors.is_empty()); - let program = allocator.alloc(parse.program); - let semantic = SemanticBuilder::new().build(program); + let semantic = SemanticBuilder::new().build(&parse.program); assert!(semantic.errors.is_empty(), "Parse error: {}", semantic.errors[0]); semantic.semantic } diff --git a/crates/oxc_semantic/src/module_record/mod.rs b/crates/oxc_semantic/src/module_record/mod.rs index 879aaeae2856a..388d664dcd258 100644 --- a/crates/oxc_semantic/src/module_record/mod.rs +++ b/crates/oxc_semantic/src/module_record/mod.rs @@ -18,9 +18,9 @@ mod module_record_tests { let source_type = SourceType::mjs(); let allocator = Allocator::default(); let ret = Parser::new(&allocator, source_text, source_type).parse(); - let program = allocator.alloc(ret.program); - let semantic_ret = - SemanticBuilder::new().build_module_record(Path::new(""), program).build(program); + let semantic_ret = SemanticBuilder::new() + .build_module_record(Path::new(""), &ret.program) + .build(&ret.program); Arc::clone(&semantic_ret.semantic.module_record) } diff --git a/crates/oxc_semantic/tests/integration/util/mod.rs b/crates/oxc_semantic/tests/integration/util/mod.rs index 77e9e50253872..ac53740f5175b 100644 --- a/crates/oxc_semantic/tests/integration/util/mod.rs +++ b/crates/oxc_semantic/tests/integration/util/mod.rs @@ -166,12 +166,11 @@ impl<'a> SemanticTester<'a> { .collect::() ); - let program = self.allocator.alloc(parse.program); SemanticBuilder::new() .with_check_syntax_error(true) .with_cfg(self.cfg) .with_scope_tree_child_ids(self.scope_tree_child_ids) - .build(program) + .build(&parse.program) } pub fn basic_blocks_count(&self) -> usize { diff --git a/crates/oxc_transformer/tests/plugins/inject_global_variables.rs b/crates/oxc_transformer/tests/plugins/inject_global_variables.rs index 015664ecc0149..0e33319849c8f 100644 --- a/crates/oxc_transformer/tests/plugins/inject_global_variables.rs +++ b/crates/oxc_transformer/tests/plugins/inject_global_variables.rs @@ -15,13 +15,13 @@ pub(crate) fn test(source_text: &str, expected: &str, config: InjectGlobalVariab let source_type = SourceType::default(); let allocator = Allocator::default(); let ret = Parser::new(&allocator, source_text, source_type).parse(); - let program = allocator.alloc(ret.program); + let mut program = ret.program; let (symbols, scopes) = - SemanticBuilder::new().build(program).semantic.into_symbol_table_and_scope_tree(); - let _ = InjectGlobalVariables::new(&allocator, config).build(symbols, scopes, program); + SemanticBuilder::new().build(&program).semantic.into_symbol_table_and_scope_tree(); + let _ = InjectGlobalVariables::new(&allocator, config).build(symbols, scopes, &mut program); let result = CodeGenerator::new() .with_options(CodegenOptions { single_quote: true, ..CodegenOptions::default() }) - .build(program) + .build(&program) .code; let expected = run(expected, source_type); assert_eq!(result, expected, "for source {source_text}"); diff --git a/crates/oxc_transformer/tests/plugins/mod.rs b/crates/oxc_transformer/tests/plugins/mod.rs index 75315e1bc9055..83b1e17463969 100644 --- a/crates/oxc_transformer/tests/plugins/mod.rs +++ b/crates/oxc_transformer/tests/plugins/mod.rs @@ -9,9 +9,8 @@ use oxc_span::SourceType; fn run(source_text: &str, source_type: SourceType) -> String { let allocator = Allocator::default(); let ret = Parser::new(&allocator, source_text, source_type).parse(); - let program = allocator.alloc(ret.program); CodeGenerator::new() .with_options(CodegenOptions { single_quote: true, ..CodegenOptions::default() }) - .build(program) + .build(&ret.program) .code } diff --git a/crates/oxc_transformer/tests/plugins/replace_global_defines.rs b/crates/oxc_transformer/tests/plugins/replace_global_defines.rs index b5e15b333e4ac..b2d8d07e7cde1 100644 --- a/crates/oxc_transformer/tests/plugins/replace_global_defines.rs +++ b/crates/oxc_transformer/tests/plugins/replace_global_defines.rs @@ -11,13 +11,13 @@ pub(crate) fn test(source_text: &str, expected: &str, config: ReplaceGlobalDefin let source_type = SourceType::default(); let allocator = Allocator::default(); let ret = Parser::new(&allocator, source_text, source_type).parse(); - let program = allocator.alloc(ret.program); + let mut program = ret.program; let (symbols, scopes) = - SemanticBuilder::new().build(program).semantic.into_symbol_table_and_scope_tree(); - let _ = ReplaceGlobalDefines::new(&allocator, config).build(symbols, scopes, program); + SemanticBuilder::new().build(&program).semantic.into_symbol_table_and_scope_tree(); + let _ = ReplaceGlobalDefines::new(&allocator, config).build(symbols, scopes, &mut program); let result = CodeGenerator::new() .with_options(CodegenOptions { single_quote: true, ..CodegenOptions::default() }) - .build(program) + .build(&program) .code; let expected = run(expected, source_type); assert_eq!(result, expected, "for source {source_text}"); diff --git a/tasks/benchmark/benches/linter.rs b/tasks/benchmark/benches/linter.rs index 7fa52aee27bd3..28af0b4d0e880 100644 --- a/tasks/benchmark/benches/linter.rs +++ b/tasks/benchmark/benches/linter.rs @@ -27,15 +27,15 @@ fn bench_linter(criterion: &mut Criterion) { |b, source_text| { let allocator = Allocator::default(); let ret = Parser::new(&allocator, source_text, source_type).parse(); - let program = allocator.alloc(ret.program); + let path = Path::new(""); let semantic_ret = SemanticBuilder::new() .with_build_jsdoc(true) .with_cfg(true) - .build_module_record(Path::new(""), program) - .build(program); + .build_module_record(path, &ret.program) + .build(&ret.program); let linter = LinterBuilder::all().with_fix(FixKind::All).build(); let semantic = Rc::new(semantic_ret.semantic); - b.iter(|| linter.run(Path::new(std::ffi::OsStr::new("")), Rc::clone(&semantic))); + b.iter(|| linter.run(path, Rc::clone(&semantic))); }, ); } diff --git a/tasks/benchmark/benches/minifier.rs b/tasks/benchmark/benches/minifier.rs index cffac57cf136b..486a5f119c727 100644 --- a/tasks/benchmark/benches/minifier.rs +++ b/tasks/benchmark/benches/minifier.rs @@ -24,18 +24,20 @@ fn bench_minifier(criterion: &mut Criterion) { allocator.reset(); // Create fresh AST + semantic data for each iteration - let program = Parser::new(&allocator, source_text, source_type).parse().program; - let program = allocator.alloc(program); + let mut program = Parser::new(&allocator, source_text, source_type).parse().program; let (symbols, scopes) = SemanticBuilder::new() - .build(program) + .build(&program) .semantic .into_symbol_table_and_scope_tree(); let options = CompressOptions::all_true(); runner.run(|| { - Compressor::new(&allocator, options) - .build_with_symbols_and_scopes(symbols, scopes, program); + Compressor::new(&allocator, options).build_with_symbols_and_scopes( + symbols, + scopes, + &mut program, + ); }); }); }); diff --git a/tasks/benchmark/benches/semantic.rs b/tasks/benchmark/benches/semantic.rs index 7ee1189ddc611..764d59cde2555 100644 --- a/tasks/benchmark/benches/semantic.rs +++ b/tasks/benchmark/benches/semantic.rs @@ -17,7 +17,6 @@ fn bench_semantic(criterion: &mut Criterion) { |b, source_text| { let allocator = Allocator::default(); let ret = Parser::new(&allocator, source_text, source_type).parse(); - let program = allocator.alloc(black_box(ret.program)); b.iter_with_large_drop(|| { // We drop `Semantic` inside this closure as drop time is part of cost of using this API. // We return `error`s to be dropped outside of the measured section, as usually @@ -25,8 +24,8 @@ fn bench_semantic(criterion: &mut Criterion) { // but that's atypical, so don't want to include it in benchmark time. let ret = SemanticBuilder::new() .with_build_jsdoc(true) - .build_module_record(Path::new(""), program) - .build(program); + .build_module_record(Path::new(""), &ret.program) + .build(&ret.program); let ret = black_box(ret); ret.errors }); diff --git a/tasks/benchmark/benches/transformer.rs b/tasks/benchmark/benches/transformer.rs index 663d6fbcd289e..900f81d6e5aee 100644 --- a/tasks/benchmark/benches/transformer.rs +++ b/tasks/benchmark/benches/transformer.rs @@ -26,13 +26,12 @@ fn bench_transformer(criterion: &mut Criterion) { allocator.reset(); // Create fresh AST + semantic data for each iteration - let ParserReturn { program, .. } = + let ParserReturn { mut program, .. } = Parser::new(&allocator, source_text, source_type).parse(); - let program = allocator.alloc(program); let (symbols, scopes) = SemanticBuilder::new() // Estimate transformer will triple scopes, symbols, references .with_excess_capacity(2.0) - .build(program) + .build(&program) .semantic .into_symbol_table_and_scope_tree(); @@ -44,7 +43,7 @@ fn bench_transformer(criterion: &mut Criterion) { runner.run(|| { let ret = Transformer::new(&allocator, Path::new(&file.file_name), options) - .build_with_symbols_and_scopes(symbols, scopes, program); + .build_with_symbols_and_scopes(symbols, scopes, &mut program); // Return the `TransformerReturn`, so it's dropped outside of the measured section. // `TransformerReturn` contains `ScopeTree` and `SymbolTable` which are costly to drop. diff --git a/tasks/minsize/src/lib.rs b/tasks/minsize/src/lib.rs index 1c8bf6f6401e2..8966c9494cf60 100644 --- a/tasks/minsize/src/lib.rs +++ b/tasks/minsize/src/lib.rs @@ -109,12 +109,12 @@ fn minify_twice(file: &TestFile) -> String { fn minify(source_text: &str, source_type: SourceType, options: MinifierOptions) -> String { let allocator = Allocator::default(); let ret = Parser::new(&allocator, source_text, source_type).parse(); - let program = allocator.alloc(ret.program); - let ret = Minifier::new(options).build(&allocator, program); + let mut program = ret.program; + let ret = Minifier::new(options).build(&allocator, &mut program); CodeGenerator::new() .with_options(CodegenOptions { minify: true, ..CodegenOptions::default() }) .with_mangler(ret.mangler) - .build(program) + .build(&program) .code } diff --git a/tasks/rulegen/src/main.rs b/tasks/rulegen/src/main.rs index 638c335dafc13..bd71dae459e15 100644 --- a/tasks/rulegen/src/main.rs +++ b/tasks/rulegen/src/main.rs @@ -695,10 +695,8 @@ fn main() { let source_type = SourceType::from_path(rule_test_path).expect("incorrect {path:?}"); let ret = Parser::new(&allocator, &body, source_type).parse(); - let program = allocator.alloc(ret.program); - let mut state = State::new(&body); - state.visit_program(program); + state.visit_program(&ret.program); let pass_cases = state.pass_cases(); let fail_cases = state.fail_cases();