|
|
|
@ -1,6 +1,6 @@
|
|
|
|
|
//! Benchmarks of the whole execution engine in Boa.
|
|
|
|
|
|
|
|
|
|
use boa::{exec, realm::Realm}; |
|
|
|
|
use boa::{exec::Interpreter, realm::Realm, Executable, Lexer, Parser}; |
|
|
|
|
use criterion::{black_box, criterion_group, criterion_main, Criterion}; |
|
|
|
|
|
|
|
|
|
#[cfg(all(target_arch = "x86_64", target_os = "linux", target_env = "gnu"))] |
|
|
|
@ -11,9 +11,9 @@ use criterion::{black_box, criterion_group, criterion_main, Criterion};
|
|
|
|
|
static ALLOC: jemallocator::Jemalloc = jemallocator::Jemalloc; |
|
|
|
|
|
|
|
|
|
static SYMBOL_CREATION: &str = r#" |
|
|
|
|
let a = Symbol(); |
|
|
|
|
let b = Symbol(); |
|
|
|
|
let c = Symbol(); |
|
|
|
|
(function () { |
|
|
|
|
return Symbol(); |
|
|
|
|
})(); |
|
|
|
|
"#; |
|
|
|
|
|
|
|
|
|
fn create_realm(c: &mut Criterion) { |
|
|
|
@ -21,32 +21,56 @@ fn create_realm(c: &mut Criterion) {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fn symbol_creation(c: &mut Criterion) { |
|
|
|
|
// Create new Realm and interpreter.
|
|
|
|
|
let realm = Realm::create(); |
|
|
|
|
let mut engine = Interpreter::new(realm); |
|
|
|
|
|
|
|
|
|
// Lex all the tokens.
|
|
|
|
|
let mut lexer = Lexer::new(black_box(SYMBOL_CREATION)); |
|
|
|
|
lexer.lex().expect("failed to lex"); |
|
|
|
|
|
|
|
|
|
// Parse the AST nodes.
|
|
|
|
|
let nodes = Parser::new(&black_box(lexer.tokens)).parse_all().unwrap(); |
|
|
|
|
|
|
|
|
|
// Execute the parsed nodes, passing them through a black box, to avoid over-optimizing by the compiler
|
|
|
|
|
c.bench_function("Symbols (Execution)", move |b| { |
|
|
|
|
b.iter(|| exec(black_box(SYMBOL_CREATION))) |
|
|
|
|
b.iter(|| black_box(&nodes).run(&mut engine).unwrap()) |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
static FOR_LOOP: &str = r#" |
|
|
|
|
let a = 10; |
|
|
|
|
(function () { |
|
|
|
|
let b = "hello"; |
|
|
|
|
for (;a > 100;) { |
|
|
|
|
a += 5; |
|
|
|
|
|
|
|
|
|
for (let a = 10; a < 100; a += 5) { |
|
|
|
|
if (a < 50) { |
|
|
|
|
b += "world"; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
b |
|
|
|
|
return b; |
|
|
|
|
})(); |
|
|
|
|
"#; |
|
|
|
|
|
|
|
|
|
fn for_loop_execution(c: &mut Criterion) { |
|
|
|
|
// Create new Realm and interpreter.
|
|
|
|
|
let realm = Realm::create(); |
|
|
|
|
let mut engine = Interpreter::new(realm); |
|
|
|
|
|
|
|
|
|
// Lex all the tokens.
|
|
|
|
|
let mut lexer = Lexer::new(black_box(FOR_LOOP)); |
|
|
|
|
lexer.lex().expect("failed to lex"); |
|
|
|
|
|
|
|
|
|
// Parse the AST nodes.
|
|
|
|
|
let nodes = Parser::new(&black_box(lexer.tokens)).parse_all().unwrap(); |
|
|
|
|
|
|
|
|
|
// Execute the parsed nodes, passing them through a black box, to avoid over-optimizing by the compiler
|
|
|
|
|
c.bench_function("For loop (Execution)", move |b| { |
|
|
|
|
b.iter(|| exec(black_box(FOR_LOOP))) |
|
|
|
|
b.iter(|| black_box(&nodes).run(&mut engine).unwrap()) |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
static FIBONACCI: &str = r#" |
|
|
|
|
(function () { |
|
|
|
|
let num = 12; |
|
|
|
|
|
|
|
|
|
function fib(n) { |
|
|
|
@ -54,14 +78,25 @@ function fib(n) {
|
|
|
|
|
return fib(n - 1) + fib(n - 2); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
let res = fib(num); |
|
|
|
|
|
|
|
|
|
res; |
|
|
|
|
return fib(num); |
|
|
|
|
})(); |
|
|
|
|
"#; |
|
|
|
|
|
|
|
|
|
fn fibonacci(c: &mut Criterion) { |
|
|
|
|
// Create new Realm and interpreter.
|
|
|
|
|
let realm = Realm::create(); |
|
|
|
|
let mut engine = Interpreter::new(realm); |
|
|
|
|
|
|
|
|
|
// Lex all the tokens.
|
|
|
|
|
let mut lexer = Lexer::new(black_box(FIBONACCI)); |
|
|
|
|
lexer.lex().expect("failed to lex"); |
|
|
|
|
|
|
|
|
|
// Parse the AST nodes.
|
|
|
|
|
let nodes = Parser::new(&black_box(lexer.tokens)).parse_all().unwrap(); |
|
|
|
|
|
|
|
|
|
// Execute the parsed nodes, passing them through a black box, to avoid over-optimizing by the compiler
|
|
|
|
|
c.bench_function("Fibonacci (Execution)", move |b| { |
|
|
|
|
b.iter(|| exec(black_box(FIBONACCI))) |
|
|
|
|
b.iter(|| black_box(&nodes).run(&mut engine).unwrap()) |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|