Skip to content

Commit 1a499c5

Browse files
joyeecheungtargos
authored andcommittedNov 10, 2023
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 Backport-PR-URL: #46425 Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com> Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
1 parent e0e09ca commit 1a499c5

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
@@ -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();
@@ -266,8 +266,9 @@ function setupFetch() {
266266
});
267267

268268
// The WebAssembly Web API: https://webassembly.github.io/spec/web-api
269-
const { wasmStreamingCallback } = require('internal/wasm_web_api');
270-
internalBinding('wasm_web_api').setImplementation(wasmStreamingCallback);
269+
internalBinding('wasm_web_api').setImplementation((streamState, source) => {
270+
require('internal/wasm_web_api').wasmStreamingCallback(streamState, source);
271+
});
271272
}
272273

273274
// TODO(aduh95): move this to internal/bootstrap/browser when the CLI flag is
@@ -324,12 +325,12 @@ function setupStacktracePrinterOnSigint() {
324325
}
325326

326327
function initializeReport() {
327-
const { report } = require('internal/process/report');
328328
ObjectDefineProperty(process, 'report', {
329329
__proto__: null,
330330
enumerable: true,
331331
configurable: true,
332332
get() {
333+
const { report } = require('internal/process/report');
333334
return report;
334335
},
335336
});
@@ -344,9 +345,10 @@ function setupDebugEnv() {
344345

345346
// This has to be called after initializeReport() is called
346347
function initializeReportSignalHandlers() {
347-
const { addSignalHandler } = require('internal/process/report');
348-
349-
addSignalHandler();
348+
if (getOptionValue('--report-on-signal')) {
349+
const { addSignalHandler } = require('internal/process/report');
350+
addSignalHandler();
351+
}
350352
}
351353

352354
function initializeHeapSnapshotSignalHandlers() {
@@ -545,8 +547,6 @@ function initializeCJSLoader() {
545547
}
546548

547549
function initializeESMLoader() {
548-
if (getEmbedderOptions().shouldNotRegisterESMLoader) return;
549-
550550
const { initializeESM } = require('internal/modules/esm/utils');
551551
initializeESM();
552552

‎test/parallel/test-bootstrap-modules.js

-3
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/webidl',
8481
'NativeModule internal/worker/js_transferable',
8582
'Internal Binding blob',

0 commit comments

Comments
 (0)
Please sign in to comment.