2020-08-26 14:50:18 +03:00
|
|
|
#![feature(test)]
|
2021-08-13 12:39:13 +03:00
|
|
|
#![feature(bench_black_box)]
|
2020-08-26 14:50:18 +03:00
|
|
|
|
|
|
|
extern crate test;
|
|
|
|
|
2021-08-13 10:05:40 +03:00
|
|
|
use std::{
|
|
|
|
hint::black_box,
|
|
|
|
io::{self, stderr},
|
|
|
|
sync::Arc,
|
2020-08-26 14:50:18 +03:00
|
|
|
};
|
2021-08-13 10:05:40 +03:00
|
|
|
use swc::config::{Config, JscConfig, Options, SourceMapsConfig};
|
|
|
|
use swc_common::{errors::Handler, FileName, FilePathMapping, SourceMap};
|
2020-08-26 14:50:18 +03:00
|
|
|
use swc_ecma_ast::Program;
|
|
|
|
use swc_ecma_parser::{JscTarget, Syntax, TsConfig};
|
|
|
|
use swc_ecma_transforms::{fixer, hygiene, resolver, typescript};
|
|
|
|
use swc_ecma_visit::FoldWith;
|
|
|
|
use test::Bencher;
|
|
|
|
|
|
|
|
static SOURCE: &str = include_str!("assets/AjaxObservable.ts");
|
|
|
|
|
|
|
|
fn mk() -> swc::Compiler {
|
|
|
|
let cm = Arc::new(SourceMap::new(FilePathMapping::empty()));
|
|
|
|
|
2021-08-13 10:05:40 +03:00
|
|
|
let c = swc::Compiler::new(cm.clone());
|
2020-08-26 14:50:18 +03:00
|
|
|
|
|
|
|
c
|
|
|
|
}
|
|
|
|
|
|
|
|
fn parse(c: &swc::Compiler) -> Program {
|
|
|
|
let fm = c.cm.new_source_file(
|
|
|
|
FileName::Real("rxjs/src/internal/observable/dom/AjaxObservable.ts".into()),
|
|
|
|
SOURCE.to_string(),
|
|
|
|
);
|
2021-08-13 10:05:40 +03:00
|
|
|
let handler = Handler::with_emitter_writer(Box::new(io::stderr()), Some(c.cm.clone()));
|
|
|
|
|
2020-08-26 14:50:18 +03:00
|
|
|
c.parse_js(
|
|
|
|
fm,
|
2021-08-13 10:05:40 +03:00
|
|
|
&handler,
|
2020-08-26 14:50:18 +03:00
|
|
|
JscTarget::Es5,
|
|
|
|
Syntax::Typescript(Default::default()),
|
|
|
|
true,
|
|
|
|
true,
|
|
|
|
)
|
|
|
|
.unwrap()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn as_es(c: &swc::Compiler) -> Program {
|
|
|
|
let program = parse(c);
|
|
|
|
|
|
|
|
program.fold_with(&mut typescript::strip())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn base_tr_fixer(b: &mut Bencher) {
|
|
|
|
let c = mk();
|
|
|
|
let module = as_es(&c);
|
|
|
|
|
|
|
|
b.iter(|| {
|
2021-08-13 10:05:40 +03:00
|
|
|
let handler = Handler::with_emitter_writer(Box::new(stderr()), Some(c.cm.clone()));
|
|
|
|
|
|
|
|
black_box(c.run_transform(&handler, true, || {
|
2020-08-26 14:50:18 +03:00
|
|
|
module.clone().fold_with(&mut fixer(Some(c.comments())))
|
|
|
|
}))
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn base_tr_resolver_and_hygiene(b: &mut Bencher) {
|
|
|
|
let c = mk();
|
|
|
|
let module = as_es(&c);
|
|
|
|
|
|
|
|
b.iter(|| {
|
2021-08-13 10:05:40 +03:00
|
|
|
let handler = Handler::with_emitter_writer(Box::new(stderr()), Some(c.cm.clone()));
|
|
|
|
|
|
|
|
black_box(c.run_transform(&handler, true, || {
|
2020-08-26 14:50:18 +03:00
|
|
|
module
|
|
|
|
.clone()
|
|
|
|
.fold_with(&mut resolver())
|
|
|
|
.fold_with(&mut hygiene())
|
|
|
|
}))
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/// This benchmark exists to know exact execution time of each pass.
|
|
|
|
#[bench]
|
|
|
|
fn config_for_file(b: &mut Bencher) {
|
|
|
|
let c = mk();
|
|
|
|
|
|
|
|
b.iter(|| {
|
2021-08-13 10:05:40 +03:00
|
|
|
let handler = Handler::with_emitter_writer(Box::new(stderr()), Some(c.cm.clone()));
|
|
|
|
|
2020-08-26 14:50:18 +03:00
|
|
|
black_box(c.config_for_file(
|
2021-08-13 10:05:40 +03:00
|
|
|
&handler,
|
2020-08-26 14:50:18 +03:00
|
|
|
&Options {
|
2021-04-08 15:55:02 +03:00
|
|
|
config: Config {
|
2020-08-26 14:50:18 +03:00
|
|
|
jsc: JscConfig {
|
2021-04-08 15:55:02 +03:00
|
|
|
target: Some(JscTarget::Es2016),
|
2020-08-26 14:50:18 +03:00
|
|
|
syntax: Some(Syntax::Typescript(TsConfig {
|
|
|
|
..Default::default()
|
|
|
|
})),
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
module: None,
|
|
|
|
..Default::default()
|
2021-04-08 15:55:02 +03:00
|
|
|
},
|
2020-08-26 14:50:18 +03:00
|
|
|
swcrc: false,
|
|
|
|
is_module: true,
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
&FileName::Real("rxjs/src/internal/observable/dom/AjaxObservable.ts".into()),
|
|
|
|
))
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/// This benchmark exists to know exact execution time of each pass.
|
|
|
|
|
|
|
|
fn bench_codegen(b: &mut Bencher, _target: JscTarget) {
|
|
|
|
let c = mk();
|
|
|
|
let module = as_es(&c);
|
|
|
|
|
|
|
|
//TODO: Use target
|
|
|
|
|
|
|
|
b.iter(|| {
|
|
|
|
black_box(
|
2020-12-03 23:03:26 +03:00
|
|
|
c.print(
|
|
|
|
&module,
|
2021-07-08 10:32:06 +03:00
|
|
|
None,
|
2021-08-02 18:49:34 +03:00
|
|
|
None,
|
2020-12-03 23:03:26 +03:00
|
|
|
JscTarget::Es2020,
|
|
|
|
SourceMapsConfig::Bool(false),
|
|
|
|
None,
|
|
|
|
false,
|
|
|
|
)
|
|
|
|
.unwrap(),
|
2020-08-26 14:50:18 +03:00
|
|
|
);
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! codegen {
|
|
|
|
($name:ident, $target:expr) => {
|
|
|
|
#[bench]
|
|
|
|
fn $name(b: &mut Bencher) {
|
|
|
|
bench_codegen(b, $target);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
codegen!(codegen_es3, JscTarget::Es3);
|
|
|
|
codegen!(codegen_es5, JscTarget::Es5);
|
|
|
|
codegen!(codegen_es2015, JscTarget::Es2015);
|
|
|
|
codegen!(codegen_es2016, JscTarget::Es2016);
|
|
|
|
codegen!(codegen_es2017, JscTarget::Es2017);
|
|
|
|
codegen!(codegen_es2018, JscTarget::Es2018);
|
|
|
|
codegen!(codegen_es2019, JscTarget::Es2019);
|
|
|
|
codegen!(codegen_es2020, JscTarget::Es2020);
|
|
|
|
|
|
|
|
fn bench_full(b: &mut Bencher, opts: &Options) {
|
|
|
|
let c = mk();
|
|
|
|
|
|
|
|
b.iter(|| {
|
2021-08-13 10:05:40 +03:00
|
|
|
let handler = Handler::with_emitter_writer(Box::new(stderr()), Some(c.cm.clone()));
|
|
|
|
|
2020-08-26 14:50:18 +03:00
|
|
|
let fm = c.cm.new_source_file(
|
|
|
|
FileName::Real("rxjs/src/internal/observable/dom/AjaxObservable.ts".into()),
|
|
|
|
SOURCE.to_string(),
|
|
|
|
);
|
2021-08-13 10:05:40 +03:00
|
|
|
c.process_js_file(fm, &handler, opts)
|
2020-08-26 14:50:18 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! compat {
|
|
|
|
($name:ident, $target:expr) => {
|
|
|
|
#[bench]
|
|
|
|
fn $name(b: &mut Bencher) {
|
|
|
|
bench_full(
|
|
|
|
b,
|
|
|
|
&Options {
|
2021-04-08 15:55:02 +03:00
|
|
|
config: Config {
|
2020-08-26 14:50:18 +03:00
|
|
|
jsc: JscConfig {
|
2021-04-08 15:55:02 +03:00
|
|
|
target: Some($target),
|
2020-08-26 14:50:18 +03:00
|
|
|
syntax: Some(Syntax::Typescript(TsConfig {
|
|
|
|
..Default::default()
|
|
|
|
})),
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
module: None,
|
|
|
|
..Default::default()
|
2021-04-08 15:55:02 +03:00
|
|
|
},
|
2020-08-26 14:50:18 +03:00
|
|
|
swcrc: false,
|
|
|
|
is_module: true,
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
compat!(full_es3, JscTarget::Es3);
|
|
|
|
compat!(full_es5, JscTarget::Es5);
|
|
|
|
compat!(full_es2015, JscTarget::Es2015);
|
|
|
|
compat!(full_es2016, JscTarget::Es2016);
|
|
|
|
compat!(full_es2017, JscTarget::Es2017);
|
|
|
|
compat!(full_es2018, JscTarget::Es2018);
|
|
|
|
compat!(full_es2019, JscTarget::Es2019);
|
|
|
|
compat!(full_es2020, JscTarget::Es2020);
|
2020-09-06 13:47:15 +03:00
|
|
|
|
|
|
|
macro_rules! tr_only {
|
|
|
|
($name:ident, $target:expr) => {
|
|
|
|
#[bench]
|
|
|
|
fn $name(b: &mut Bencher) {
|
|
|
|
let c = mk();
|
|
|
|
let module = parse(&c);
|
|
|
|
|
|
|
|
b.iter(|| {
|
2021-08-13 10:05:40 +03:00
|
|
|
let handler = Handler::with_emitter_writer(Box::new(stderr()), Some(c.cm.clone()));
|
2020-09-06 13:47:15 +03:00
|
|
|
let program = module.clone();
|
|
|
|
|
|
|
|
let mut config = c
|
|
|
|
.config_for_file(
|
2021-08-13 10:05:40 +03:00
|
|
|
&handler,
|
2020-09-06 13:47:15 +03:00
|
|
|
&Options {
|
2021-04-08 15:55:02 +03:00
|
|
|
config: Config {
|
2020-09-06 13:47:15 +03:00
|
|
|
jsc: JscConfig {
|
2021-04-08 15:55:02 +03:00
|
|
|
target: Some($target),
|
2020-09-06 13:47:15 +03:00
|
|
|
syntax: Some(Syntax::Typescript(TsConfig {
|
|
|
|
..Default::default()
|
|
|
|
})),
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
module: None,
|
|
|
|
..Default::default()
|
2021-04-08 15:55:02 +03:00
|
|
|
},
|
2020-09-06 13:47:15 +03:00
|
|
|
swcrc: false,
|
|
|
|
is_module: true,
|
|
|
|
..Default::default()
|
|
|
|
},
|
|
|
|
&FileName::Real(
|
|
|
|
"rxjs/src/internal/observable/dom/AjaxObservable.ts".into(),
|
|
|
|
),
|
|
|
|
)
|
2021-01-18 17:00:53 +03:00
|
|
|
.unwrap()
|
2020-09-06 13:47:15 +03:00
|
|
|
.unwrap();
|
2021-08-13 10:05:40 +03:00
|
|
|
let program =
|
|
|
|
c.run_transform(&handler, true, || program.fold_with(&mut config.pass));
|
2020-09-06 13:47:15 +03:00
|
|
|
black_box(program)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
tr_only!(transforms_es3, JscTarget::Es3);
|
|
|
|
tr_only!(transforms_es5, JscTarget::Es5);
|
|
|
|
tr_only!(transforms_es2015, JscTarget::Es2015);
|
|
|
|
tr_only!(transforms_es2016, JscTarget::Es2016);
|
|
|
|
tr_only!(transforms_es2017, JscTarget::Es2017);
|
|
|
|
tr_only!(transforms_es2018, JscTarget::Es2018);
|
|
|
|
tr_only!(transforms_es2019, JscTarget::Es2019);
|
|
|
|
tr_only!(transforms_es2020, JscTarget::Es2020);
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn parser(b: &mut Bencher) {
|
|
|
|
let c = mk();
|
|
|
|
|
|
|
|
//TODO: Use target
|
|
|
|
|
|
|
|
b.iter(|| {
|
|
|
|
black_box(parse(&c));
|
|
|
|
})
|
|
|
|
}
|