GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — perf(javascript): parse every program through parseProgram, ESTree on request by alexander-akait · Pull Request #23073 · webpack/webpack

perf(javascript): parse every program through parseProgram, ESTree on request #23073

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.

Sign up for GitHub

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

Merged
alexander-akait merged 15 commits into main from perf/javascript-flat-estree
Nov 2, 2026

Uh oh!

There was an error while loading. Please reload this page.

Merged

perf(javascript): parse every program through parseProgram, ESTree on request #23073

Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
15 commits
Select commit Hold shift + click to select a range
7dcb6a4
perf(javascript): write a SyntaxTree's nodes children first
alexander-akait Oct 28, 2026
3fd8794
perf(javascript): regenerate types for the post-order SyntaxTree writer
alexander-akait Oct 28, 2026
cde0f18
perf(javascript): build a whole SyntaxTree's ESTree in one pass
alexander-akait Oct 28, 2026
ed5f763
perf(javascript): make lazy parsing flat by default, ESTree on request
alexander-akait Oct 29, 2026
d4d8f5c
perf(javascript): parse every program through parseProgram
alexander-akait Oct 29, 2026
8c092bc
refactor(javascript): type parseProgram with overloads, no casts to any
alexander-akait Oct 29, 2026
2ae76fa
refactor(javascript): validate the printer's regexps without a parse
alexander-akait Oct 29, 2026
a6a167a
fix(javascript): refuse an unterminated literal before validating a r…
alexander-akait Oct 29, 2026
921ff2b
docs(javascript): describe parseProgram as the one entry, parsedTree …
alexander-akait Nov 2, 2026
781c0a2
refactor(javascript): type addEstree's root as the node it reads, wit…
alexander-akait Nov 2, 2026
5056ed8
docs(changeset): keep the folded JavaScript entry to one short sentence
alexander-akait Nov 2, 2026
b137712
fix(javascript): guard and invalidate the tree on every parseProgram …
alexander-akait Nov 2, 2026
208adba
fix(javascript): write a node acorn shares between two fields once pe…
alexander-akait Nov 2, 2026
76936cb
fix(javascript): build a tree in id order only when every child comes…
alexander-akait Nov 2, 2026
f9ffe0c
fix(javascript): read words as a script once auto falls back to one
alexander-akait Nov 2, 2026
File filter
Filter by extension

Clear filters
Conversations
Failed to load comments.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Jump to
Jump to file
Failed to load files.
Loading

Uh oh!

There was an error while loading. Please reload this page.

Diff view
Diff view
Prev Previous commit
Next Next commit
perf(javascript): make lazy parsing flat by default, ESTree on request
`parse` with `lazyNodes` now writes a SyntaxTree read through `A` and
returns the program's node; `estree: true` keeps building ESTree objects,
which JavascriptParser and WebManifestParser ask for. Rebuilding a whole
program with `toEstreeObject` costs 30-55% more, so the option stays.

`toEstreeObject(node, fields?)` reads the last parse's tree, and any
tree builds its own with `SyntaxTree#estree`; the JavaScript path drops
its `tree` member, the printer reading `parsedTree()` once per print.

Claude-Session: https://claude.ai/code/session_013FhutQ8TLT8praLXM7AUrr
  • Loading branch information

Uh oh!

There was an error while loading. Please reload this page.

@alexander-akait
alexander-akait committed Nov 2, 2026
commit ed5f7631a598393922307cb70c31cc47f4540575

Some comments aren't visible on the classic Files Changed page.

There are no files selected for viewing

3 changes: 2 additions & 1 deletion 3 lib/asset/WebManifestParser.js
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ class WebManifestParser extends Parser {
ast = parse(`(${source})`, {
ecmaVersion: "latest",
sourceType: "script",
lazyNodes: true
lazyNodes: true,
estree: true
});
} catch (_err) {
// Not valid JSON — leave the file untouched (still emitted as-is).
Expand Down
3 changes: 3 additions & 0 deletions 3 lib/javascript/JavascriptParser.js
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,7 @@ class VariableInfo {
* @property {boolean=} allowHashBang
* @property {boolean=} allowReturnOutsideFunction
* @property {boolean=} lazyNodes internal: serve `range` lazily and skip the parser's location/range tracking
* @property {boolean=} estree internal: with `lazyNodes`, build ESTree objects rather than the flat `SyntaxTree`
* @property {Comment[]=} lazyComments internal: collect comments here without slicing their text eagerly
* @property {boolean=} importPhases enable parsing of the import phase proposals (import defer / import source)
* @property {boolean=} moduleFallback internal: for `auto`, let the parser downgrade module->script in place instead of re-parsing
Expand Down Expand Up @@ -7602,6 +7603,8 @@ class JavascriptParser extends Parser {
// Ranges alone (the main parse path and the concatenation re-parse) are
// served on demand; hoisted, since the auto-fallback re-enters here.
options.lazyNodes = wantLazyNodes;
// hooks read ESTree; lazy mode is otherwise flat
options.estree = true;
if (wantLazyNodes) {
// the lazy plugin replaces acorn's native tracking — disabling it
// here lets WebpackParser skip its defensive options copy
Expand Down
141 changes: 100 additions & 41 deletions 141 lib/javascript/syntax-parser.js
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -7603,7 +7603,7 @@ const stringToBigInt = (str) => BigInt(str.replace(/_/g, ""));
/** @typedef {typeof SyntaxParser} ParserClass the parser class itself, as a plugin receives and returns it */
/** @typedef {(BaseParser: ParserClass) => ParserClass} ParserPlugin a plugin: receives the parser class, returns an extended one */
/** @typedef {CollectedComment[] | ((isBlock: boolean, text: string, start: number, end: number) => void)} CommentSink where comments are collected, as an array or a callback */
/** @typedef {Omit<Options, "onComment"> & { onComment?: CommentSink, lazyNodes?: boolean, lazyComments?: CollectedComment[], importPhases?: boolean, moduleFallback?: boolean }} ParserOptions the parser's options, carrying webpack's own comment type plus its lazy-node and import-phase switches */
/** @typedef {Omit<Options, "onComment"> & { onComment?: CommentSink, lazyNodes?: boolean, lazyComments?: CollectedComment[], importPhases?: boolean, moduleFallback?: boolean, estree?: boolean, syntaxTree?: SyntaxTree }} ParserOptions the parser's options, carrying webpack's own comment type plus its lazy-node, ESTree and import-phase switches */

// Symbol-keyed so they stay out of for-in, Object.keys and JSON.stringify over
// AST nodes.
Expand Down Expand Up @@ -9826,6 +9826,28 @@ class WebpackParser extends BaseParser {
return /** @type {typeof WebpackParser} */ (super.extend(...plugins));
}

/**
* webpack's lazy mode is flat: it writes a `SyntaxTree` read through `A` and
* returns the program's node, unless `estree: true` asks for ESTree objects.
* @template {Partial<ParserOptions>} [O=Partial<ParserOptions>]
* @param {string} input the source text
* @param {O=} options parser options
* @returns {O extends { lazyNodes: true } ? O extends { estree: true } ? ParsedProgram : number : ParsedProgram} the program
*/
static parse(input, options) {
if (
options !== undefined &&
options.lazyNodes === true &&
options.estree !== true &&
options.syntaxTree === undefined
) {
return /** @type {EXPECTED_ANY} */ (
parseIntoTree(this, input, undefined, options)
);
}
return /** @type {EXPECTED_ANY} */ (super.parse(input, options));
}

/**
* @param {Options & { lazyNodes?: boolean, lazyComments?: CollectedComment[], importPhases?: boolean, moduleFallback?: boolean, syntaxTree?: SyntaxTree }} options options
* @param {string} input source code
Expand Down Expand Up @@ -19769,16 +19791,16 @@ const collectCjsRequireSpecifiers = (source) => {
};

/**
* Parses `input` into an estree program. The entry point for everything outside
* this module, so no caller needs the parser class or its underlying types.
* Parses `input`: with `lazyNodes`, into a `SyntaxTree` read through `A`, or
* ESTree objects under `estree: true`; acorn's ESTree program otherwise. The
* entry point for everything outside this module.
* @template {ParserOptions} O
* @param {string} input source code
* @param {ParserOptions} options parser options
* @returns {import("estree").Program} the parsed program
* @param {O} options parser options
* @returns {O extends { lazyNodes: true } ? O extends { estree: true } ? import("estree").Program : number : import("estree").Program} the program, or its node
*/
const parse = (input, options) =>
/** @type {import("estree").Program} */ (
WebpackParser.parse(input, /** @type {Options} */ (options))
);
/** @type {EXPECTED_ANY} */ (WebpackParser.parse(input, options));

// A field of a `SyntaxTree` node holds a child node, a list of them, a string,
// a flag, a literal's value, or a string the node lacks while unset.
Expand Down Expand Up @@ -20520,6 +20542,17 @@ class SyntaxTree {
return list;
}

/**
* The ESTree object of a node of this tree, as `toEstreeObject` builds it
* for the last parse's.
* @param {number} node the node, or `0` for none
* @param {string[]=} fields the fields to build, or every field
* @returns {import("estree").Node | null} the node's object, or null for none
*/
estree(node, fields) {
return buildEstreeObject(this, node, fields);
}

/**
* @param {number} node the node
* @returns {number} its `NodeType`
Expand Down Expand Up @@ -20726,6 +20759,16 @@ const buildFields = (tree, node, fields, pending) => {
if (fields === undefined) tree._complete[node] = 1;
};

/**
* The ESTree object of a node of the last parse, the same object each time it
* is asked for, as `SyntaxTree#estree` builds it.
* @param {number} node the node, or `0` for none
* @param {string[]=} fields the fields to build, or every field
* @returns {import("estree").Node | null} the node's object, or null for none
*/
const toEstreeObject = (node, fields) =>
/** @type {SyntaxTree} */ (walkTree).estree(node, fields);

/**
* The ESTree object of a node, the same object each time it is asked for.
* With `fields`, only those are built, each a whole subtree; a later call adds
Expand All @@ -20735,7 +20778,7 @@ const buildFields = (tree, node, fields, pending) => {
* @param {string[]=} fields the fields to build, or every field
* @returns {import("estree").Node | null} the node's object, or null for none
*/
const toEstreeObject = (tree, node, fields) => {
const buildEstreeObject = (tree, node, fields) => {
// WHY: a tree is written children first, so the root is its last node; with
// nothing built yet, one pass in id order builds each node from its built
// children, with none of the bookkeeping a partial build needs.
Expand Down Expand Up @@ -21306,14 +21349,6 @@ const A = {
walkStopped = true;
walkHalted = getHaltVisitors().haltVisitors(walkVisitors);
},
/**
* The tree walked, for a reader of many nodes that would rather not go
* through the path for each.
* @returns {SyntaxTree} the tree
*/
get tree() {
return /** @type {SyntaxTree} */ (walkTree);
},
/**
* @param {number} level how many levels further out than the parent
* @returns {number} a node enclosing the current one, 0 past the program
Expand Down Expand Up @@ -21611,40 +21646,22 @@ let walking = false;
* @returns {number | undefined} the program's node, or undefined where a syntax error stops the parse
*/
const parseProgram = (input, pos = 0, options = {}) => {
if (walking) {
throw new Error(
"javascript syntax: process() is running; a visitor cannot start another JavaScript parse"
);
}
if (programParsing) {
throw new Error(
"javascript syntax: parseProgram() is running; its parseError callback cannot start another JavaScript parse"
);
}
releaseParsedTree();
const tree = new SyntaxTree();
programParsing = true;
programParseError = options.parseError;
if (programErrorsSeen.size > 0) programErrorsSeen.clear();
let program = 0;
try {
program = /** @type {number} */ (
/** @type {unknown} */ (
new WebpackParser(
{
ecmaVersion: "latest",
...parserGoal(options.sourceType),
importPhases: true,
lazyNodes: true,
syntaxTree: tree
},
input,
pos === 0 ? undefined : pos
).parse()
)
);
return parseIntoTree(WebpackParser, input, pos === 0 ? undefined : pos, {
ecmaVersion: "latest",
...parserGoal(options.sourceType),
importPhases: true,
lazyNodes: true
});
} catch (error) {
tree.release();
if (error instanceof SyntaxError && !programParseErrorThrew) {
return undefined;
}
Expand All @@ -21654,12 +21671,53 @@ const parseProgram = (input, pos = 0, options = {}) => {
programParseError = undefined;
programParseErrorThrew = false;
}
};

/**
* Parses into a new `SyntaxTree`, which `A`, `toEstreeObject` and
* `parsedTree` then read in place of the last parse's.
* @param {typeof WebpackParser} Parser the parser class, perhaps extended
* @param {string} input source text
* @param {number | undefined} pos where the program starts
* @param {Partial<ParserOptions>} options the parser's options
* @returns {number} the program's node
*/
const parseIntoTree = (Parser, input, pos, options) => {
if (walking) {
throw new Error(
"javascript syntax: process() is running; a visitor cannot start another JavaScript parse"
);
}
releaseParsedTree();
const tree = new SyntaxTree();
let program;
try {
program = /** @type {number} */ (
/** @type {unknown} */ (
new Parser(
/** @type {ParserOptions} */ ({ ...options, syntaxTree: tree }),
input,
pos
).parse()
)
);
} catch (error) {
tree.release();
throw error;
}
walkTree = tree;
walkInput = input;
walkLineStarts = null;
return program;
};

/**
* The tree the last parse wrote, for a reader of many nodes, as the printer
* is, that would rather not go through `A` for each; valid until the next parse.
* @returns {SyntaxTree | null} the tree, null before any parse
*/
const parsedTree = () => walkTree;

/**
* Hand back the tree the last `parseProgram` left for `A` to read.
* @returns {void}
Expand Down Expand Up @@ -21871,6 +21929,7 @@ module.exports.overflowFrom = overflowFrom;
module.exports.hasOctalEscape = hasOctalEscape;
module.exports.parse = parse;
module.exports.parseProgram = parseProgram;
module.exports.parsedTree = parsedTree;
module.exports.positionAt = positionAt;
module.exports.releaseParserCaches = releaseParserCaches;
module.exports.toEstreeObject = toEstreeObject;
Expand Down
4 changes: 3 additions & 1 deletion 4 lib/javascript/syntax-printer.js
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -17780,7 +17780,9 @@ const createNodePrinter = (modules) => {
path = at;
const node = at.node;
if (context !== writer) {
tree = at.tree;
tree = /** @type {SyntaxTree} */ (
require("./syntax-parser").parsedTree()
);
let program = node;
for (let level = 0; at.ancestor(level) !== 0; level++) {
program = at.ancestor(level);
Expand Down
3 changes: 2 additions & 1 deletion 3 test/helpers/javascriptParse.js
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ const parse = (code, options) => {
allowReturnOutsideFunction: rest.sourceType === "script",
...rest,
ranges: lazyNodes ? false : rest.ranges,
lazyNodes
lazyNodes,
estree: true
};
if (collectComments === true) {
if (lazyNodes) parserOptions.lazyComments = comments;
Expand Down
3 changes: 2 additions & 1 deletion 3 test/specCases/js-fuzz.spectest.js
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,8 @@ const parseOurs = (code, isModule) =>
sourceType: isModule ? "module" : "script",
ecmaVersion: "latest",
ranges: false,
lazyNodes: true
lazyNodes: true,
estree: true
});

/**
Expand Down
3 changes: 2 additions & 1 deletion 3 test/specCases/test262-parser.spectest.js
Show comments
View file
Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ const parseOurs = (code, { comments: collectComments, ...options }) => {
...options,
allowReturnOutsideFunction: options.sourceType === "script",
ranges: lazyNodes ? false : options.ranges,
lazyNodes
lazyNodes,
estree: true
};
if (collectComments === true) {
if (lazyNodes) parserOptions.lazyComments = comments;
Expand Down
Loading
Oops, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

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.
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.