From b333157c3715d29120eb1dfb1aaa3c0280c2510a Mon Sep 17 00:00:00 2001 From: Aaron O'Mullan Date: Fri, 21 May 2021 14:17:24 +0200 Subject: [PATCH 1/2] cleanup(bench_util): use Extensions for setup --- bench_util/benches/op_baseline.rs | 21 +++++++++++++-------- bench_util/src/js_runtime.rs | 21 +++++++++------------ extensions/url/benches/url_ops.rs | 24 +++++++++--------------- 3 files changed, 31 insertions(+), 35 deletions(-) diff --git a/bench_util/benches/op_baseline.rs b/bench_util/benches/op_baseline.rs index ecce90becc9a69..42be1ddada13d1 100644 --- a/bench_util/benches/op_baseline.rs +++ b/bench_util/benches/op_baseline.rs @@ -6,20 +6,25 @@ use deno_core::error::AnyError; use deno_core::op_async; use deno_core::op_sync; use deno_core::serialize_op_result; -use deno_core::JsRuntime; +use deno_core::Extension; use deno_core::Op; use deno_core::OpState; use std::cell::RefCell; use std::rc::Rc; -fn setup(runtime: &mut JsRuntime) { - runtime.register_op("pi_json", op_sync(|_, _: (), _: ()| Ok(314159))); - runtime.register_op("pi_async", op_async(op_pi_async)); - runtime.register_op("nop", |state, _| { - Op::Sync(serialize_op_result(Ok(9), state)) - }); - runtime.sync_ops_cache(); +fn setup() -> Vec { + vec![ + Extension::builder() + .ops(vec![ + ("pi_json", op_sync(|_, _: (), _: ()| Ok(314159))), + ("pi_async", op_async(op_pi_async)), + ("nop", Box::new(|state, _| { + Op::Sync(serialize_op_result(Ok(9), state)) + })), + ]) + .build() + ] } // this is a function since async closures aren't stable diff --git a/bench_util/src/js_runtime.rs b/bench_util/src/js_runtime.rs index a19eccd1f830a5..4556a8c7b226e7 100644 --- a/bench_util/src/js_runtime.rs +++ b/bench_util/src/js_runtime.rs @@ -2,19 +2,16 @@ use bencher::Bencher; use deno_core::v8; use deno_core::JsRuntime; +use deno_core::RuntimeOptions; +use deno_core::Extension; use crate::profiling::is_profiling; -pub fn create_js_runtime(setup: impl FnOnce(&mut JsRuntime)) -> JsRuntime { - let mut rt = JsRuntime::new(Default::default()); - - // Caller provided setup - setup(&mut rt); - - // Init ops - rt.sync_ops_cache(); - - rt +pub fn create_js_runtime(setup: impl FnOnce() -> Vec) -> JsRuntime { + JsRuntime::new(RuntimeOptions{ + extensions: setup(), + ..Default::default() + }) } fn loop_code(iters: u64, src: &str) -> String { @@ -24,7 +21,7 @@ fn loop_code(iters: u64, src: &str) -> String { pub fn bench_js_sync( b: &mut Bencher, src: &str, - setup: impl FnOnce(&mut JsRuntime), + setup: impl FnOnce() -> Vec, ) { let mut runtime = create_js_runtime(setup); let scope = &mut runtime.handle_scope(); @@ -50,7 +47,7 @@ pub fn bench_js_sync( pub fn bench_js_async( b: &mut Bencher, src: &str, - setup: impl FnOnce(&mut JsRuntime), + setup: impl FnOnce() -> Vec, ) { let mut runtime = create_js_runtime(setup); let tokio_runtime = tokio::runtime::Builder::new_current_thread() diff --git a/extensions/url/benches/url_ops.rs b/extensions/url/benches/url_ops.rs index b901939e0dd4a0..fbeff0eca4e4aa 100644 --- a/extensions/url/benches/url_ops.rs +++ b/extensions/url/benches/url_ops.rs @@ -2,22 +2,16 @@ use deno_bench_util::bench_js_sync; use deno_bench_util::bench_or_profile; use deno_bench_util::bencher::{benchmark_group, Bencher}; -use deno_core::JsRuntime; +use deno_core::Extension; -fn setup(runtime: &mut JsRuntime) { - // TODO(@AaronO): support caller provided extensions in deno_bench_util - let mut ext = deno_url::init(); - - for (name, op_fn) in ext.init_ops().unwrap() { - runtime.register_op(name, op_fn); - } - for (filename, src) in ext.init_js() { - runtime.execute(filename, src).unwrap(); - } - - runtime - .execute("setup", "const { URL } = globalThis.__bootstrap.url;") - .unwrap(); +fn setup() -> Vec { + vec![ + deno_url::init(), + Extension::builder().js(vec![ + ("setup", "const { URL } = globalThis.__bootstrap.url;") + ]) + .build(), + ] } fn bench_url_parse(b: &mut Bencher) { From 72067e237772b15c9757c9cdee7b9fa1ad3f627b Mon Sep 17 00:00:00 2001 From: Aaron O'Mullan Date: Fri, 21 May 2021 14:19:02 +0200 Subject: [PATCH 2/2] fmt --- bench_util/benches/op_baseline.rs | 13 ++++++------- bench_util/src/js_runtime.rs | 4 ++-- extensions/url/benches/url_ops.rs | 10 ++++++---- 3 files changed, 14 insertions(+), 13 deletions(-) diff --git a/bench_util/benches/op_baseline.rs b/bench_util/benches/op_baseline.rs index 42be1ddada13d1..0496a034bdf3e5 100644 --- a/bench_util/benches/op_baseline.rs +++ b/bench_util/benches/op_baseline.rs @@ -14,17 +14,16 @@ use std::cell::RefCell; use std::rc::Rc; fn setup() -> Vec { - vec![ - Extension::builder() + vec![Extension::builder() .ops(vec![ ("pi_json", op_sync(|_, _: (), _: ()| Ok(314159))), ("pi_async", op_async(op_pi_async)), - ("nop", Box::new(|state, _| { - Op::Sync(serialize_op_result(Ok(9), state)) - })), + ( + "nop", + Box::new(|state, _| Op::Sync(serialize_op_result(Ok(9), state))), + ), ]) - .build() - ] + .build()] } // this is a function since async closures aren't stable diff --git a/bench_util/src/js_runtime.rs b/bench_util/src/js_runtime.rs index 4556a8c7b226e7..7d003484eed0f7 100644 --- a/bench_util/src/js_runtime.rs +++ b/bench_util/src/js_runtime.rs @@ -1,14 +1,14 @@ // Copyright 2018-2021 the Deno authors. All rights reserved. MIT license. use bencher::Bencher; use deno_core::v8; +use deno_core::Extension; use deno_core::JsRuntime; use deno_core::RuntimeOptions; -use deno_core::Extension; use crate::profiling::is_profiling; pub fn create_js_runtime(setup: impl FnOnce() -> Vec) -> JsRuntime { - JsRuntime::new(RuntimeOptions{ + JsRuntime::new(RuntimeOptions { extensions: setup(), ..Default::default() }) diff --git a/extensions/url/benches/url_ops.rs b/extensions/url/benches/url_ops.rs index fbeff0eca4e4aa..252c1bfd625e4f 100644 --- a/extensions/url/benches/url_ops.rs +++ b/extensions/url/benches/url_ops.rs @@ -7,10 +7,12 @@ use deno_core::Extension; fn setup() -> Vec { vec![ deno_url::init(), - Extension::builder().js(vec![ - ("setup", "const { URL } = globalThis.__bootstrap.url;") - ]) - .build(), + Extension::builder() + .js(vec![( + "setup", + "const { URL } = globalThis.__bootstrap.url;", + )]) + .build(), ] }