Skip to content

Commit 6e3d7f8

Browse files
joyeecheungtargos
authored andcommitted
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: #45849 Reviewed-By: Geoffrey Booth <[email protected]> Reviewed-By: Chengzhong Wu <[email protected]>
1 parent d181b76 commit 6e3d7f8

File tree

3 files changed

+38
-12
lines changed

3 files changed

+38
-12
lines changed

lib/internal/bootstrap/switches/is_main_thread.js

+29
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');

lib/internal/process/pre_execution.js

+9-9
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ const {
1212

1313
const {
1414
getOptionValue,
15-
getEmbedderOptions,
1615
refreshOptions,
1716
} = require('internal/options');
1817
const { reconnectZeroFillToggle } = require('internal/buffer');
@@ -81,6 +80,7 @@ function prepareExecution(options) {
8180
initializeSourceMapsHandlers();
8281
initializeDeprecations();
8382
initializeWASI();
83+
8484
require('internal/dns/utils').initializeDns();
8585

8686
if (isMainThread) {
@@ -263,8 +263,9 @@ function setupFetch() {
263263
});
264264

265265
// The WebAssembly Web API: https://webassembly.github.io/spec/web-api
266-
const { wasmStreamingCallback } = require('internal/wasm_web_api');
267-
internalBinding('wasm_web_api').setImplementation(wasmStreamingCallback);
266+
internalBinding('wasm_web_api').setImplementation((streamState, source) => {
267+
require('internal/wasm_web_api').wasmStreamingCallback(streamState, source);
268+
});
268269
}
269270

270271
// TODO(aduh95): move this to internal/bootstrap/browser when the CLI flag is
@@ -337,12 +338,12 @@ function setupStacktracePrinterOnSigint() {
337338
}
338339

339340
function initializeReport() {
340-
const { report } = require('internal/process/report');
341341
ObjectDefineProperty(process, 'report', {
342342
__proto__: null,
343343
enumerable: true,
344344
configurable: true,
345345
get() {
346+
const { report } = require('internal/process/report');
346347
return report;
347348
}
348349
});
@@ -357,9 +358,10 @@ function setupDebugEnv() {
357358

358359
// This has to be called after initializeReport() is called
359360
function initializeReportSignalHandlers() {
360-
const { addSignalHandler } = require('internal/process/report');
361-
362-
addSignalHandler();
361+
if (getOptionValue('--report-on-signal')) {
362+
const { addSignalHandler } = require('internal/process/report');
363+
addSignalHandler();
364+
}
363365
}
364366

365367
function initializeHeapSnapshotSignalHandlers() {
@@ -565,8 +567,6 @@ function initializeCJSLoader() {
565567
}
566568

567569
function initializeESMLoader() {
568-
if (getEmbedderOptions().shouldNotRegisterESMLoader) return;
569-
570570
const { initializeESM } = require('internal/modules/esm/utils');
571571
initializeESM();
572572

test/parallel/test-bootstrap-modules.js

-3
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ const expectedModules = new Set([
2525
'Internal Binding options',
2626
'Internal Binding performance',
2727
'Internal Binding process_methods',
28-
'Internal Binding report',
2928
'Internal Binding string_decoder',
3029
'Internal Binding symbols',
3130
'Internal Binding task_queue',
@@ -66,7 +65,6 @@ const expectedModules = new Set([
6665
'NativeModule internal/process/per_thread',
6766
'NativeModule internal/process/pre_execution',
6867
'NativeModule internal/process/promises',
69-
'NativeModule internal/process/report',
7068
'NativeModule internal/process/signal',
7169
'NativeModule internal/process/task_queues',
7270
'NativeModule internal/process/warning',
@@ -82,7 +80,6 @@ const expectedModules = new Set([
8280
'NativeModule internal/validators',
8381
'NativeModule internal/vm',
8482
'NativeModule internal/vm/module',
85-
'NativeModule internal/wasm_web_api',
8683
'NativeModule internal/worker/js_transferable',
8784
'NativeModule path',
8885
'NativeModule querystring',

0 commit comments

Comments
 (0)