Skip to content

Commit 509b192

Browse files
committed
refactor: use a single transform option
1 parent b7e58e8 commit 509b192

File tree

3 files changed

+47
-25
lines changed

3 files changed

+47
-25
lines changed

benches/transformer.rs

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,34 @@ static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;
88

99
trait TheBencher {
1010
type RunOutput;
11+
type Options: Send + Sync;
1112

1213
const ID: &'static str;
1314

14-
fn run(path: &Path, source: &str) -> Self::RunOutput;
15+
fn run(path: &Path, source: &str, options: &Self::Options) -> Self::RunOutput;
1516

16-
fn bench(g: &mut BenchmarkGroup<'_, WallTime>, path: &Path, source: &str) {
17+
fn bench(
18+
g: &mut BenchmarkGroup<'_, WallTime>,
19+
path: &Path,
20+
source: &str,
21+
options: &Self::Options,
22+
) {
1723
let cpus = num_cpus::get_physical();
1824
let id = BenchmarkId::new(Self::ID, "single-thread");
19-
g.bench_with_input(id, &source, |b, source| b.iter(|| Self::run(path, source)));
25+
g.bench_with_input(id, &source, |b, source| {
26+
b.iter(|| Self::run(path, source, options))
27+
});
2028

2129
let id = BenchmarkId::new(Self::ID, "no-drop");
2230
g.bench_with_input(id, &source, |b, source| {
23-
b.iter_with_large_drop(|| Self::run(path, source))
31+
b.iter_with_large_drop(|| Self::run(path, source, options))
2432
});
2533

2634
let id = BenchmarkId::new(Self::ID, "parallel");
2735
g.bench_with_input(id, &source, |b, source| {
2836
b.iter(|| {
2937
(0..cpus).into_par_iter().for_each(|_| {
30-
Self::run(path, source);
38+
Self::run(path, source, options);
3139
});
3240
})
3341
});
@@ -38,22 +46,24 @@ struct OxcBencher;
3846

3947
impl TheBencher for OxcBencher {
4048
type RunOutput = (oxc::allocator::Allocator, String);
49+
type Options = oxc::transformer::TransformOptions;
4150

4251
const ID: &'static str = "oxc";
4352

44-
fn run(path: &Path, source_text: &str) -> Self::RunOutput {
45-
bench_transformer::oxc::transform(path, source_text)
53+
fn run(path: &Path, source_text: &str, options: &Self::Options) -> Self::RunOutput {
54+
bench_transformer::oxc::transform(path, source_text, options)
4655
}
4756
}
4857

4958
struct SwcBencher;
5059

5160
impl TheBencher for SwcBencher {
5261
type RunOutput = String;
62+
type Options = ();
5363

5464
const ID: &'static str = "swc";
5565

56-
fn run(path: &Path, source_text: &str) -> Self::RunOutput {
66+
fn run(path: &Path, source_text: &str, _option: &Self::Options) -> Self::RunOutput {
5767
bench_transformer::swc::transform(path, source_text)
5868
}
5969
}
@@ -64,8 +74,11 @@ fn transformer_benchmark(c: &mut Criterion) {
6474
let path = Path::new("files").join(filename);
6575
let source = std::fs::read_to_string(&path).unwrap();
6676
let mut g = c.benchmark_group(filename);
67-
OxcBencher::bench(&mut g, &path, &source);
68-
SwcBencher::bench(&mut g, &path, &source);
77+
78+
let options = oxc::transformer::TransformOptions::from_target("es2015").unwrap();
79+
OxcBencher::bench(&mut g, &path, &source, &options);
80+
81+
SwcBencher::bench(&mut g, &path, &source, &());
6982
g.finish();
7083
}
7184
}

src/lib.rs

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,30 @@ pub mod oxc {
1010
transformer::{TransformOptions, Transformer},
1111
};
1212

13-
pub fn transform(path: &Path, source_text: &str) -> (Allocator, String) {
13+
pub fn transform_options() -> TransformOptions {
14+
TransformOptions::from_target("es2015").unwrap()
15+
}
16+
17+
pub fn transform(
18+
path: &Path,
19+
source_text: &str,
20+
options: &TransformOptions,
21+
) -> (Allocator, String) {
1422
let allocator = Allocator::default();
1523
let source_type = SourceType::from_path(path).unwrap();
16-
let printed = {
17-
let ret = Parser::new(&allocator, source_text, source_type).parse();
18-
let mut program = ret.program;
19-
let transform_options = TransformOptions::from_target("es2015").unwrap();
20-
let (symbols, scopes) = SemanticBuilder::new()
21-
.build(&program)
22-
.semantic
23-
.into_symbol_table_and_scope_tree();
24-
let ret = Transformer::new(&allocator, path, &transform_options)
25-
.build_with_symbols_and_scopes(symbols, scopes, &mut program);
26-
assert!(ret.errors.is_empty());
27-
CodeGenerator::new().build(&program).code
28-
};
24+
let ret = Parser::new(&allocator, source_text, source_type).parse();
25+
let mut program = ret.program;
26+
let (symbols, scopes) = SemanticBuilder::new()
27+
.build(&program)
28+
.semantic
29+
.into_symbol_table_and_scope_tree();
30+
let ret = Transformer::new(&allocator, path, options).build_with_symbols_and_scopes(
31+
symbols,
32+
scopes,
33+
&mut program,
34+
);
35+
assert!(ret.errors.is_empty());
36+
let printed = CodeGenerator::new().build(&program).code;
2937

3038
(allocator, printed)
3139
}

src/oxc.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ pub fn main() {
99
let path = env::args().nth(1).unwrap();
1010
let path = Path::new(&path);
1111
let source_text = fs::read_to_string(path).unwrap();
12-
let _output = oxc::transform(path, &source_text);
12+
let options = oxc::transform_options();
13+
let _output = oxc::transform(path, &source_text, &options);
1314
// println!("{}", output.1);
1415
}

0 commit comments

Comments
 (0)