Skip to content

Commit d93d8ad

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: #45849 Reviewed-By: Geoffrey Booth <[email protected]> Reviewed-By: Chengzhong Wu <[email protected]>
1 parent 27e1616 commit d93d8ad

File tree

3 files changed

+38
-12
lines changed

3 files changed

+38
-12
lines changed

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

lib/internal/process/pre_execution.js

Lines changed: 9 additions & 9 deletions
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');
@@ -80,6 +79,7 @@ function prepareExecution(options) {
8079
initializeSourceMapsHandlers();
8180
initializeDeprecations();
8281
initializeWASI();
82+
8383
require('internal/dns/utils').initializeDns();
8484

8585
if (isMainThread) {
@@ -262,8 +262,9 @@ function setupFetch() {
262262
});
263263

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

269270
// TODO(aduh95): move this to internal/bootstrap/browser when the CLI flag is
@@ -328,12 +329,12 @@ function setupStacktracePrinterOnSigint() {
328329
}
329330

330331
function initializeReport() {
331-
const { report } = require('internal/process/report');
332332
ObjectDefineProperty(process, 'report', {
333333
__proto__: null,
334334
enumerable: true,
335335
configurable: true,
336336
get() {
337+
const { report } = require('internal/process/report');
337338
return report;
338339
}
339340
});
@@ -348,9 +349,10 @@ function setupDebugEnv() {
348349

349350
// This has to be called after initializeReport() is called
350351
function initializeReportSignalHandlers() {
351-
const { addSignalHandler } = require('internal/process/report');
352-
353-
addSignalHandler();
352+
if (getOptionValue('--report-on-signal')) {
353+
const { addSignalHandler } = require('internal/process/report');
354+
addSignalHandler();
355+
}
354356
}
355357

356358
function initializeHeapSnapshotSignalHandlers() {
@@ -556,8 +558,6 @@ function initializeCJSLoader() {
556558
}
557559

558560
function initializeESMLoader() {
559-
if (getEmbedderOptions().shouldNotRegisterESMLoader) return;
560-
561561
const { initializeESM } = require('internal/modules/esm/utils');
562562
initializeESM();
563563

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',
@@ -64,7 +63,6 @@ const expectedModules = new Set([
6463
'NativeModule internal/process/per_thread',
6564
'NativeModule internal/process/pre_execution',
6665
'NativeModule internal/process/promises',
67-
'NativeModule internal/process/report',
6866
'NativeModule internal/process/signal',
6967
'NativeModule internal/process/task_queues',
7068
'NativeModule internal/process/warning',
@@ -79,7 +77,6 @@ const expectedModules = new Set([
7977
'NativeModule internal/validators',
8078
'NativeModule internal/vm',
8179
'NativeModule internal/vm/module',
82-
'NativeModule internal/wasm_web_api',
8380
'NativeModule internal/worker/js_transferable',
8481
'Internal Binding blob',
8582
'NativeModule async_hooks',

0 commit comments

Comments
 (0)