2018-12-01 16:24:26 +03:00
|
|
|
#![feature(test)]
|
|
|
|
#![feature(specialization)]
|
|
|
|
#![feature(box_syntax)]
|
|
|
|
|
2019-12-13 09:21:25 +03:00
|
|
|
extern crate test;
|
|
|
|
|
|
|
|
use ast::*;
|
2018-12-01 16:24:26 +03:00
|
|
|
use swc_common::{FileName, Fold, FoldWith, Visit, VisitWith, DUMMY_SP};
|
2019-12-14 12:51:08 +03:00
|
|
|
use swc_ecma_parser::{lexer::Lexer, Parser, Session, SourceFileInput, Syntax};
|
2018-12-01 16:24:26 +03:00
|
|
|
use swc_ecma_transforms::util::ExprFactory;
|
|
|
|
use test::Bencher;
|
|
|
|
|
2019-11-17 07:21:53 +03:00
|
|
|
static SOURCE: &str = r#"
|
2018-12-01 16:24:26 +03:00
|
|
|
'use strict';
|
|
|
|
/**
|
|
|
|
* Extract red color out of a color integer:
|
|
|
|
*
|
|
|
|
* 0x00DEAD -> 0x00
|
|
|
|
*
|
|
|
|
* @param {Number} color
|
|
|
|
* @return {Number}
|
|
|
|
*/
|
|
|
|
function red( color )
|
|
|
|
{
|
|
|
|
let foo = 3.14;
|
|
|
|
return color >> 16;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Extract green out of a color integer:
|
|
|
|
*
|
|
|
|
* 0x00DEAD -> 0xDE
|
|
|
|
*
|
|
|
|
* @param {Number} color
|
|
|
|
* @return {Number}
|
|
|
|
*/
|
|
|
|
function green( color )
|
|
|
|
{
|
|
|
|
return ( color >> 8 ) & 0xFF;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Extract blue color out of a color integer:
|
|
|
|
*
|
|
|
|
* 0x00DEAD -> 0xAD
|
|
|
|
*
|
|
|
|
* @param {Number} color
|
|
|
|
* @return {Number}
|
|
|
|
*/
|
|
|
|
function blue( color )
|
|
|
|
{
|
|
|
|
return color & 0xFF;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Converts an integer containing a color such as 0x00DEAD to a hex
|
|
|
|
* string, such as '#00DEAD';
|
|
|
|
*
|
|
|
|
* @param {Number} int
|
|
|
|
* @return {String}
|
|
|
|
*/
|
|
|
|
function intToHex( int )
|
|
|
|
{
|
|
|
|
const mask = '#000000';
|
|
|
|
const hex = int.toString( 16 );
|
|
|
|
return mask.substring( 0, 7 - hex.length ) + hex;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Converts a hex string containing a color such as '#00DEAD' to
|
|
|
|
* an integer, such as 0x00DEAD;
|
|
|
|
*
|
|
|
|
* @param {Number} num
|
|
|
|
* @return {String}
|
|
|
|
*/
|
|
|
|
function hexToInt( hex )
|
|
|
|
{
|
|
|
|
return parseInt( hex.substring( 1 ), 16 );
|
|
|
|
}
|
|
|
|
module.exports = {
|
|
|
|
red,
|
|
|
|
green,
|
|
|
|
blue,
|
|
|
|
intToHex,
|
|
|
|
hexToInt,
|
|
|
|
};"#;
|
|
|
|
|
|
|
|
#[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(
|
2019-01-07 13:43:47 +03:00
|
|
|
Session { handler: &handler },
|
2019-01-08 10:34:35 +03:00
|
|
|
Syntax::default(),
|
2019-12-14 12:51:08 +03:00
|
|
|
Default::default(),
|
2018-12-01 16:24:26 +03:00
|
|
|
SourceFileInput::from(&*fm),
|
2019-02-08 07:00:58 +03:00
|
|
|
None,
|
2018-12-01 16:24:26 +03:00
|
|
|
);
|
2019-12-14 12:51:08 +03:00
|
|
|
let mut parser = Parser::new_from(Session { handler: &handler }, lexer);
|
2019-01-07 13:43:47 +03:00
|
|
|
let module = parser
|
|
|
|
.parse_module()
|
2019-01-17 17:17:16 +03:00
|
|
|
.map_err(|mut e| {
|
2019-01-07 13:43:47 +03:00
|
|
|
e.emit();
|
|
|
|
})
|
|
|
|
.unwrap();
|
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 _;
|
|
|
|
|
2018-12-01 16:24:26 +03:00
|
|
|
struct Noop;
|
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(
|
2019-01-07 13:43:47 +03:00
|
|
|
Session { handler: &handler },
|
2019-01-08 10:34:35 +03:00
|
|
|
Syntax::default(),
|
2019-12-14 12:51:08 +03:00
|
|
|
Default::default(),
|
2018-12-01 16:24:26 +03:00
|
|
|
SourceFileInput::from(&*fm),
|
2019-02-08 07:00:58 +03:00
|
|
|
None,
|
2018-12-01 16:24:26 +03:00
|
|
|
);
|
2019-12-14 12:51:08 +03:00
|
|
|
let mut parser = Parser::new_from(Session { handler: &handler }, lexer);
|
2019-01-07 13:43:47 +03:00
|
|
|
let module = parser
|
|
|
|
.parse_module()
|
2019-01-17 17:17:16 +03:00
|
|
|
.map_err(|mut e| {
|
2019-01-07 13:43:47 +03:00
|
|
|
e.emit();
|
|
|
|
})
|
|
|
|
.unwrap();
|
2018-12-01 16:24:26 +03:00
|
|
|
let mut folder = Noop;
|
|
|
|
|
|
|
|
b.iter(|| test::black_box(module.clone().fold_with(&mut folder)));
|
|
|
|
Ok(())
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Optimized out
|
|
|
|
#[bench]
|
|
|
|
fn fold_noop_impl_all(b: &mut Bencher) {
|
|
|
|
struct Noop;
|
|
|
|
impl<T> Fold<T> for Noop
|
|
|
|
where
|
|
|
|
T: FoldWith<Self>,
|
|
|
|
{
|
|
|
|
fn fold(&mut self, node: T) -> T {
|
|
|
|
node
|
|
|
|
}
|
|
|
|
}
|
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(
|
2019-01-07 13:43:47 +03:00
|
|
|
Session { handler: &handler },
|
2019-01-08 10:34:35 +03:00
|
|
|
Syntax::default(),
|
2019-12-14 12:51:08 +03:00
|
|
|
Default::default(),
|
2018-12-01 16:24:26 +03:00
|
|
|
SourceFileInput::from(&*fm),
|
2019-02-08 07:00:58 +03:00
|
|
|
None,
|
2018-12-01 16:24:26 +03:00
|
|
|
);
|
2019-12-14 12:51:08 +03:00
|
|
|
let mut parser = Parser::new_from(Session { handler: &handler }, lexer);
|
2019-01-07 13:43:47 +03:00
|
|
|
let module = parser
|
|
|
|
.parse_module()
|
2019-01-17 17:17:16 +03:00
|
|
|
.map_err(|mut e| {
|
2019-01-07 13:43:47 +03:00
|
|
|
e.emit();
|
|
|
|
})
|
|
|
|
.unwrap();
|
2018-12-01 16:24:26 +03:00
|
|
|
let mut folder = Noop;
|
|
|
|
|
|
|
|
b.iter(|| test::black_box(module.clone().fold_with(&mut folder)));
|
|
|
|
Ok(())
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Optimized out
|
|
|
|
#[bench]
|
|
|
|
fn fold_noop_impl_vec(b: &mut Bencher) {
|
|
|
|
struct Noop;
|
|
|
|
impl<T> Fold<Vec<T>> for Noop
|
|
|
|
where
|
|
|
|
Vec<T>: FoldWith<Self>,
|
|
|
|
{
|
|
|
|
fn fold(&mut self, node: Vec<T>) -> Vec<T> {
|
|
|
|
node
|
|
|
|
}
|
|
|
|
}
|
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(
|
2019-01-07 13:43:47 +03:00
|
|
|
Session { handler: &handler },
|
2019-01-08 10:34:35 +03:00
|
|
|
Syntax::default(),
|
2019-12-14 12:51:08 +03:00
|
|
|
Default::default(),
|
2018-12-01 16:24:26 +03:00
|
|
|
SourceFileInput::from(&*fm),
|
2019-02-08 07:00:58 +03:00
|
|
|
None,
|
2018-12-01 16:24:26 +03:00
|
|
|
);
|
2019-12-14 12:51:08 +03:00
|
|
|
let mut parser = Parser::new_from(Session { handler: &handler }, lexer);
|
2019-01-07 13:43:47 +03:00
|
|
|
let module = parser
|
|
|
|
.parse_module()
|
2019-01-17 17:17:16 +03:00
|
|
|
.map_err(|mut e| {
|
2019-01-07 13:43:47 +03:00
|
|
|
e.emit();
|
|
|
|
})
|
|
|
|
.unwrap();
|
2018-12-01 16:24:26 +03:00
|
|
|
let mut folder = Noop;
|
|
|
|
|
|
|
|
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, |_, _| {
|
2018-12-01 16:24:26 +03:00
|
|
|
let expr = box mk_expr();
|
|
|
|
|
|
|
|
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) {
|
|
|
|
struct Noop;
|
|
|
|
|
2019-01-07 13:43:47 +03:00
|
|
|
let _ = ::testing::run_test(false, |_, _| {
|
2018-12-01 16:24:26 +03:00
|
|
|
let mut folder = Noop;
|
|
|
|
let expr = box mk_expr();
|
|
|
|
|
|
|
|
b.iter(|| test::black_box(expr.clone().fold_with(&mut folder)));
|
|
|
|
Ok(())
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
#[bench]
|
|
|
|
fn boxing_unboxed(b: &mut Bencher) {
|
|
|
|
struct Noop;
|
|
|
|
|
2019-01-07 13:43:47 +03:00
|
|
|
let _ = ::testing::run_test(false, |_, _| {
|
2018-12-01 16:24:26 +03:00
|
|
|
let mut folder = Noop;
|
|
|
|
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 _;
|
|
|
|
|
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(
|
2019-01-07 13:43:47 +03:00
|
|
|
Session { handler: &handler },
|
2019-01-08 10:34:35 +03:00
|
|
|
Syntax::default(),
|
2019-12-14 12:51:08 +03:00
|
|
|
Default::default(),
|
2018-12-01 16:24:26 +03:00
|
|
|
SourceFileInput::from(&*fm),
|
2019-02-08 07:00:58 +03:00
|
|
|
None,
|
2018-12-01 16:24:26 +03:00
|
|
|
);
|
2019-12-14 12:51:08 +03:00
|
|
|
let mut parser = Parser::new_from(Session { handler: &handler }, lexer);
|
2019-11-17 07:21:53 +03:00
|
|
|
let _module = parser
|
2019-01-07 13:43:47 +03:00
|
|
|
.parse_module()
|
2019-01-17 17:17:16 +03:00
|
|
|
.map_err(|mut e| {
|
2019-01-07 13:43:47 +03:00
|
|
|
e.emit();
|
|
|
|
})
|
|
|
|
.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,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Visit<ThisExpr> for Visitor {
|
|
|
|
fn visit(&mut self, _: &ThisExpr) {
|
|
|
|
self.found = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Visit<FnExpr> for Visitor {
|
|
|
|
/// Don't recurse into fn
|
|
|
|
fn visit(&mut self, _: &FnExpr) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Visit<FnDecl> for Visitor {
|
|
|
|
/// Don't recurse into fn
|
|
|
|
fn visit(&mut self, _: &FnDecl) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut visitor = Visitor { found: false };
|
|
|
|
body.visit_with(&mut visitor);
|
|
|
|
visitor.found
|
|
|
|
}
|
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(
|
2019-01-07 13:43:47 +03:00
|
|
|
Session { handler: &handler },
|
2019-01-08 10:34:35 +03:00
|
|
|
Syntax::default(),
|
2019-12-14 12:51:08 +03:00
|
|
|
Default::default(),
|
2018-12-01 16:24:26 +03:00
|
|
|
SourceFileInput::from(&*fm),
|
2019-02-08 07:00:58 +03:00
|
|
|
None,
|
2018-12-01 16:24:26 +03:00
|
|
|
);
|
2019-12-14 12:51:08 +03:00
|
|
|
let mut parser = Parser::new_from(Session { handler: &handler }, lexer);
|
2019-01-07 13:43:47 +03:00
|
|
|
let module = parser
|
|
|
|
.parse_module()
|
2019-01-17 17:17:16 +03:00
|
|
|
.map_err(|mut e| {
|
2019-01-07 13:43:47 +03:00
|
|
|
e.emit();
|
|
|
|
})
|
|
|
|
.unwrap();
|
2018-12-01 16:24:26 +03:00
|
|
|
|
|
|
|
b.iter(|| test::black_box(contains_this_expr(&module)));
|
|
|
|
Ok(())
|
|
|
|
});
|
|
|
|
}
|