Skip to content

Commit

Permalink
refactor(benchmark): transform code before codegen (#7934)
Browse files Browse the repository at this point in the history
@overlookmotel suggested this [here](#7926 (comment)).
  • Loading branch information
camc314 committed Dec 16, 2024
1 parent 84b75a0 commit 9b3a2be
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 5 deletions.
10 changes: 9 additions & 1 deletion tasks/benchmark/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,15 @@ minifier = [
"dep:oxc_span",
"dep:oxc_tasks_common",
]
codegen = ["dep:oxc_allocator", "dep:oxc_codegen", "dep:oxc_parser", "dep:oxc_span", "dep:oxc_tasks_common"]
codegen = [
"dep:oxc_allocator",
"dep:oxc_codegen",
"dep:oxc_parser",
"dep:oxc_semantic",
"dep:oxc_span",
"dep:oxc_tasks_common",
"dep:oxc_transformer",
]
linter = [
"dep:oxc_allocator",
"dep:oxc_linter",
Expand Down
29 changes: 25 additions & 4 deletions tasks/benchmark/benches/codegen.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,49 @@
use std::path::PathBuf;
use std::path::{Path, PathBuf};

use oxc_allocator::Allocator;
use oxc_benchmark::{criterion_group, criterion_main, BenchmarkId, Criterion};
use oxc_codegen::{CodeGenerator, CodegenOptions};
use oxc_parser::Parser;
use oxc_semantic::SemanticBuilder;
use oxc_span::SourceType;
use oxc_tasks_common::TestFiles;
use oxc_transformer::{TransformOptions, Transformer};

fn bench_codegen(criterion: &mut Criterion) {
for file in TestFiles::complicated_one(0).files() {
let id = BenchmarkId::from_parameter(&file.file_name);
let source_type = SourceType::from_path(&file.file_name).unwrap();
let allocator = Allocator::default();
let source_text = &file.source_text;
let ret = Parser::new(&allocator, source_text, source_type).parse();

let mut parser_ret = Parser::new(&allocator, source_text, source_type).parse();
assert!(parser_ret.errors.is_empty());

let mut group = criterion.benchmark_group("codegen");
group.bench_with_input(id.clone(), &ret.program, |b, program| {
group.bench_with_input(id.clone(), &parser_ret.program, |b, program| {
b.iter_with_large_drop(|| CodeGenerator::new().build(program).map);
});
group.finish();

let transformed_program = {
let transform_options = TransformOptions::enable_all();
let (symbols, scopes) = SemanticBuilder::new()
// Estimate transformer will triple scopes, symbols, references
.with_excess_capacity(2.0)
.build(&parser_ret.program)
.semantic
.into_symbol_table_and_scope_tree();

let transformer_ret =
Transformer::new(&allocator, Path::new(&file.file_name), &transform_options)
.build_with_symbols_and_scopes(symbols, scopes, &mut parser_ret.program);

assert!(transformer_ret.errors.is_empty());
parser_ret.program
};

let mut group = criterion.benchmark_group("codegen_sourcemap");
group.bench_with_input(id, &ret.program, |b, program| {
group.bench_with_input(id, &transformed_program, |b, program| {
b.iter_with_large_drop(|| {
CodeGenerator::new()
.with_options(CodegenOptions {
Expand Down

0 comments on commit 9b3a2be

Please sign in to comment.