Skip to content

Commit

Permalink
refactor(oxc): remove useless allocator.alloc(program) calls (#6571)
Browse files Browse the repository at this point in the history
  • Loading branch information
Boshen committed Oct 15, 2024
1 parent ef237cf commit 435a89c
Show file tree
Hide file tree
Showing 23 changed files with 55 additions and 68 deletions.
7 changes: 4 additions & 3 deletions crates/oxc_language_server/src/linter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions crates/oxc_linter/examples/linter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<OxcDiagnostic> = vec![];

Expand Down
3 changes: 1 addition & 2 deletions crates/oxc_linter/src/service/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions crates/oxc_linter/src/utils/jest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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| {
Expand Down
5 changes: 2 additions & 3 deletions crates/oxc_minifier/examples/mangler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
6 changes: 3 additions & 3 deletions crates/oxc_minifier/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<CompressOptions>) -> 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
}
4 changes: 2 additions & 2 deletions crates/oxc_semantic/examples/cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions crates/oxc_semantic/examples/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 1 addition & 3 deletions crates/oxc_semantic/src/jsdoc/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>(
Expand Down
4 changes: 1 addition & 3 deletions crates/oxc_semantic/src/jsdoc/parser/jsdoc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
4 changes: 1 addition & 3 deletions crates/oxc_semantic/src/jsdoc/parser/jsdoc_tag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
3 changes: 1 addition & 2 deletions crates/oxc_semantic/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
6 changes: 3 additions & 3 deletions crates/oxc_semantic/src/module_record/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
3 changes: 1 addition & 2 deletions crates/oxc_semantic/tests/integration/util/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,12 +166,11 @@ impl<'a> SemanticTester<'a> {
.collect::<String>()
);

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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}");
Expand Down
3 changes: 1 addition & 2 deletions crates/oxc_transformer/tests/plugins/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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}");
Expand Down
8 changes: 4 additions & 4 deletions tasks/benchmark/benches/linter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
},
);
}
Expand Down
12 changes: 7 additions & 5 deletions tasks/benchmark/benches/minifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);
});
});
});
Expand Down
5 changes: 2 additions & 3 deletions tasks/benchmark/benches/semantic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,15 @@ 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
// code would have no errors. One of our benchmarks `cal.com.tsx` has a lot of errors,
// 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
});
Expand Down
7 changes: 3 additions & 4 deletions tasks/benchmark/benches/transformer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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.
Expand Down
6 changes: 3 additions & 3 deletions tasks/minsize/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
4 changes: 1 addition & 3 deletions tasks/rulegen/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 435a89c

Please sign in to comment.