summaryrefslogtreecommitdiff
path: root/ops/op2/test_cases/sync
diff options
context:
space:
mode:
authorMatt Mastracci <matthew@mastracci.com>2023-06-25 16:36:09 +0200
committerGitHub <noreply@github.com>2023-06-25 16:36:09 +0200
commit8fe9b8a4cc381f9b94ce2caf10c61ddff864bdb4 (patch)
tree9ae0797ef865f0f0a023052f7dbe433d238561d4 /ops/op2/test_cases/sync
parent3fe44a50c37b3f045c95a2085b58cfe3f71e8f6a (diff)
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.
Diffstat (limited to 'ops/op2/test_cases/sync')
-rw-r--r--ops/op2/test_cases/sync/add.out7
-rw-r--r--ops/op2/test_cases/sync/doc_comment.out5
-rw-r--r--ops/op2/test_cases/sync/result_primitive.out59
-rw-r--r--ops/op2/test_cases/sync/result_primitive.rs2
-rw-r--r--ops/op2/test_cases/sync/result_void.out59
-rw-r--r--ops/op2/test_cases/sync/result_void.rs2
-rw-r--r--ops/op2/test_cases/sync/smi.out7
7 files changed, 119 insertions, 22 deletions
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<deno_core::v8::Object>) -> () {
- 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::<deno_core::v8::External>::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::<deno_core::v8::External>::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<deno_core::v8::Object>,
+ 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<u32, AnyError> {}
}
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<u32, AnyError> {}
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::<deno_core::v8::External>::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::<deno_core::v8::External>::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<deno_core::v8::Object>,
+ 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 {}