From 4380a09a0598c73aa434e2f0f3a34555e0bd55cb Mon Sep 17 00:00:00 2001 From: Matt Mastracci Date: Tue, 15 Aug 2023 13:36:36 -0600 Subject: feat(ext/node): eagerly bootstrap node (#20153) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit To fix bugs around detection of when node emulation is required, we will just eagerly initialize it. The improvements we make to reduce the impact of the startup time: - [x] Process stdin/stdout/stderr are lazily created - [x] node.js global proxy no longer allocates on each access check - [x] Process checks for `beforeExit` listeners before doing expensive shutdown work - [x] Process should avoid adding global event handlers until listeners are added Benchmarking this PR (`89de7e1ff`) vs main (`41cad2179`) ``` 12:36 $ third_party/prebuilt/mac/hyperfine --warmup 100 -S none './deno-41cad2179 run ./empty.js' './deno-89de7e1ff run ./empty.js' Benchmark 1: ./deno-41cad2179 run ./empty.js Time (mean ± σ): 24.3 ms ± 1.6 ms [User: 16.2 ms, System: 6.0 ms] Range (min … max): 21.1 ms … 29.1 ms 115 runs Benchmark 2: ./deno-89de7e1ff run ./empty.js Time (mean ± σ): 24.0 ms ± 1.4 ms [User: 16.3 ms, System: 5.6 ms] Range (min … max): 21.3 ms … 28.6 ms 126 runs ``` Fixes https://github.com/denoland/deno/issues/20142 Fixes https://github.com/denoland/deno/issues/15826 Fixes https://github.com/denoland/deno/issues/20028 --- runtime/js/99_main.js | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) (limited to 'runtime/js') diff --git a/runtime/js/99_main.js b/runtime/js/99_main.js index c8fdabc25..fdd82862c 100644 --- a/runtime/js/99_main.js +++ b/runtime/js/99_main.js @@ -438,6 +438,7 @@ function bootstrapMainRuntime(runtimeOptions) { if (hasBootstrapped) { throw new Error("Worker runtime already bootstrapped"); } + const nodeBootstrap = globalThis.nodeBootstrap; const { 0: args, @@ -456,6 +457,8 @@ function bootstrapMainRuntime(runtimeOptions) { 13: userAgent, 14: inspectFlag, // 15: enableTestingFeaturesFlag + 16: hasNodeModulesDir, + 17: maybeBinaryNpmCommandName, } = runtimeOptions; performance.setTimeOrigin(DateNow()); @@ -464,12 +467,13 @@ function bootstrapMainRuntime(runtimeOptions) { // Remove bootstrapping data from the global scope delete globalThis.__bootstrap; delete globalThis.bootstrap; + delete globalThis.nodeBootstrap; hasBootstrapped = true; // If the `--location` flag isn't set, make `globalThis.location` `undefined` and // writable, so that they can mock it themselves if they like. If the flag was // set, define `globalThis.location`, using the provided value. - if (location_ === undefined) { + if (location_ == null) { mainRuntimeGlobalProperties.location = { writable: true, }; @@ -542,6 +546,10 @@ function bootstrapMainRuntime(runtimeOptions) { ObjectDefineProperty(globalThis, "Deno", util.readOnly(finalDenoNs)); util.log("args", args); + + if (nodeBootstrap) { + nodeBootstrap(hasNodeModulesDir, maybeBinaryNpmCommandName); + } } function bootstrapWorkerRuntime( @@ -553,6 +561,8 @@ function bootstrapWorkerRuntime( throw new Error("Worker runtime already bootstrapped"); } + const nodeBootstrap = globalThis.nodeBootstrap; + const { 0: args, 1: cpuCount, @@ -570,6 +580,8 @@ function bootstrapWorkerRuntime( 13: userAgent, // 14: inspectFlag, 15: enableTestingFeaturesFlag, + 16: hasNodeModulesDir, + 17: maybeBinaryNpmCommandName, } = runtimeOptions; performance.setTimeOrigin(DateNow()); @@ -580,6 +592,7 @@ function bootstrapWorkerRuntime( // Remove bootstrapping data from the global scope delete globalThis.__bootstrap; delete globalThis.bootstrap; + delete globalThis.nodeBootstrap; hasBootstrapped = true; if (unstableFlag) { @@ -649,6 +662,10 @@ function bootstrapWorkerRuntime( // Setup `Deno` global - we're actually overriding already // existing global `Deno` with `Deno` namespace from "./deno.ts". ObjectDefineProperty(globalThis, "Deno", util.readOnly(finalDenoNs)); + + if (nodeBootstrap) { + nodeBootstrap(hasNodeModulesDir, maybeBinaryNpmCommandName); + } } globalThis.bootstrap = { -- cgit v1.2.3