From 4b6305f4f25fc76f974bbdcc9cdb139d5ab8f5f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Sat, 18 Mar 2023 07:51:21 -0400 Subject: perf(core): preserve ops between snapshots (#18080) This commit changes the build process in a way that preserves already registered ops in the snapshot. This allows us to skip creating hundreds of "v8::String" on each startup, but sadly there is still some op registration going on startup (however we're registering 49 ops instead of >200 ops). This situation could be further improved, by moving some of the ops from "runtime/" to a separate extension crates. --------- Co-authored-by: Divy Srivastava --- core/ops.rs | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) (limited to 'core/ops.rs') diff --git a/core/ops.rs b/core/ops.rs index ca465c821..3a276082f 100644 --- a/core/ops.rs +++ b/core/ops.rs @@ -19,10 +19,13 @@ use std::cell::RefCell; use std::ops::Deref; use std::ops::DerefMut; use std::pin::Pin; +use std::ptr::NonNull; use std::rc::Rc; use std::rc::Weak; use std::task::Context; use std::task::Poll; +use v8::fast_api::CFunctionInfo; +use v8::fast_api::CTypeInfo; /// Wrapper around a Future, which causes that Future to be polled immediately. /// @@ -155,11 +158,45 @@ pub struct OpCtx { pub id: OpId, pub state: Rc>, pub decl: Rc, + pub fast_fn_c_info: Option>, pub runtime_state: Weak>, // Index of the current realm into `JsRuntimeState::known_realms`. pub realm_idx: RealmIdx, } +impl OpCtx { + pub fn new( + id: OpId, + realm_idx: RealmIdx, + decl: Rc, + state: Rc>, + runtime_state: Weak>, + ) -> Self { + let mut fast_fn_c_info = None; + + if let Some(fast_fn) = &decl.fast_fn { + let args = CTypeInfo::new_from_slice(fast_fn.args()); + let ret = CTypeInfo::new(fast_fn.return_type()); + + // SAFETY: all arguments are coming from the trait and they have + // static lifetime + let c_fn = unsafe { + CFunctionInfo::new(args.as_ptr(), fast_fn.args().len(), ret.as_ptr()) + }; + fast_fn_c_info = Some(c_fn); + } + + OpCtx { + id, + state, + runtime_state, + decl, + realm_idx, + fast_fn_c_info, + } + } +} + /// Maintains the resources and ops inside a JS runtime. pub struct OpState { pub resource_table: ResourceTable, -- cgit v1.2.3