2018-12-01 16:24:26 +03:00
|
|
|
#![feature(test)]
|
2019-12-13 09:21:25 +03:00
|
|
|
extern crate test;
|
|
|
|
|
2020-07-23 20:18:22 +03:00
|
|
|
use swc_common::{FileName, DUMMY_SP};
|
2020-02-05 14:20:25 +03:00
|
|
|
use swc_ecma_ast::*;
|
2020-07-31 12:49:07 +03:00
|
|
|
use swc_ecma_parser::{lexer::Lexer, Parser, StringInput, Syntax};
|
2021-01-05 08:29:52 +03:00
|
|
|
use swc_ecma_transforms_base::pass::noop;
|
|
|
|
use swc_ecma_utils::ExprFactory;
|
2021-12-07 08:34:16 +03:00
|
|
|
use swc_ecma_visit::{FoldWith, Visit, VisitWith};
|
2018-12-01 16:24:26 +03:00
|
|
|
use test::Bencher;
|
|
|
|
|
2020-08-26 14:50:18 +03:00
|
|
|
static SOURCE: &str = include_str!("assets/AjaxObservable.ts");
|
2018-12-01 16:24:26 +03:00
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn module_clone(b: &mut Bencher) {
|
2019-12-28 05:25:54 +03:00
|
|
|
b.bytes = SOURCE.len() as _;
|
|
|
|
|
2019-01-07 13:43:47 +03:00
|
|
|
let _ = ::testing::run_test(false, |cm, handler| {
|
2019-02-10 14:49:29 +03:00
|
|
|
let fm = cm.new_source_file(FileName::Anon, SOURCE.into());
|
2019-12-14 12:51:08 +03:00
|
|
|
let lexer = Lexer::new(
|
2020-08-26 14:50:18 +03:00
|
|
|
Syntax::Typescript(Default::default()),
|
2019-12-14 12:51:08 +03:00
|
|
|
Default::default(),
|
2020-07-31 12:49:07 +03:00
|
|
|
StringInput::from(&*fm),
|
2019-02-08 07:00:58 +03:00
|
|
|
None,
|
2018-12-01 16:24:26 +03:00
|
|
|
);
|
2020-07-25 14:26:04 +03:00
|
|
|
let mut parser = Parser::new_from(lexer);
|
2019-01-07 13:43:47 +03:00
|
|
|
let module = parser
|
|
|
|
.parse_module()
|
2020-07-25 14:26:04 +03:00
|
|
|
.map_err(|e| {
|
|
|
|
e.into_diagnostic(handler).emit();
|
2019-01-07 13:43:47 +03:00
|
|
|
})
|
|
|
|
.unwrap();
|
2018-12-01 16:24:26 +03:00
|
|
|
|
2020-07-25 14:26:04 +03:00
|
|
|
for e in parser.take_errors() {
|
|
|
|
e.into_diagnostic(handler).emit();
|
|
|
|
}
|
|
|
|
|
2018-12-01 16:24:26 +03:00
|
|
|
b.iter(|| test::black_box(module.clone()));
|
|
|
|
Ok(())
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn fold_empty(b: &mut Bencher) {
|
2018-12-21 10:54:36 +03:00
|
|
|
b.bytes = SOURCE.len() as _;
|
|
|
|
|
2019-01-07 13:43:47 +03:00
|
|
|
let _ = ::testing::run_test(false, |cm, handler| {
|
2019-02-10 14:49:29 +03:00
|
|
|
let fm = cm.new_source_file(FileName::Anon, SOURCE.into());
|
2019-12-14 12:51:08 +03:00
|
|
|
let lexer = Lexer::new(
|
2020-08-26 14:50:18 +03:00
|
|
|
Syntax::Typescript(Default::default()),
|
2019-12-14 12:51:08 +03:00
|
|
|
Default::default(),
|
2020-07-31 12:49:07 +03:00
|
|
|
StringInput::from(&*fm),
|
2019-02-08 07:00:58 +03:00
|
|
|
None,
|
2018-12-01 16:24:26 +03:00
|
|
|
);
|
2020-07-25 14:26:04 +03:00
|
|
|
let mut parser = Parser::new_from(lexer);
|
2019-01-07 13:43:47 +03:00
|
|
|
let module = parser
|
|
|
|
.parse_module()
|
2020-07-25 14:26:04 +03:00
|
|
|
.map_err(|e| {
|
2022-01-13 17:06:11 +03:00
|
|
|
e.into_diagnostic(handler).emit();
|
2019-01-07 13:43:47 +03:00
|
|
|
})
|
|
|
|
.unwrap();
|
2020-07-25 14:26:04 +03:00
|
|
|
|
|
|
|
for e in parser.take_errors() {
|
2022-01-13 17:06:11 +03:00
|
|
|
e.into_diagnostic(handler).emit();
|
2020-07-25 14:26:04 +03:00
|
|
|
}
|
|
|
|
|
2020-07-23 20:18:22 +03:00
|
|
|
let mut folder = noop();
|
2018-12-01 16:24:26 +03:00
|
|
|
|
|
|
|
b.iter(|| test::black_box(module.clone().fold_with(&mut folder)));
|
|
|
|
Ok(())
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Optimized out
|
|
|
|
#[bench]
|
|
|
|
fn fold_noop_impl_all(b: &mut Bencher) {
|
2018-12-21 10:54:36 +03:00
|
|
|
b.bytes = SOURCE.len() as _;
|
|
|
|
|
2019-01-07 13:43:47 +03:00
|
|
|
let _ = ::testing::run_test(false, |cm, handler| {
|
2019-02-10 14:49:29 +03:00
|
|
|
let fm = cm.new_source_file(FileName::Anon, SOURCE.into());
|
2018-12-01 16:24:26 +03:00
|
|
|
|
2019-12-14 12:51:08 +03:00
|
|
|
let lexer = Lexer::new(
|
2020-08-26 14:50:18 +03:00
|
|
|
Syntax::Typescript(Default::default()),
|
2019-12-14 12:51:08 +03:00
|
|
|
Default::default(),
|
2020-07-31 12:49:07 +03:00
|
|
|
StringInput::from(&*fm),
|
2019-02-08 07:00:58 +03:00
|
|
|
None,
|
2018-12-01 16:24:26 +03:00
|
|
|
);
|
2020-07-25 14:26:04 +03:00
|
|
|
let mut parser = Parser::new_from(lexer);
|
2019-01-07 13:43:47 +03:00
|
|
|
let module = parser
|
|
|
|
.parse_module()
|
2022-01-13 17:06:11 +03:00
|
|
|
.map_err(|e| e.into_diagnostic(handler).emit())
|
2019-01-07 13:43:47 +03:00
|
|
|
.unwrap();
|
2020-07-25 14:26:04 +03:00
|
|
|
|
|
|
|
for e in parser.take_errors() {
|
2022-01-13 17:06:11 +03:00
|
|
|
e.into_diagnostic(handler).emit();
|
2020-07-25 14:26:04 +03:00
|
|
|
}
|
|
|
|
|
2020-07-23 20:18:22 +03:00
|
|
|
let mut folder = noop();
|
2018-12-01 16:24:26 +03:00
|
|
|
|
|
|
|
b.iter(|| test::black_box(module.clone().fold_with(&mut folder)));
|
|
|
|
Ok(())
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Optimized out
|
|
|
|
#[bench]
|
|
|
|
fn fold_noop_impl_vec(b: &mut Bencher) {
|
2018-12-21 10:54:36 +03:00
|
|
|
b.bytes = SOURCE.len() as _;
|
|
|
|
|
2019-01-07 13:43:47 +03:00
|
|
|
let _ = ::testing::run_test(false, |cm, handler| {
|
2019-02-10 14:49:29 +03:00
|
|
|
let fm = cm.new_source_file(FileName::Anon, SOURCE.into());
|
2019-12-14 12:51:08 +03:00
|
|
|
let lexer = Lexer::new(
|
2020-08-26 14:50:18 +03:00
|
|
|
Syntax::Typescript(Default::default()),
|
2019-12-14 12:51:08 +03:00
|
|
|
Default::default(),
|
2020-07-31 12:49:07 +03:00
|
|
|
StringInput::from(&*fm),
|
2019-02-08 07:00:58 +03:00
|
|
|
None,
|
2018-12-01 16:24:26 +03:00
|
|
|
);
|
2020-07-25 14:26:04 +03:00
|
|
|
let mut parser = Parser::new_from(lexer);
|
2019-01-07 13:43:47 +03:00
|
|
|
let module = parser
|
|
|
|
.parse_module()
|
2020-07-25 14:26:04 +03:00
|
|
|
.map_err(|e| {
|
2022-01-13 17:06:11 +03:00
|
|
|
e.into_diagnostic(handler).emit();
|
2019-01-07 13:43:47 +03:00
|
|
|
})
|
|
|
|
.unwrap();
|
2020-07-25 14:26:04 +03:00
|
|
|
|
|
|
|
for e in parser.take_errors() {
|
2022-01-13 17:06:11 +03:00
|
|
|
e.into_diagnostic(handler).emit();
|
2020-07-25 14:26:04 +03:00
|
|
|
}
|
|
|
|
|
2020-07-23 20:18:22 +03:00
|
|
|
let mut folder = noop();
|
2018-12-01 16:24:26 +03:00
|
|
|
|
|
|
|
b.iter(|| test::black_box(module.clone().fold_with(&mut folder)));
|
|
|
|
Ok(())
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
fn mk_expr() -> Expr {
|
|
|
|
Expr::Call(CallExpr {
|
|
|
|
span: DUMMY_SP,
|
|
|
|
callee: Ident::new("foo".into(), DUMMY_SP).as_callee(),
|
|
|
|
args: vec![],
|
2019-01-07 13:43:47 +03:00
|
|
|
type_args: None,
|
2018-12-01 16:24:26 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn boxing_boxed_clone(b: &mut Bencher) {
|
2019-01-07 13:43:47 +03:00
|
|
|
let _ = ::testing::run_test(false, |_, _| {
|
2020-07-23 20:18:22 +03:00
|
|
|
let expr = Box::new(mk_expr());
|
2018-12-01 16:24:26 +03:00
|
|
|
|
|
|
|
b.iter(|| test::black_box(expr.clone()));
|
|
|
|
Ok(())
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn boxing_unboxed_clone(b: &mut Bencher) {
|
2019-01-07 13:43:47 +03:00
|
|
|
let _ = ::testing::run_test(false, |_, _| {
|
2018-12-01 16:24:26 +03:00
|
|
|
let expr = mk_expr();
|
|
|
|
|
|
|
|
b.iter(|| test::black_box(expr.clone()));
|
|
|
|
Ok(())
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn boxing_boxed(b: &mut Bencher) {
|
2019-01-07 13:43:47 +03:00
|
|
|
let _ = ::testing::run_test(false, |_, _| {
|
2020-07-23 20:18:22 +03:00
|
|
|
let mut folder = noop();
|
|
|
|
let expr = Box::new(mk_expr());
|
2018-12-01 16:24:26 +03:00
|
|
|
|
|
|
|
b.iter(|| test::black_box(expr.clone().fold_with(&mut folder)));
|
|
|
|
Ok(())
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn boxing_unboxed(b: &mut Bencher) {
|
2019-01-07 13:43:47 +03:00
|
|
|
let _ = ::testing::run_test(false, |_, _| {
|
2020-07-23 20:18:22 +03:00
|
|
|
let mut folder = noop();
|
2018-12-01 16:24:26 +03:00
|
|
|
let expr = mk_expr();
|
|
|
|
|
|
|
|
b.iter(|| test::black_box(expr.clone().fold_with(&mut folder)));
|
|
|
|
Ok(())
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn visit_empty(b: &mut Bencher) {
|
2018-12-21 10:54:36 +03:00
|
|
|
b.bytes = SOURCE.len() as _;
|
|
|
|
|
2020-07-25 14:26:04 +03:00
|
|
|
let _ = ::testing::run_test(false, |cm, _| {
|
2019-02-10 14:49:29 +03:00
|
|
|
let fm = cm.new_source_file(FileName::Anon, SOURCE.into());
|
2019-12-14 12:51:08 +03:00
|
|
|
let lexer = Lexer::new(
|
2020-08-26 14:50:18 +03:00
|
|
|
Syntax::Typescript(Default::default()),
|
2019-12-14 12:51:08 +03:00
|
|
|
Default::default(),
|
2020-07-31 12:49:07 +03:00
|
|
|
StringInput::from(&*fm),
|
2019-02-08 07:00:58 +03:00
|
|
|
None,
|
2018-12-01 16:24:26 +03:00
|
|
|
);
|
2020-07-25 14:26:04 +03:00
|
|
|
let mut parser = Parser::new_from(lexer);
|
|
|
|
let _module = parser.parse_module().map_err(|_| ()).unwrap();
|
2018-12-01 16:24:26 +03:00
|
|
|
|
2019-11-17 07:21:53 +03:00
|
|
|
b.iter(|| test::black_box(()));
|
2018-12-01 16:24:26 +03:00
|
|
|
Ok(())
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn visit_contains_this(b: &mut Bencher) {
|
|
|
|
fn contains_this_expr(body: &Module) -> bool {
|
|
|
|
struct Visitor {
|
|
|
|
found: bool,
|
|
|
|
}
|
|
|
|
|
2020-07-23 20:18:22 +03:00
|
|
|
impl Visit for Visitor {
|
2018-12-01 16:24:26 +03:00
|
|
|
/// Don't recurse into fn
|
2021-12-07 08:34:16 +03:00
|
|
|
fn visit_fn_expr(&mut self, _: &FnExpr) {}
|
2018-12-01 16:24:26 +03:00
|
|
|
|
|
|
|
/// Don't recurse into fn
|
2021-12-07 08:34:16 +03:00
|
|
|
fn visit_fn_decl(&mut self, _: &FnDecl) {}
|
2020-07-23 20:18:22 +03:00
|
|
|
|
2021-12-07 08:34:16 +03:00
|
|
|
fn visit_this_expr(&mut self, _: &ThisExpr) {
|
2020-07-23 20:18:22 +03:00
|
|
|
self.found = true;
|
|
|
|
}
|
2018-12-01 16:24:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
let mut visitor = Visitor { found: false };
|
2021-12-07 08:34:16 +03:00
|
|
|
body.visit_with(&mut visitor);
|
2018-12-01 16:24:26 +03:00
|
|
|
visitor.found
|
|
|
|
}
|
2018-12-21 10:54:36 +03:00
|
|
|
|
|
|
|
b.bytes = SOURCE.len() as _;
|
|
|
|
|
2020-07-25 14:26:04 +03:00
|
|
|
let _ = ::testing::run_test(false, |cm, _| {
|
2019-02-10 14:49:29 +03:00
|
|
|
let fm = cm.new_source_file(FileName::Anon, SOURCE.into());
|
2019-12-14 12:51:08 +03:00
|
|
|
let lexer = Lexer::new(
|
2020-08-26 14:50:18 +03:00
|
|
|
Syntax::Typescript(Default::default()),
|
2019-12-14 12:51:08 +03:00
|
|
|
Default::default(),
|
2020-07-31 12:49:07 +03:00
|
|
|
StringInput::from(&*fm),
|
2019-02-08 07:00:58 +03:00
|
|
|
None,
|
2018-12-01 16:24:26 +03:00
|
|
|
);
|
2020-07-25 14:26:04 +03:00
|
|
|
let mut parser = Parser::new_from(lexer);
|
|
|
|
let module = parser.parse_module().map_err(|_| ()).unwrap();
|
2018-12-01 16:24:26 +03:00
|
|
|
|
|
|
|
b.iter(|| test::black_box(contains_this_expr(&module)));
|
|
|
|
Ok(())
|
|
|
|
});
|
|
|
|
}
|