From 8fe9b8a4cc381f9b94ce2caf10c61ddff864bdb4 Mon Sep 17 00:00:00 2001 From: Matt Mastracci Date: Sun, 25 Jun 2023 16:36:09 +0200 Subject: refactor(ops): ops2 supports result in fast path (#19603) Implements `Result` in fast-calls. Note that the approach here is slightly different. Rather than store the last result in the `OpState`, we put it into the `OpCtx` which saves us a lookup and lock in the error case. We do not have to lock this field as it's guaranteed only one runtime and thread can ever access it. The fastcall path for many ops can avoid doing a great deal of work, even for `Result` return values. In the previous iteration of `ops`, all `Result`-returning functions would fetch and lock the `OpState`, regardless of whether it was used or not. --- ops/op2/test_cases/sync/add.out | 7 ++-- ops/op2/test_cases/sync/doc_comment.out | 5 ++- ops/op2/test_cases/sync/result_primitive.out | 59 +++++++++++++++++++++++++--- ops/op2/test_cases/sync/result_primitive.rs | 2 +- ops/op2/test_cases/sync/result_void.out | 59 +++++++++++++++++++++++++--- ops/op2/test_cases/sync/result_void.rs | 2 +- ops/op2/test_cases/sync/smi.out | 7 ++-- 7 files changed, 119 insertions(+), 22 deletions(-) (limited to 'ops/op2/test_cases/sync') diff --git a/ops/op2/test_cases/sync/add.out b/ops/op2/test_cases/sync/add.out index a7269c5cf..7d97a7161 100644 --- a/ops/op2/test_cases/sync/add.out +++ b/ops/op2/test_cases/sync/add.out @@ -13,7 +13,7 @@ impl op_add { use deno_core::v8::fast_api::Type; use deno_core::v8::fast_api::CType; deno_core::v8::fast_api::FastFunction::new( - &[Type::Uint32, Type::Uint32], + &[Type::V8Value, Type::Uint32, Type::Uint32], CType::Uint32, Self::fast_function as *const ::std::ffi::c_void, ) @@ -43,9 +43,8 @@ impl op_add { arg0: u32, arg1: u32, ) -> u32 { - let arg0 = arg0 as _; - let arg1 = arg1 as _; - Self::call(arg0, arg1) + let result = Self::call(arg0 as _, arg1 as _); + result } #[inline(always)] fn call(a: u32, b: u32) -> u32 { diff --git a/ops/op2/test_cases/sync/doc_comment.out b/ops/op2/test_cases/sync/doc_comment.out index bd0d0b21f..e9f063102 100644 --- a/ops/op2/test_cases/sync/doc_comment.out +++ b/ops/op2/test_cases/sync/doc_comment.out @@ -13,7 +13,7 @@ impl op_has_doc_comment { use deno_core::v8::fast_api::Type; use deno_core::v8::fast_api::CType; deno_core::v8::fast_api::FastFunction::new( - &[], + &[Type::V8Value], CType::Void, Self::fast_function as *const ::std::ffi::c_void, ) @@ -28,7 +28,8 @@ impl op_has_doc_comment { let result = Self::call(); } fn fast_function(_: deno_core::v8::Local) -> () { - Self::call() + let result = Self::call(); + result } #[inline(always)] pub fn call() -> () {} diff --git a/ops/op2/test_cases/sync/result_primitive.out b/ops/op2/test_cases/sync/result_primitive.out index a8ac50174..151e8e730 100644 --- a/ops/op2/test_cases/sync/result_primitive.out +++ b/ops/op2/test_cases/sync/result_primitive.out @@ -9,7 +9,15 @@ impl op_u32_with_result { name: stringify!(op_u32_with_result), v8_fn_ptr: Self::slow_function as _, enabled: true, - fast_fn: None, + fast_fn: Some({ + use deno_core::v8::fast_api::Type; + use deno_core::v8::fast_api::CType; + deno_core::v8::fast_api::FastFunction::new( + &[Type::V8Value, Type::CallbackOptions], + CType::Uint32, + Self::fast_function as *const ::std::ffi::c_void, + ) + }), is_async: false, is_unstable: false, is_v8: false, @@ -20,6 +28,27 @@ impl op_u32_with_result { let mut rv = deno_core::v8::ReturnValue::from_function_callback_info(unsafe { &*info }); + let args = deno_core::v8::FunctionCallbackArguments::from_function_callback_info(unsafe { + &*info + }); + let opctx = unsafe { + &*(deno_core::v8::Local::::cast(args.data()).value() + as *const deno_core::_ops::OpCtx) + }; + if let Some(err) = unsafe { opctx.unsafely_take_last_error_for_ops_only() } { + let scope = &mut unsafe { deno_core::v8::CallbackScope::new(&*info) }; + let args = deno_core::v8::FunctionCallbackArguments::from_function_callback_info(unsafe { + &*info + }); + let opstate = ::std::cell::RefCell::borrow(&*opctx.state); + let exception = deno_core::error::to_v8_error( + scope, + opstate.get_error_class_fn, + &err, + ); + scope.throw_exception(exception); + return; + } let result = Self::call(); match result { Ok(result) => { @@ -30,10 +59,6 @@ impl op_u32_with_result { let args = deno_core::v8::FunctionCallbackArguments::from_function_callback_info(unsafe { &*info }); - let opctx = unsafe { - &*(deno_core::v8::Local::::cast(args.data()) - .value() as *const deno_core::_ops::OpCtx) - }; let opstate = ::std::cell::RefCell::borrow(&*opctx.state); let exception = deno_core::error::to_v8_error( scope, @@ -45,6 +70,30 @@ impl op_u32_with_result { } }; } + fn fast_function( + _: deno_core::v8::Local, + fast_api_callback_options: *mut deno_core::v8::fast_api::FastApiCallbackOptions, + ) -> u32 { + let fast_api_callback_options = unsafe { &mut *fast_api_callback_options }; + let opctx = unsafe { + &*(deno_core::v8::Local::< + v8::External, + >::cast(unsafe { fast_api_callback_options.data.data }) + .value() as *const deno_core::_ops::OpCtx) + }; + let result = Self::call(); + let result = match result { + Ok(result) => result, + Err(err) => { + unsafe { + opctx.unsafely_set_last_error_for_ops_only(err); + } + fast_api_callback_options.fallback = true; + return ::std::default::Default::default(); + } + }; + result + } #[inline(always)] pub fn call() -> Result {} } diff --git a/ops/op2/test_cases/sync/result_primitive.rs b/ops/op2/test_cases/sync/result_primitive.rs index 6f68fa228..df89c2432 100644 --- a/ops/op2/test_cases/sync/result_primitive.rs +++ b/ops/op2/test_cases/sync/result_primitive.rs @@ -1,4 +1,4 @@ // Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. -#[op2] +#[op2(fast)] pub fn op_u32_with_result() -> Result {} diff --git a/ops/op2/test_cases/sync/result_void.out b/ops/op2/test_cases/sync/result_void.out index 74c0c66a6..afc10582b 100644 --- a/ops/op2/test_cases/sync/result_void.out +++ b/ops/op2/test_cases/sync/result_void.out @@ -9,7 +9,15 @@ impl op_void_with_result { name: stringify!(op_void_with_result), v8_fn_ptr: Self::slow_function as _, enabled: true, - fast_fn: None, + fast_fn: Some({ + use deno_core::v8::fast_api::Type; + use deno_core::v8::fast_api::CType; + deno_core::v8::fast_api::FastFunction::new( + &[Type::V8Value, Type::CallbackOptions], + CType::Void, + Self::fast_function as *const ::std::ffi::c_void, + ) + }), is_async: false, is_unstable: false, is_v8: false, @@ -17,6 +25,27 @@ impl op_void_with_result { } } pub extern "C" fn slow_function(info: *const deno_core::v8::FunctionCallbackInfo) { + let args = deno_core::v8::FunctionCallbackArguments::from_function_callback_info(unsafe { + &*info + }); + let opctx = unsafe { + &*(deno_core::v8::Local::::cast(args.data()).value() + as *const deno_core::_ops::OpCtx) + }; + if let Some(err) = unsafe { opctx.unsafely_take_last_error_for_ops_only() } { + let scope = &mut unsafe { deno_core::v8::CallbackScope::new(&*info) }; + let args = deno_core::v8::FunctionCallbackArguments::from_function_callback_info(unsafe { + &*info + }); + let opstate = ::std::cell::RefCell::borrow(&*opctx.state); + let exception = deno_core::error::to_v8_error( + scope, + opstate.get_error_class_fn, + &err, + ); + scope.throw_exception(exception); + return; + } let result = Self::call(); match result { Ok(result) => {} @@ -25,10 +54,6 @@ impl op_void_with_result { let args = deno_core::v8::FunctionCallbackArguments::from_function_callback_info(unsafe { &*info }); - let opctx = unsafe { - &*(deno_core::v8::Local::::cast(args.data()) - .value() as *const deno_core::_ops::OpCtx) - }; let opstate = ::std::cell::RefCell::borrow(&*opctx.state); let exception = deno_core::error::to_v8_error( scope, @@ -40,6 +65,30 @@ impl op_void_with_result { } }; } + fn fast_function( + _: deno_core::v8::Local, + fast_api_callback_options: *mut deno_core::v8::fast_api::FastApiCallbackOptions, + ) -> () { + let fast_api_callback_options = unsafe { &mut *fast_api_callback_options }; + let opctx = unsafe { + &*(deno_core::v8::Local::< + v8::External, + >::cast(unsafe { fast_api_callback_options.data.data }) + .value() as *const deno_core::_ops::OpCtx) + }; + let result = Self::call(); + let result = match result { + Ok(result) => result, + Err(err) => { + unsafe { + opctx.unsafely_set_last_error_for_ops_only(err); + } + fast_api_callback_options.fallback = true; + return ::std::default::Default::default(); + } + }; + result + } #[inline(always)] pub fn call() -> Result<(), AnyError> {} } diff --git a/ops/op2/test_cases/sync/result_void.rs b/ops/op2/test_cases/sync/result_void.rs index 41256e8c4..ef3aa7b32 100644 --- a/ops/op2/test_cases/sync/result_void.rs +++ b/ops/op2/test_cases/sync/result_void.rs @@ -1,4 +1,4 @@ // Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. -#[op2] +#[op2(fast)] pub fn op_void_with_result() -> Result<(), AnyError> {} diff --git a/ops/op2/test_cases/sync/smi.out b/ops/op2/test_cases/sync/smi.out index e6c1bc1e3..7210e0572 100644 --- a/ops/op2/test_cases/sync/smi.out +++ b/ops/op2/test_cases/sync/smi.out @@ -13,7 +13,7 @@ impl op_add { use deno_core::v8::fast_api::Type; use deno_core::v8::fast_api::CType; deno_core::v8::fast_api::FastFunction::new( - &[Type::Int32, Type::Uint32], + &[Type::V8Value, Type::Int32, Type::Uint32], CType::Uint32, Self::fast_function as *const ::std::ffi::c_void, ) @@ -43,9 +43,8 @@ impl op_add { arg0: i32, arg1: u32, ) -> u32 { - let arg0 = arg0 as _; - let arg1 = arg1 as _; - Self::call(arg0, arg1) + let result = Self::call(arg0 as _, arg1 as _); + result } #[inline(always)] fn call(id: ResourceId, extra: u16) -> u32 {} -- cgit v1.2.3