Skip to content

Commit 5bc398f

Browse files
committed
bootstrap: optimize modules loaded in the built-in snapshot
Preload essential modules and lazy-load non-essential ones. After this patch, all modules listed by running this snippet: ``` const list = process.moduleLoadList.join('\n'); require('fs').writeSync(1, list, 'utf-8'); ``` (which is roughly the same list as the one in test-bootstrap-module.js for the main thread) are loaded from the snapshot so no additional compilation cost is incurred. PR-URL: nodejs/node#45849 Backport-PR-URL: nodejs/node#46425 Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com> Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
1 parent db8ea44 commit 5bc398f

3 files changed

Lines changed: 38 additions & 12 deletions

File tree

graal-nodejs/lib/internal/bootstrap/switches/is_main_thread.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,3 +286,32 @@ rawMethods.resetStdioForTesting = function() {
286286
stdout = undefined;
287287
stderr = undefined;
288288
};
289+
290+
// Needed by the module loader and generally needed everywhere.
291+
require('fs');
292+
require('util');
293+
require('url');
294+
295+
require('internal/modules/cjs/loader');
296+
require('internal/modules/esm/utils');
297+
require('internal/vm/module');
298+
// Needed to refresh the time origin.
299+
require('internal/perf/utils');
300+
// Needed to register the async hooks.
301+
if (internalBinding('config').hasInspector) {
302+
require('internal/inspector_async_hook');
303+
}
304+
// Needed to set the wasm web API callbacks.
305+
internalBinding('wasm_web_api');
306+
// Needed to detect whether it's on main thread.
307+
internalBinding('worker');
308+
// Needed to setup source maps.
309+
require('internal/source_map/source_map_cache');
310+
// Needed by most execution modes.
311+
require('internal/modules/run_main');
312+
// Needed to refresh DNS configurations.
313+
require('internal/dns/utils');
314+
// Needed by almost all execution modes. It's fine to
315+
// load them into the snapshot as long as we don't run
316+
// any of the initialization.
317+
require('internal/process/pre_execution');

graal-nodejs/lib/internal/process/pre_execution.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ const {
1414

1515
const {
1616
getOptionValue,
17-
getEmbedderOptions,
1817
refreshOptions,
1918
} = require('internal/options');
2019
const { reconnectZeroFillToggle } = require('internal/buffer');
@@ -74,6 +73,7 @@ function prepareExecution(options) {
7473
initializeReport();
7574
initializeSourceMapsHandlers();
7675
initializeDeprecations();
76+
7777
require('internal/dns/utils').initializeDns();
7878

7979
setupSymbolDisposePolyfill();
@@ -275,8 +275,9 @@ function setupFetch() {
275275
}
276276

277277
// The WebAssembly Web API: https://webassembly.github.io/spec/web-api
278-
const { wasmStreamingCallback } = require('internal/wasm_web_api');
279-
internalBinding('wasm_web_api').setImplementation(wasmStreamingCallback);
278+
internalBinding('wasm_web_api').setImplementation((streamState, source) => {
279+
require('internal/wasm_web_api').wasmStreamingCallback(streamState, source);
280+
});
280281
}
281282

282283
// TODO(aduh95): move this to internal/bootstrap/browser when the CLI flag is
@@ -333,12 +334,12 @@ function setupStacktracePrinterOnSigint() {
333334
}
334335

335336
function initializeReport() {
336-
const { report } = require('internal/process/report');
337337
ObjectDefineProperty(process, 'report', {
338338
__proto__: null,
339339
enumerable: true,
340340
configurable: true,
341341
get() {
342+
const { report } = require('internal/process/report');
342343
return report;
343344
},
344345
});
@@ -353,9 +354,10 @@ function setupDebugEnv() {
353354

354355
// This has to be called after initializeReport() is called
355356
function initializeReportSignalHandlers() {
356-
const { addSignalHandler } = require('internal/process/report');
357-
358-
addSignalHandler();
357+
if (getOptionValue('--report-on-signal')) {
358+
const { addSignalHandler } = require('internal/process/report');
359+
addSignalHandler();
360+
}
359361
}
360362

361363
function initializeHeapSnapshotSignalHandlers() {
@@ -554,8 +556,6 @@ function initializeCJSLoader() {
554556
}
555557

556558
function initializeESMLoader() {
557-
if (getEmbedderOptions().shouldNotRegisterESMLoader) return;
558-
559559
const { initializeESM } = require('internal/modules/esm/utils');
560560
initializeESM();
561561

graal-nodejs/test/parallel/test-bootstrap-modules.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ const expectedModules = new Set([
2424
'Internal Binding options',
2525
'Internal Binding performance',
2626
'Internal Binding process_methods',
27-
'Internal Binding report',
2827
'Internal Binding string_decoder',
2928
'Internal Binding symbols',
3029
'Internal Binding task_queue',
@@ -65,7 +64,6 @@ const expectedModules = new Set([
6564
'NativeModule internal/process/per_thread',
6665
'NativeModule internal/process/pre_execution',
6766
'NativeModule internal/process/promises',
68-
'NativeModule internal/process/report',
6967
'NativeModule internal/process/signal',
7068
'NativeModule internal/process/task_queues',
7169
'NativeModule internal/process/warning',
@@ -80,7 +78,6 @@ const expectedModules = new Set([
8078
'NativeModule internal/validators',
8179
'NativeModule internal/vm',
8280
'NativeModule internal/vm/module',
83-
'NativeModule internal/wasm_web_api',
8481
'NativeModule internal/webidl',
8582
'NativeModule internal/worker/js_transferable',
8683
'Internal Binding blob',

0 commit comments

Comments
 (0)