From d24c6ea27f7dea57e3bc0cda342d6cbe59782f7d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Wed, 8 Mar 2023 07:43:26 -0400 Subject: refactor(runtime): conditionally register Extension with source files (#18068) Since we are snapshotting extension source at build time, there's no need to define list of sources for the extension at runtime. This commit changes "deno_node" extension by removing "init_polyfill" function in favor of "init_polyfill_ops_and_esm()" and "init_polyfill_ops()". The former is used during snapshot and when "deno_runtime" is compiled with "dont_create_runtime_snapshot" cargo feature flag. The latter is used when running a worker from an existing snapshot. This is a start of a bigger refactor to all extensions - thanks to this change, we don't have to iterate over all defined source files for extension at runtime, and because of that we don't have to create a filepath for each of the source files. It's not a big deal, but we are iterating over 300 files on each start, and concatenating 3 strings before creating a "PathBuf" for ~200 of them. This is already visible on the startup flamegraphs and should be avoided. --- runtime/worker.rs | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) (limited to 'runtime/worker.rs') diff --git a/runtime/worker.rs b/runtime/worker.rs index c10f9f36e..e624cb2b4 100644 --- a/runtime/worker.rs +++ b/runtime/worker.rs @@ -214,8 +214,7 @@ impl MainWorker { CreateCache(Arc::new(create_cache_fn)) }); - // Internal modules - let mut extensions: Vec = vec![ + let mut extensions = vec![ // Web APIs deno_webidl::init(), deno_console::init(), @@ -263,7 +262,6 @@ impl MainWorker { options.unsafely_ignore_certificate_errors.clone(), ), deno_napi::init::(), - deno_node::init_polyfill(), deno_node::init::(options.npm_resolver), ops::os::init(exit_code.clone()), ops::permissions::init(), @@ -273,9 +271,18 @@ impl MainWorker { deno_http::init(), deno_flash::init::(unstable), ops::http::init(), - // Permissions ext (worker specific state) - perm_ext, ]; + + // TODO(bartlomieju): finish this work, currently only `deno_node` is different + // as it has the most files + #[cfg(feature = "dont_create_runtime_snapshot")] + extensions.push(deno_node::init_polyfill_ops_and_esm()); + + #[cfg(not(feature = "dont_create_runtime_snapshot"))] + extensions.push(deno_node::init_polyfill_ops()); + + extensions.push(perm_ext); + extensions.extend(std::mem::take(&mut options.extensions)); #[cfg(not(feature = "dont_create_runtime_snapshot"))] -- cgit v1.2.3