From df9ea2a54d5362225b18070cdfe2d7e78199126c Mon Sep 17 00:00:00 2001 From: Ryan Dahl Date: Wed, 5 Oct 2022 09:29:30 -0400 Subject: [PATCH] Revert "Add benchmarks for V8 native function calls (#1079)" This reverts commit 6444cb9d614ba82a1ecf8f35eaa574a694c49476. --- Cargo.toml | 5 -- benches/function.rs | 135 -------------------------------------------- 2 files changed, 140 deletions(-) delete mode 100644 benches/function.rs diff --git a/Cargo.toml b/Cargo.toml index f33809b5..c5b29122 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -106,10 +106,5 @@ name = "process" name = "build" path = "build.rs" -[[bench]] -name = "function" -path = "benches/function.rs" -harness = false - [workspace] members = ["examples/android"] diff --git a/benches/function.rs b/benches/function.rs deleted file mode 100644 index 33370003..00000000 --- a/benches/function.rs +++ /dev/null @@ -1,135 +0,0 @@ -use std::os::raw::c_void; - -fn main() { - v8::V8::set_flags_from_string( - "--turbo_fast_api_calls --allow_natives_syntax", - ); - let platform = v8::new_default_platform(0, false).make_shared(); - v8::V8::initialize_platform(platform); - v8::V8::initialize(); - let isolate = &mut v8::Isolate::new(v8::CreateParams::default()); - let handle_scope = &mut v8::HandleScope::new(isolate); - let context = v8::Context::new(handle_scope); - let scope = &mut v8::ContextScope::new(handle_scope, context); - let global = context.global(scope); - { - let func = v8::Function::new( - scope, - |scope: &mut v8::HandleScope, - _: v8::FunctionCallbackArguments, - mut rv: v8::ReturnValue| { - rv.set(v8::Integer::new(scope, 42).into()); - }, - ) - .unwrap(); - let name = v8::String::new(scope, "new_").unwrap(); - global.set(scope, name.into(), func.into()).unwrap(); - } - { - extern "C" fn callback(info: *const v8::FunctionCallbackInfo) { - let scope = unsafe { &mut v8::CallbackScope::new(&*info) }; - let mut rv = - unsafe { v8::ReturnValue::from_function_callback_info(info) }; - rv.set(v8::Integer::new(scope, 42).into()); - } - let func = v8::Function::new_raw(scope, callback).unwrap(); - let name = v8::String::new(scope, "new_raw").unwrap(); - global.set(scope, name.into(), func.into()).unwrap(); - } - { - extern "C" fn callback(info: *const v8::FunctionCallbackInfo) { - let mut rv = - unsafe { v8::ReturnValue::from_function_callback_info(info) }; - rv.set_uint32(42); - } - let func = v8::Function::new_raw(scope, callback).unwrap(); - let name = v8::String::new(scope, "new_raw_set_uint32").unwrap(); - global.set(scope, name.into(), func.into()).unwrap(); - } - { - let func = v8::Function::new( - scope, - |_: &mut v8::HandleScope, - _: v8::FunctionCallbackArguments, - mut rv: v8::ReturnValue| { - rv.set_uint32(42); - }, - ) - .unwrap(); - let name = v8::String::new(scope, "new_set_uint32").unwrap(); - global.set(scope, name.into(), func.into()).unwrap(); - } - { - fn fast_fn() -> i32 { - 42 - } - pub struct FastCall; - impl v8::fast_api::FastFunction for FastCall { - fn args(&self) -> &'static [v8::fast_api::Type] { - &[v8::fast_api::Type::V8Value] - } - fn return_type(&self) -> v8::fast_api::CType { - v8::fast_api::CType::Int32 - } - - fn function(&self) -> *const c_void { - fast_fn as _ - } - } - let template = v8::FunctionTemplate::builder( - |scope: &mut v8::HandleScope, - _: v8::FunctionCallbackArguments, - mut rv: v8::ReturnValue| { - rv.set(v8::Integer::new(scope, 42).into()); - }, - ) - .build_fast(scope, &FastCall, None); - let name = v8::String::new(scope, "new_fast").unwrap(); - let value = template.get_function(scope).unwrap(); - - global.set(scope, name.into(), value.into()).unwrap(); - } - - let runs = 100_000_000; - - for x in [ - "new_", - "new_raw", - "new_set_uint32", - "new_raw_set_uint32", - "new_fast", - ] { - let code = format!( - " - function bench() {{ return {}(); }}; - runs = {}; - start = Date.now(); - for (i = 0; i < runs; i++) bench(); - Date.now() - start; - ", - x, runs - ); - - let r = eval(scope, &code).unwrap(); - let number = r.to_number(scope).unwrap(); - let total_ms = number.number_value(scope).unwrap(); - let total_ns = 1e6 * total_ms; - let ns_per_run = total_ns / (runs as f64); - let mops_per_sec = (runs as f64) / (total_ms / 1000.0) / 1e6; - println!( - "{:.1} ns per run {:.1} million ops/sec → {}", - ns_per_run, mops_per_sec, x - ); - } -} - -fn eval<'s>( - scope: &mut v8::HandleScope<'s>, - code: &str, -) -> Option> { - let scope = &mut v8::EscapableHandleScope::new(scope); - let source = v8::String::new(scope, code).unwrap(); - let script = v8::Script::compile(scope, source, None).unwrap(); - let r = script.run(scope); - r.map(|v| scope.escape(v)) -}