refactor(oxc): remove useless allocator.alloc(program) calls (#6571)

This commit is contained in:
Boshen 2024-10-15 02:21:20 +00:00
parent ef237cf8ff
commit 435a89c6e4
23 changed files with 55 additions and 68 deletions

View file

@ -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

View file

@ -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![];

View file

@ -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();

View file

@ -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| {

View file

@ -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
}

View file

@ -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
}

View file

@ -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

View file

@ -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

View file

@ -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>(

View file

@ -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]

View file

@ -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]

View file

@ -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
}

View file

@ -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)
}

View file

@ -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 {

View file

@ -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}");

View file

@ -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
}

View file

@ -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}");

View file

@ -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)));
},
);
}

View file

@ -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,
);
});
});
});

View file

@ -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
});

View file

@ -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.

View file

@ -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
}

View file

@ -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();