Skip to content

Commit b5fb3fb

Browse files
joyeecheungpanva
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 b1a1798 commit b5fb3fb

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
@@ -11,7 +11,6 @@ const {
1111

1212
const {
1313
getOptionValue,
14-
getEmbedderOptions,
1514
refreshOptions,
1615
} = require('internal/options');
1716
const { reconnectZeroFillToggle } = require('internal/buffer');
@@ -79,6 +78,7 @@ function prepareExecution(options) {
7978
initializeSourceMapsHandlers();
8079
initializeDeprecations();
8180
initializeWASI();
81+
8282
require('internal/dns/utils').initializeDns();
8383

8484
if (isMainThread) {
@@ -261,8 +261,9 @@ function setupFetch() {
261261
});
262262

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

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

329330
function initializeReport() {
330-
const { report } = require('internal/process/report');
331331
ObjectDefineProperty(process, 'report', {
332332
__proto__: null,
333333
enumerable: true,
334334
configurable: true,
335335
get() {
336+
const { report } = require('internal/process/report');
336337
return report;
337338
}
338339
});
@@ -347,9 +348,10 @@ function setupDebugEnv() {
347348

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

355357
function initializeHeapSnapshotSignalHandlers() {
@@ -555,8 +557,6 @@ function initializeCJSLoader() {
555557
}
556558

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

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)