Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: reduce generated op code for metrics" #1041

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions core/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,8 @@ pub mod _ops {
pub use super::ops_metrics::dispatch_metrics_async;
pub use super::ops_metrics::dispatch_metrics_fast;
pub use super::ops_metrics::dispatch_metrics_slow;
pub use super::ops_metrics::with_metrics;
pub use super::ops_metrics::with_metrics_fast;
pub use super::ops_metrics::OpMetricsEvent;
pub use super::runtime::ops::*;
pub use super::runtime::ops_rust_to_v8::*;
Expand Down
37 changes: 37 additions & 0 deletions core/ops_metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::ops::OpCtx;
use crate::serde::Serialize;
use crate::OpDecl;
use crate::OpId;
use deno_core::v8;
use std::cell::Ref;
use std::cell::RefCell;
use std::cell::RefMut;
Expand Down Expand Up @@ -60,6 +61,42 @@ pub fn merge_op_metrics(
})
}

pub fn with_metrics<'s>(
info: &'s v8::FunctionCallbackInfo,
closure: impl FnOnce() -> usize,
) {
let args = v8::FunctionCallbackArguments::from_function_callback_info(info);

let opctx: &'s _ = unsafe {
&*(v8::Local::<v8::External>::cast_unchecked(args.data()).value()
as *const OpCtx)
};

dispatch_metrics_slow(opctx, deno_core::_ops::OpMetricsEvent::Dispatched);
let res = closure();
if res == 0 {
dispatch_metrics_slow(opctx, OpMetricsEvent::Completed);
} else {
dispatch_metrics_slow(opctx, OpMetricsEvent::Error);
}
}

pub fn with_metrics_fast<'s, Output>(
fast_api_callback_options: *mut v8::fast_api::FastApiCallbackOptions<'s>,
closure: impl FnOnce() -> Output,
) -> Output {
let opctx: &'s _ = unsafe {
&*(v8::Local::<v8::External>::cast_unchecked(
(*fast_api_callback_options).data,
)
.value() as *const deno_core::_ops::OpCtx)
};
dispatch_metrics_fast(opctx, deno_core::_ops::OpMetricsEvent::Dispatched);
let res = closure();
dispatch_metrics_fast(opctx, deno_core::_ops::OpMetricsEvent::Completed);
res
}

#[doc(hidden)]
pub fn dispatch_metrics_fast(opctx: &OpCtx, metrics: OpMetricsEvent) {
// SAFETY: this should only be called from ops where we know the function is Some
Expand Down
14 changes: 2 additions & 12 deletions ops/op2/dispatch_fast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -558,23 +558,13 @@ pub(crate) fn generate_dispatch_fast(
let (fastcall_names, fastcall_types): (Vec<_>, Vec<_>) =
fastsig.input_args(generator_state).into_iter().unzip();

let fast_fn = gs_quote!(generator_state(result, fast_api_callback_options, fast_function, fast_function_metrics) => {
let fast_fn = gs_quote!(generator_state(result, fast_function, fast_function_metrics) => {
#[allow(clippy::too_many_arguments)]
extern "C" fn #fast_function_metrics<'s>(
this: deno_core::v8::Local<deno_core::v8::Object>,
#( #fastcall_metrics_names: #fastcall_metrics_types, )*
) -> #output_type {
let #fast_api_callback_options: &'s mut _ =
unsafe { &mut *#fast_api_callback_options };
let opctx: &'s _ = unsafe {
&*(deno_core::v8::Local::<deno_core::v8::External>::cast_unchecked(
unsafe { #fast_api_callback_options.data }
).value() as *const deno_core::_ops::OpCtx)
};
deno_core::_ops::dispatch_metrics_fast(opctx, deno_core::_ops::OpMetricsEvent::Dispatched);
let res = Self::#fast_function( this, #( #fastcall_names, )* );
deno_core::_ops::dispatch_metrics_fast(opctx, deno_core::_ops::OpMetricsEvent::Completed);
res
deno_core::_ops::with_metrics_fast(fast_api_callback_options, || Self::#fast_function( this, #( #fastcall_names, )* ))
}

#[allow(clippy::too_many_arguments)]
Expand Down
16 changes: 2 additions & 14 deletions ops/op2/dispatch_slow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ pub(crate) fn generate_dispatch_slow(
};

Ok(
gs_quote!(generator_state(opctx, info, slow_function, slow_function_metrics) => {
gs_quote!(generator_state(info, slow_function, slow_function_metrics) => {
fn slow_function_impl<'s>(#info: &'s deno_core::v8::FunctionCallbackInfo) -> usize {
#[cfg(debug_assertions)]
let _reentrancy_check_guard = deno_core::_ops::reentrancy_check(&<Self as deno_core::_ops::Op>::DECL);
Expand All @@ -171,20 +171,8 @@ pub(crate) fn generate_dispatch_slow(

extern "C" fn #slow_function_metrics<'s>(#info: *const deno_core::v8::FunctionCallbackInfo) {
let info: &'s _ = unsafe { &*#info };
let args = deno_core::v8::FunctionCallbackArguments::from_function_callback_info(info);
deno_core::_ops::with_metrics(info, || Self::slow_function_impl(info));

let #opctx: &'s _ = unsafe {
&*(deno_core::v8::Local::<deno_core::v8::External>::cast_unchecked(args.data()).value()
as *const deno_core::_ops::OpCtx)
};

deno_core::_ops::dispatch_metrics_slow(#opctx, deno_core::_ops::OpMetricsEvent::Dispatched);
let res = Self::slow_function_impl(info);
if res == 0 {
deno_core::_ops::dispatch_metrics_slow(#opctx, deno_core::_ops::OpMetricsEvent::Completed);
} else {
deno_core::_ops::dispatch_metrics_slow(#opctx, deno_core::_ops::OpMetricsEvent::Error);
}
}
}),
)
Expand Down
23 changes: 4 additions & 19 deletions ops/op2/test_cases/async/async_deferred.out

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 4 additions & 19 deletions ops/op2/test_cases/async/async_lazy.out

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

49 changes: 5 additions & 44 deletions ops/op2/test_cases/sync/add.out

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 1 addition & 25 deletions ops/op2/test_cases/sync/add_options.out

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

49 changes: 5 additions & 44 deletions ops/op2/test_cases/sync/bigint.out

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

49 changes: 5 additions & 44 deletions ops/op2/test_cases/sync/bool.out

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading