-
-
Notifications
You must be signed in to change notification settings - Fork 35.2k
module: synchronously load most ES modules #62530
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
GeoffreyBooth
wants to merge
1
commit into
nodejs:main
Choose a base branch
from
GeoffreyBooth:synchronously-load-most-es-modules
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| 'use strict'; | ||
|
|
||
| const common = require('../common'); | ||
| const fs = require('fs'); | ||
| const path = require('path'); | ||
| const { spawnSync } = require('child_process'); | ||
| const tmpdir = require('../../test/common/tmpdir'); | ||
|
|
||
| const bench = common.createBenchmark(main, { | ||
| modules: [250, 500, 1000, 2000], | ||
| n: [30], | ||
| }); | ||
|
|
||
| function prepare(count) { | ||
| tmpdir.refresh(); | ||
| const dir = tmpdir.resolve('esm-graph'); | ||
| fs.mkdirSync(dir, { recursive: true }); | ||
|
|
||
| // Create a flat ESM graph: entry imports all modules directly. | ||
| // Each module is independent, maximizing the number of resolve/load/link | ||
| // operations in the loader pipeline. | ||
| const imports = []; | ||
| for (let i = 0; i < count; i++) { | ||
| fs.writeFileSync( | ||
| path.join(dir, `mod${i}.mjs`), | ||
| `export const value${i} = ${i};\n`, | ||
| ); | ||
| imports.push(`import './mod${i}.mjs';`); | ||
| } | ||
|
|
||
| const entry = path.join(dir, 'entry.mjs'); | ||
| fs.writeFileSync(entry, imports.join('\n') + '\n'); | ||
| return entry; | ||
| } | ||
|
|
||
| function main({ n, modules }) { | ||
| const entry = prepare(modules); | ||
| const cmd = process.execPath || process.argv[0]; | ||
| const warmup = 3; | ||
| const state = { finished: -warmup }; | ||
|
|
||
| while (state.finished < n) { | ||
| const child = spawnSync(cmd, [entry]); | ||
| if (child.status !== 0) { | ||
| console.log('---- STDOUT ----'); | ||
| console.log(child.stdout.toString()); | ||
| console.log('---- STDERR ----'); | ||
| console.log(child.stderr.toString()); | ||
| throw new Error(`Child process stopped with exit code ${child.status}`); | ||
| } | ||
| state.finished++; | ||
| if (state.finished === 0) { | ||
| bench.start(); | ||
| } | ||
| if (state.finished === n) { | ||
| bench.end(n); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| // Flags: --no-warnings | ||
| import { spawnPromisified } from '../common/index.mjs'; | ||
| import * as fixtures from '../common/fixtures.mjs'; | ||
| import { describe, it } from 'node:test'; | ||
| import assert from 'node:assert'; | ||
|
|
||
|
|
||
| describe('synchronous ESM loading', () => { | ||
| it('should create minimal promises for ESM importing ESM', async () => { | ||
| // import-esm.mjs imports imported-esm.mjs — a pure ESM graph. | ||
| const count = await getPromiseCount(fixtures.path('es-modules', 'import-esm.mjs')); | ||
| // V8's Module::Evaluate returns one promise for the entire graph. | ||
| assert.strictEqual(count, 1); | ||
| }); | ||
|
|
||
| it('should create minimal promises for ESM importing CJS', async () => { | ||
| // builtin-imports-case.mjs imports node:assert (builtin) + dep1.js and dep2.js (CJS). | ||
| const count = await getPromiseCount(fixtures.path('es-modules', 'builtin-imports-case.mjs')); | ||
| // V8 creates one promise for the ESM entry evaluation, plus one per CJS module | ||
| // in the graph (each CJS namespace is wrapped in a promise). | ||
| // entry (ESM, 1) + node:assert (CJS, 1) + dep1.js (CJS, 1) + dep2.js (CJS, 1) = 4. | ||
| assert.strictEqual(count, 4); | ||
| }); | ||
|
|
||
| it('should fall back to async evaluation for top-level await', async () => { | ||
| // tla/resolved.mjs uses top-level await, so the sync path detects TLA | ||
| // and falls back to async evaluation. | ||
| const count = await getPromiseCount(fixtures.path('es-modules', 'tla', 'resolved.mjs')); | ||
| // The async fallback creates more promises — just verify the module | ||
| // still runs successfully. The promise count will be higher than the | ||
| // sync path but should remain bounded. | ||
| assert(count > 1, `Expected TLA fallback to create multiple promises, got ${count}`); | ||
| }); | ||
|
|
||
| it('should create minimal promises when entry point is CJS importing ESM', async () => { | ||
| // When a CJS entry point uses require(esm), the ESM module is loaded via | ||
| // ModuleJobSync, so the same promise minimization applies. | ||
| const count = await getPromiseCount(fixtures.path('es-modules', 'require-esm-entry.cjs')); | ||
| // V8's Module::Evaluate returns one promise for the ESM module. | ||
| assert.strictEqual(count, 1); | ||
| }); | ||
| }); | ||
|
|
||
|
|
||
| async function getPromiseCount(entry) { | ||
| const { stdout, stderr, code } = await spawnPromisified(process.execPath, [ | ||
| '--require', fixtures.path('es-modules', 'promise-counter.cjs'), | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can use |
||
| entry, | ||
| ]); | ||
| assert.strictEqual(code, 0, `child failed:\nstdout: ${stdout}\nstderr: ${stderr}`); | ||
| const match = stdout.match(/PROMISE_COUNT=(\d+)/); | ||
| assert(match, `Expected PROMISE_COUNT in output, got: ${stdout}`); | ||
| return Number(match[1]); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| // Counts PROMISE async resources created during the process lifetime. | ||
| // Used by test-esm-sync-entry-point.mjs to verify the sync ESM loader | ||
| // path does not create unnecessary promises. | ||
| 'use strict'; | ||
| let count = 0; | ||
| require('async_hooks').createHook({ | ||
| init(id, type) { if (type === 'PROMISE') count++; }, | ||
| }).enable(); | ||
| process.on('exit', () => { | ||
| process.stdout.write(`PROMISE_COUNT=${count}\n`); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| require('./imported-esm.mjs'); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is
--inspect-brkan exception?