diff options
author | Matt Mastracci <matthew@mastracci.com> | 2024-03-22 13:49:07 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-03-22 20:49:07 +0000 |
commit | 08ec6e5831478f6d15188e91d9a8e3c00d80c1ed (patch) | |
tree | 13a830c1ab1ee2ce5ebcffe5c0fcecd77146c8bc /ext/node/polyfills/02_init.js | |
parent | 9c2f9f14e749e4dd63ad02e3ca8af5fc8bd112ee (diff) |
perf: warm expensive init code at snapshot time (#22714)
Slightly different approach to similar changes in #22386
Note that this doesn't use a warmup script -- we are actually just doing
more work at snapshot time.
Diffstat (limited to 'ext/node/polyfills/02_init.js')
-rw-r--r-- | ext/node/polyfills/02_init.js | 67 |
1 files changed, 37 insertions, 30 deletions
diff --git a/ext/node/polyfills/02_init.js b/ext/node/polyfills/02_init.js index 85f924493..752f518bf 100644 --- a/ext/node/polyfills/02_init.js +++ b/ext/node/polyfills/02_init.js @@ -16,38 +16,32 @@ function initialize( runningOnMainThread, workerId, maybeWorkerMetadata, + warmup = false, ) { - if (initialized) { - throw Error("Node runtime already initialized"); - } - initialized = true; - if (usesLocalNodeModulesDir) { - requireImpl.setUsesLocalNodeModulesDir(); - } - const nativeModuleExports = requireImpl.nativeModuleExports; - nodeGlobals.Buffer = nativeModuleExports["buffer"].Buffer; - nodeGlobals.clearImmediate = nativeModuleExports["timers"].clearImmediate; - nodeGlobals.clearInterval = nativeModuleExports["timers"].clearInterval; - nodeGlobals.clearTimeout = nativeModuleExports["timers"].clearTimeout; - nodeGlobals.console = nativeModuleExports["console"]; - nodeGlobals.global = globalThis; - nodeGlobals.process = nativeModuleExports["process"]; - nodeGlobals.setImmediate = nativeModuleExports["timers"].setImmediate; - nodeGlobals.setInterval = nativeModuleExports["timers"].setInterval; - nodeGlobals.setTimeout = nativeModuleExports["timers"].setTimeout; - nodeGlobals.performance = nativeModuleExports["perf_hooks"].performance; + if (!warmup) { + if (initialized) { + throw Error("Node runtime already initialized"); + } + initialized = true; + if (usesLocalNodeModulesDir) { + requireImpl.setUsesLocalNodeModulesDir(); + } - // FIXME(bartlomieju): not nice to depend on `Deno` namespace here - // but it's the only way to get `args` and `version` and this point. - internals.__bootstrapNodeProcess(argv0, Deno.args, Deno.version); - internals.__initWorkerThreads( - runningOnMainThread, - workerId, - maybeWorkerMetadata, - ); - internals.__setupChildProcessIpcChannel(); - // `Deno[Deno.internal].requireImpl` will be unreachable after this line. - delete internals.requireImpl; + // FIXME(bartlomieju): not nice to depend on `Deno` namespace here + // but it's the only way to get `args` and `version` and this point. + internals.__bootstrapNodeProcess(argv0, Deno.args, Deno.version); + internals.__initWorkerThreads( + runningOnMainThread, + workerId, + maybeWorkerMetadata, + ); + internals.__setupChildProcessIpcChannel(); + // `Deno[Deno.internal].requireImpl` will be unreachable after this line. + delete internals.requireImpl; + } else { + // Warm up the process module + internals.__bootstrapNodeProcess(undefined, undefined, undefined, true); + } } function loadCjsModule(moduleName, isMain, inspectBrk) { @@ -63,3 +57,16 @@ internals.node = { initialize, loadCjsModule, }; + +const nativeModuleExports = requireImpl.nativeModuleExports; +nodeGlobals.Buffer = nativeModuleExports["buffer"].Buffer; +nodeGlobals.clearImmediate = nativeModuleExports["timers"].clearImmediate; +nodeGlobals.clearInterval = nativeModuleExports["timers"].clearInterval; +nodeGlobals.clearTimeout = nativeModuleExports["timers"].clearTimeout; +nodeGlobals.console = nativeModuleExports["console"]; +nodeGlobals.global = globalThis; +nodeGlobals.process = nativeModuleExports["process"]; +nodeGlobals.setImmediate = nativeModuleExports["timers"].setImmediate; +nodeGlobals.setInterval = nativeModuleExports["timers"].setInterval; +nodeGlobals.setTimeout = nativeModuleExports["timers"].setTimeout; +nodeGlobals.performance = nativeModuleExports["perf_hooks"].performance; |