1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-21 15:04:11 -05:00

perf: snapshot runtime ops (#21127)

Closes https://github.com/denoland/deno/issues/21135

~1ms startup time improvement

---------

Signed-off-by: Divy Srivastava <dj.srivastava23@gmail.com>
Co-authored-by: David Sherret <dsherret@users.noreply.github.com>
This commit is contained in:
Divy Srivastava 2023-11-11 09:01:48 -08:00 committed by GitHub
parent 56e76242f3
commit 9f4a45561f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 84 additions and 56 deletions

View file

@ -307,6 +307,12 @@ pub enum DenoSubcommand {
Vendor(VendorFlags), Vendor(VendorFlags),
} }
impl DenoSubcommand {
pub fn is_run(&self) -> bool {
matches!(self, Self::Run(_))
}
}
impl Default for DenoSubcommand { impl Default for DenoSubcommand {
fn default() -> DenoSubcommand { fn default() -> DenoSubcommand {
DenoSubcommand::Repl(ReplFlags { DenoSubcommand::Repl(ReplFlags {

View file

@ -392,6 +392,20 @@ fn create_cli_snapshot(snapshot_path: PathBuf) -> CreateSnapshotOutput {
deno_fs::deno_fs::init_ops::<PermissionsContainer>(fs.clone()), deno_fs::deno_fs::init_ops::<PermissionsContainer>(fs.clone()),
deno_node::deno_node::init_ops::<PermissionsContainer>(None, fs), deno_node::deno_node::init_ops::<PermissionsContainer>(None, fs),
deno_runtime::runtime::init_ops(), deno_runtime::runtime::init_ops(),
deno_runtime::ops::runtime::deno_runtime::init_ops(
"deno:runtime".parse().unwrap(),
),
deno_runtime::ops::worker_host::deno_worker_host::init_ops(
Arc::new(|_| unreachable!("not used in snapshot.")),
None,
),
deno_runtime::ops::fs_events::deno_fs_events::init_ops(),
deno_runtime::ops::os::deno_os::init_ops(Default::default()),
deno_runtime::ops::permissions::deno_permissions::init_ops(),
deno_runtime::ops::process::deno_process::init_ops(),
deno_runtime::ops::signal::deno_signal::init_ops(),
deno_runtime::ops::tty::deno_tty::init_ops(),
deno_runtime::ops::http::deno_http_runtime::init_ops(),
cli::init_ops_and_esm(), // NOTE: This needs to be init_ops_and_esm! cli::init_ops_and_esm(), // NOTE: This needs to be init_ops_and_esm!
]; ];

View file

@ -668,6 +668,10 @@ impl CliFactory {
) -> Result<CliMainWorkerOptions, AnyError> { ) -> Result<CliMainWorkerOptions, AnyError> {
Ok(CliMainWorkerOptions { Ok(CliMainWorkerOptions {
argv: self.options.argv().clone(), argv: self.options.argv().clone(),
// This optimization is only available for "run" subcommand
// because we need to register new ops for testing and jupyter
// integration.
skip_op_registration: self.options.sub_command().is_run(),
log_level: self.options.log_level().unwrap_or(log::Level::Info).into(), log_level: self.options.log_level().unwrap_or(log::Level::Info).into(),
coverage_dir: self.options.coverage_dir(), coverage_dir: self.options.coverage_dir(),
enable_op_summary_metrics: self.options.enable_op_summary_metrics(), enable_op_summary_metrics: self.options.enable_op_summary_metrics(),

View file

@ -82,6 +82,16 @@ impl ByonmCliNpmResolver {
} }
impl NpmResolver for ByonmCliNpmResolver { impl NpmResolver for ByonmCliNpmResolver {
fn get_npm_process_state(&self) -> String {
serde_json::to_string(&NpmProcessState {
kind: NpmProcessStateKind::Byonm,
local_node_modules_path: Some(
self.root_node_modules_dir.to_string_lossy().to_string(),
),
})
.unwrap()
}
fn resolve_package_folder_from_package( fn resolve_package_folder_from_package(
&self, &self,
name: &str, name: &str,
@ -248,16 +258,6 @@ impl CliNpmResolver for ByonmCliNpmResolver {
) )
} }
fn get_npm_process_state(&self) -> String {
serde_json::to_string(&NpmProcessState {
kind: NpmProcessStateKind::Byonm,
local_node_modules_path: Some(
self.root_node_modules_dir.to_string_lossy().to_string(),
),
})
.unwrap()
}
fn check_state_hash(&self) -> Option<u64> { fn check_state_hash(&self) -> Option<u64> {
// it is very difficult to determine the check state hash for byonm // it is very difficult to determine the check state hash for byonm
// so we just return None to signify check caching is not supported // so we just return None to signify check caching is not supported

View file

@ -492,6 +492,23 @@ impl ManagedCliNpmResolver {
} }
impl NpmResolver for ManagedCliNpmResolver { impl NpmResolver for ManagedCliNpmResolver {
/// Gets the state of npm for the process.
fn get_npm_process_state(&self) -> String {
serde_json::to_string(&NpmProcessState {
kind: NpmProcessStateKind::Snapshot(
self
.resolution
.serialized_valid_snapshot()
.into_serialized(),
),
local_node_modules_path: self
.fs_resolver
.node_modules_path()
.map(|p| p.to_string_lossy().to_string()),
})
.unwrap()
}
fn resolve_package_folder_from_package( fn resolve_package_folder_from_package(
&self, &self,
name: &str, name: &str,
@ -571,23 +588,6 @@ impl CliNpmResolver for ManagedCliNpmResolver {
self.resolve_pkg_folder_from_pkg_id(&pkg_id) self.resolve_pkg_folder_from_pkg_id(&pkg_id)
} }
/// Gets the state of npm for the process.
fn get_npm_process_state(&self) -> String {
serde_json::to_string(&NpmProcessState {
kind: NpmProcessStateKind::Snapshot(
self
.resolution
.serialized_valid_snapshot()
.into_serialized(),
),
local_node_modules_path: self
.fs_resolver
.node_modules_path()
.map(|p| p.to_string_lossy().to_string()),
})
.unwrap()
}
fn check_state_hash(&self) -> Option<u64> { fn check_state_hash(&self) -> Option<u64> {
// We could go further and check all the individual // We could go further and check all the individual
// npm packages, but that's probably overkill. // npm packages, but that's probably overkill.

View file

@ -83,9 +83,6 @@ pub trait CliNpmResolver: NpmResolver {
referrer: &ModuleSpecifier, referrer: &ModuleSpecifier,
) -> Result<PathBuf, AnyError>; ) -> Result<PathBuf, AnyError>;
/// Gets the state of npm for the process.
fn get_npm_process_state(&self) -> String;
/// Returns a hash returning the state of the npm resolver /// Returns a hash returning the state of the npm resolver
/// or `None` if the state currently can't be determined. /// or `None` if the state currently can't be determined.
fn check_state_hash(&self) -> Option<u64>; fn check_state_hash(&self) -> Option<u64>;

View file

@ -1,30 +1,23 @@
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license. // Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
use std::sync::Arc;
use crate::npm::CliNpmResolver;
use deno_core::error::AnyError;
use deno_core::op2;
use deno_core::Extension; use deno_core::Extension;
use deno_core::OpState;
pub mod bench; pub mod bench;
pub mod jupyter; pub mod jupyter;
pub mod testing; pub mod testing;
pub fn cli_exts(npm_resolver: Arc<dyn CliNpmResolver>) -> Vec<Extension> { pub fn cli_exts() -> Vec<Extension> {
vec![ vec![
#[cfg(not(feature = "__runtime_js_sources"))] #[cfg(not(feature = "__runtime_js_sources"))]
cli::init_ops(npm_resolver), cli::init_ops(),
#[cfg(feature = "__runtime_js_sources")] #[cfg(feature = "__runtime_js_sources")]
cli::init_ops_and_esm(npm_resolver), cli::init_ops_and_esm(),
] ]
} }
// ESM parts duplicated in `../build.rs`. Keep in sync! // ESM parts duplicated in `../build.rs`. Keep in sync!
deno_core::extension!(cli, deno_core::extension!(cli,
deps = [runtime], deps = [runtime],
ops = [op_npm_process_state],
esm_entry_point = "ext:cli/99_main.js", esm_entry_point = "ext:cli/99_main.js",
esm = [ esm = [
dir "js", dir "js",
@ -32,12 +25,6 @@ deno_core::extension!(cli,
"40_jupyter.js", "40_jupyter.js",
"99_main.js" "99_main.js"
], ],
options = {
npm_resolver: Arc<dyn CliNpmResolver>,
},
state = |state, options| {
state.put(options.npm_resolver);
},
customizer = |ext: &mut deno_core::Extension| { customizer = |ext: &mut deno_core::Extension| {
ext.esm_files.to_mut().push(deno_core::ExtensionFileSource { ext.esm_files.to_mut().push(deno_core::ExtensionFileSource {
specifier: "ext:cli/runtime/js/99_main.js", specifier: "ext:cli/runtime/js/99_main.js",
@ -47,10 +34,3 @@ deno_core::extension!(cli,
}); });
}, },
); );
#[op2]
#[string]
fn op_npm_process_state(state: &mut OpState) -> Result<String, AnyError> {
let npm_resolver = state.borrow_mut::<Arc<dyn CliNpmResolver>>();
Ok(npm_resolver.get_npm_process_state())
}

View file

@ -462,6 +462,7 @@ pub async fn run(
strace_ops: None, strace_ops: None,
is_inspecting: false, is_inspecting: false,
is_npm_main: main_module.scheme() == "npm", is_npm_main: main_module.scheme() == "npm",
skip_op_registration: true,
location: metadata.location, location: metadata.location,
maybe_binary_npm_command_name: NpmPackageReqReference::from_specifier( maybe_binary_npm_command_name: NpmPackageReqReference::from_specifier(
main_module, main_module,

View file

@ -101,6 +101,7 @@ pub struct CliMainWorkerOptions {
pub seed: Option<u64>, pub seed: Option<u64>,
pub unsafely_ignore_certificate_errors: Option<Vec<String>>, pub unsafely_ignore_certificate_errors: Option<Vec<String>>,
pub unstable: bool, pub unstable: bool,
pub skip_op_registration: bool,
pub maybe_root_package_json_deps: Option<PackageJsonDeps>, pub maybe_root_package_json_deps: Option<PackageJsonDeps>,
} }
@ -528,7 +529,7 @@ impl CliMainWorkerFactory {
.join(checksum::gen(&[key.as_bytes()])) .join(checksum::gen(&[key.as_bytes()]))
}); });
let mut extensions = ops::cli_exts(shared.npm_resolver.clone()); let mut extensions = ops::cli_exts();
extensions.append(&mut custom_extensions); extensions.append(&mut custom_extensions);
// TODO(bartlomieju): this is cruft, update FeatureChecker to spit out // TODO(bartlomieju): this is cruft, update FeatureChecker to spit out
@ -596,6 +597,7 @@ impl CliMainWorkerFactory {
), ),
stdio, stdio,
feature_checker, feature_checker,
skip_op_registration: shared.options.skip_op_registration,
}; };
let worker = MainWorker::bootstrap_from_options( let worker = MainWorker::bootstrap_from_options(
@ -706,7 +708,7 @@ fn create_web_worker_callback(
let create_web_worker_cb = let create_web_worker_cb =
create_web_worker_callback(shared.clone(), stdio.clone()); create_web_worker_callback(shared.clone(), stdio.clone());
let extensions = ops::cli_exts(shared.npm_resolver.clone()); let extensions = ops::cli_exts();
let maybe_storage_key = shared let maybe_storage_key = shared
.storage_key_resolver .storage_key_resolver

View file

@ -14,6 +14,7 @@ use deno_core::v8;
use deno_core::v8::ExternalReference; use deno_core::v8::ExternalReference;
use deno_core::JsRuntime; use deno_core::JsRuntime;
use deno_core::ModuleSpecifier; use deno_core::ModuleSpecifier;
use deno_core::OpState;
use deno_fs::sync::MaybeSend; use deno_fs::sync::MaybeSend;
use deno_fs::sync::MaybeSync; use deno_fs::sync::MaybeSync;
use once_cell::sync::Lazy; use once_cell::sync::Lazy;
@ -75,6 +76,16 @@ impl NodePermissions for AllowAllNodePermissions {
pub type NpmResolverRc = deno_fs::sync::MaybeArc<dyn NpmResolver>; pub type NpmResolverRc = deno_fs::sync::MaybeArc<dyn NpmResolver>;
pub trait NpmResolver: std::fmt::Debug + MaybeSend + MaybeSync { pub trait NpmResolver: std::fmt::Debug + MaybeSend + MaybeSync {
/// Gets a string containing the serialized npm state of the process.
///
/// This will be set on the `DENO_DONT_USE_INTERNAL_NODE_COMPAT_STATE` environment
/// variable when doing a `child_process.fork`. The implementor can then check this environment
/// variable on startup to repopulate the internal npm state.
fn get_npm_process_state(&self) -> String {
// This method is only used in the CLI.
String::new()
}
/// Resolves an npm package folder path from an npm package referrer. /// Resolves an npm package folder path from an npm package referrer.
fn resolve_package_folder_from_package( fn resolve_package_folder_from_package(
&self, &self,
@ -139,6 +150,13 @@ fn op_node_is_promise_rejected(value: v8::Local<v8::Value>) -> bool {
promise.state() == v8::PromiseState::Rejected promise.state() == v8::PromiseState::Rejected
} }
#[op2]
#[string]
fn op_npm_process_state(state: &mut OpState) -> Result<String, AnyError> {
let npm_resolver = state.borrow_mut::<NpmResolverRc>();
Ok(npm_resolver.get_npm_process_state())
}
deno_core::extension!(deno_node, deno_core::extension!(deno_node,
deps = [ deno_io, deno_fs ], deps = [ deno_io, deno_fs ],
parameters = [P: NodePermissions], parameters = [P: NodePermissions],
@ -254,6 +272,7 @@ deno_core::extension!(deno_node,
op_node_build_os, op_node_build_os,
op_is_any_arraybuffer, op_is_any_arraybuffer,
op_node_is_promise_rejected, op_node_is_promise_rejected,
op_npm_process_state,
ops::require::op_require_init_paths, ops::require::op_require_init_paths,
ops::require::op_require_node_module_paths<P>, ops::require::op_require_node_module_paths<P>,
ops::require::op_require_proxy_path, ops::require::op_require_proxy_path,

View file

@ -464,7 +464,6 @@ impl WebWorker {
options.fs, options.fs,
), ),
// Runtime ops that are always initialized for WebWorkers // Runtime ops that are always initialized for WebWorkers
ops::web_worker::deno_web_worker::init_ops_and_esm(),
ops::runtime::deno_runtime::init_ops_and_esm(main_module.clone()), ops::runtime::deno_runtime::init_ops_and_esm(main_module.clone()),
ops::worker_host::deno_worker_host::init_ops_and_esm( ops::worker_host::deno_worker_host::init_ops_and_esm(
options.create_web_worker_cb.clone(), options.create_web_worker_cb.clone(),
@ -482,6 +481,7 @@ impl WebWorker {
enable_testing_features, enable_testing_features,
), ),
runtime::init_ops_and_esm(), runtime::init_ops_and_esm(),
ops::web_worker::deno_web_worker::init_ops_and_esm(),
]; ];
for extension in &mut extensions { for extension in &mut extensions {

View file

@ -94,6 +94,9 @@ pub struct WorkerOptions {
/// V8 snapshot that should be loaded on startup. /// V8 snapshot that should be loaded on startup.
pub startup_snapshot: Option<Snapshot>, pub startup_snapshot: Option<Snapshot>,
/// Should op registration be skipped?
pub skip_op_registration: bool,
/// Optional isolate creation parameters, such as heap limits. /// Optional isolate creation parameters, such as heap limits.
pub create_params: Option<v8::CreateParams>, pub create_params: Option<v8::CreateParams>,
@ -160,6 +163,7 @@ impl Default for WorkerOptions {
}), }),
fs: Arc::new(deno_fs::RealFs), fs: Arc::new(deno_fs::RealFs),
module_loader: Rc::new(FsModuleLoader), module_loader: Rc::new(FsModuleLoader),
skip_op_registration: false,
seed: None, seed: None,
unsafely_ignore_certificate_errors: Default::default(), unsafely_ignore_certificate_errors: Default::default(),
should_break_on_first_statement: Default::default(), should_break_on_first_statement: Default::default(),
@ -419,6 +423,7 @@ impl MainWorker {
.or_else(crate::js::deno_isolate_init), .or_else(crate::js::deno_isolate_init),
create_params: options.create_params, create_params: options.create_params,
source_map_getter: options.source_map_getter, source_map_getter: options.source_map_getter,
skip_op_registration: options.skip_op_registration,
get_error_class_fn: options.get_error_class_fn, get_error_class_fn: options.get_error_class_fn,
shared_array_buffer_store: options.shared_array_buffer_store.clone(), shared_array_buffer_store: options.shared_array_buffer_store.clone(),
compiled_wasm_module_store: options.compiled_wasm_module_store.clone(), compiled_wasm_module_store: options.compiled_wasm_module_store.clone(),