2021-09-29 15:10:38 +03:00
|
|
|
extern crate swc_node_base;
|
2020-08-18 19:29:49 +03:00
|
|
|
|
2022-04-03 18:56:29 +03:00
|
|
|
use criterion::{black_box, criterion_group, criterion_main, Bencher, Criterion};
|
2020-08-18 19:29:49 +03:00
|
|
|
use swc_common::{FileName, Span, SyntaxContext, DUMMY_SP};
|
|
|
|
use swc_ecma_ast::Module;
|
|
|
|
use swc_ecma_parser::{Parser, StringInput, Syntax};
|
|
|
|
use swc_ecma_visit::{Fold, FoldWith, VisitMut, VisitMutWith};
|
|
|
|
|
2021-11-10 13:00:54 +03:00
|
|
|
static SOURCE: &str = include_str!("files/angular-1.2.5.js");
|
2020-08-18 19:29:49 +03:00
|
|
|
|
|
|
|
fn run<F>(b: &mut Bencher, mut op: F)
|
|
|
|
where
|
|
|
|
F: FnMut(Module) -> Module,
|
|
|
|
{
|
|
|
|
let _ = ::testing::run_test(false, |cm, _| {
|
|
|
|
let fm = cm.new_source_file(FileName::Anon, SOURCE.into());
|
|
|
|
|
|
|
|
let mut parser = Parser::new(Syntax::default(), StringInput::from(&*fm), None);
|
|
|
|
let module = parser.parse_module().map_err(|_| ()).unwrap();
|
|
|
|
b.iter(|| {
|
|
|
|
let module = module.clone();
|
|
|
|
let module = op(module);
|
2022-04-03 18:56:29 +03:00
|
|
|
black_box(module)
|
2020-08-18 19:29:49 +03:00
|
|
|
});
|
|
|
|
Ok(())
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-04-03 18:56:29 +03:00
|
|
|
fn bench_cases(c: &mut Criterion) {
|
2022-04-17 21:36:15 +03:00
|
|
|
c.bench_function("es/visitor/compare/clone", |b| run(b, |m| m));
|
2020-08-18 19:29:49 +03:00
|
|
|
|
2022-04-17 21:36:15 +03:00
|
|
|
c.bench_function("es/visitor/compare/visit_mut_span", |b| {
|
2022-04-03 18:56:29 +03:00
|
|
|
struct RespanVisitMut;
|
2020-08-18 19:29:49 +03:00
|
|
|
|
2022-04-03 18:56:29 +03:00
|
|
|
impl VisitMut for RespanVisitMut {
|
|
|
|
fn visit_mut_span(&mut self, span: &mut Span) {
|
|
|
|
*span = DUMMY_SP;
|
|
|
|
}
|
2020-08-18 19:29:49 +03:00
|
|
|
}
|
|
|
|
|
2022-04-03 18:56:29 +03:00
|
|
|
run(b, |mut m| {
|
|
|
|
m.visit_mut_with(&mut RespanVisitMut);
|
2020-08-18 19:29:49 +03:00
|
|
|
|
2022-04-03 18:56:29 +03:00
|
|
|
m
|
|
|
|
});
|
2020-08-18 19:29:49 +03:00
|
|
|
});
|
|
|
|
|
2022-04-17 21:36:15 +03:00
|
|
|
c.bench_function("es/visitor/compare/visit_mut_span_panic", |b| {
|
2022-04-03 18:56:29 +03:00
|
|
|
struct RespanVisitMut;
|
2020-08-18 19:29:49 +03:00
|
|
|
|
2022-04-03 18:56:29 +03:00
|
|
|
impl VisitMut for RespanVisitMut {
|
|
|
|
fn visit_mut_span(&mut self, span: &mut Span) {
|
|
|
|
if span.ctxt != SyntaxContext::empty() {
|
|
|
|
panic!()
|
|
|
|
}
|
2020-08-18 19:29:49 +03:00
|
|
|
|
2022-04-03 18:56:29 +03:00
|
|
|
*span = DUMMY_SP;
|
|
|
|
}
|
2020-08-18 19:29:49 +03:00
|
|
|
}
|
|
|
|
|
2022-04-03 18:56:29 +03:00
|
|
|
run(b, |mut m| {
|
|
|
|
m.visit_mut_with(&mut RespanVisitMut);
|
2020-08-18 19:29:49 +03:00
|
|
|
|
2022-04-03 18:56:29 +03:00
|
|
|
m
|
|
|
|
});
|
2020-08-18 19:29:49 +03:00
|
|
|
});
|
|
|
|
|
2022-04-17 21:36:15 +03:00
|
|
|
c.bench_function("es/visitor/compare/fold_span", |b| {
|
2022-04-03 18:56:29 +03:00
|
|
|
struct RespanFold;
|
2020-08-18 19:29:49 +03:00
|
|
|
|
2022-04-03 18:56:29 +03:00
|
|
|
impl Fold for RespanFold {
|
|
|
|
fn fold_span(&mut self, _: Span) -> Span {
|
|
|
|
DUMMY_SP
|
|
|
|
}
|
2020-08-18 19:29:49 +03:00
|
|
|
}
|
|
|
|
|
2022-04-03 18:56:29 +03:00
|
|
|
run(b, |m| m.fold_with(&mut RespanFold));
|
|
|
|
});
|
2020-08-18 19:29:49 +03:00
|
|
|
|
2022-04-17 21:36:15 +03:00
|
|
|
c.bench_function("es/visitor/compare/fold_span_panic", |b| {
|
2022-04-03 18:56:29 +03:00
|
|
|
struct RespanFold;
|
2020-08-18 19:29:49 +03:00
|
|
|
|
2022-04-03 18:56:29 +03:00
|
|
|
impl Fold for RespanFold {
|
|
|
|
fn fold_span(&mut self, s: Span) -> Span {
|
|
|
|
if s.ctxt != SyntaxContext::empty() {
|
|
|
|
panic!()
|
|
|
|
}
|
|
|
|
DUMMY_SP
|
2020-08-18 19:29:49 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-03 18:56:29 +03:00
|
|
|
run(b, |m| m.fold_with(&mut RespanFold));
|
|
|
|
});
|
2020-08-18 19:29:49 +03:00
|
|
|
}
|
2022-04-03 18:56:29 +03:00
|
|
|
|
|
|
|
criterion_group!(benches, bench_cases);
|
|
|
|
criterion_main!(benches);
|