Skip to content

Keep parsing and analysis linear on long import lists, wrapped expressions and late module syntax - #1330

Open
tolgaergin wants to merge 4 commits into
deps/oxc-0.160from
perf/linear-parse-and-analysis-worst-cases
Open

tolgaergin wants to merge 4 commits into
deps/oxc-0.160from
perf/linear-parse-and-analysis-worst-cases

Conversation

@tolgaergin

@tolgaergin tolgaergin commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Acts on the Gemini 4 Neon performance review of this stack. Each item below is work that grows with what came before it rather than with the bytes read, so neither the speculation budget nor the stack floor bounded it. Most of it predates the oxc 0.160 update.

Parser (vendor/oxc_parser, described in LPM-PATCH.md)

Input Before (release) Change
import{a,a,…}from'x';export{b,b,…};, 40,000 each 2.9 s resolve_export_entries indexes the import entries by local name once there are more than 16 and a local export to look up, keeping the first entry for each name as the search did.
/*#__PURE__*/( nested 40,000 deep, also with a pure-annotated computed key at each level 1.1 s, or 5.1 s at 60,000 levels with keys ParserState::pure_walks remembers where each pure comment's walk started and what it found; a walk that reaches one takes its answer, so no node is walked twice.
8,000 lines of await before export {} in an unambiguous source 0.5 s, 1 GB arena reparse_potential_top_level_awaits skips the candidates an earlier statement parsed again took in.

Fixes to the top-level-await reparse. A statement parsed again as a module can end inside one its script reading took in. Upstream then lost the rest from the AST while keeping its tokens; Node runs that code:

  • await /a/\neval(y) lost eval(y), which the script read as part of await / a / eval(y).
  • await / a; x = /;0/ + eval(y); lost 0/ + eval(y).

Now:

  • The reparse goes on to the end of a statement the first parse read, so the rest is parsed again too.
  • It then drops only the statements that are fully covered, so none overlap and none is lost.
  • The module record keeps a dynamic import or import.meta recorded again only once.

The parser also checks both of its limits with one comparison per token. The lexer's stop_below starts at the stack floor and goes past every address once either limit is reached.

Parse speed over 300 MB of local package-store files (release, full LTO, interleaved runs, medians; statement counts identical across all three):

Options Published 0.160.0 Patch before This PR
Default 483 ms 494 ms (+2.2%) 491 ms (+1.7%)
Floor and budget, as lpm-security parses 484 ms* 500 ms (+3.2%) 493 ms (+1.9%)

* The published crate has neither option, so it parses without them.

lpm-security

The rules read through parentheses and TypeScript wrappers (as, satisfies, !, <T>, instantiations), and through sequences via their last expression. They also read a binding's initializer again for each reference to it. An initializer wrapped 10,000 deep and referenced 20,000 times took 2.8 s in a debug build, about 1 s per pass in release.

syntax.rs now measures, during the literal visit it already makes, the longest run of such wrappers that an expression sits in directly. A run continues only into the expression a wrapper wraps, or into a sequence's last expression, never into a type annotation.

  • A source over MAX_WRAPPER_RUN (32) keeps its comments and literals masked for the pattern checks, but the rules that read through wrappers don't run on it.
  • It counts as unparsed, so lpm audit reports the package's coverage as partial (unsupported_source_syntax) and lpm query refuses an incomplete analysis.
  • Across the 370,776 JavaScript and TypeScript files of a local package store, the longest run in real code is 5; 99.6% of files have 2 or fewer.

Tests

  • Parser timing (syntax.rs), each failing before its fix:
    • exports_of_many_imported_names_resolve_in_linear_time;
    • pure_comments_on_nested_parentheses_apply_in_linear_time, both shapes;
    • top_level_awaits_parsed_again_as_a_module_take_linear_time, 10,000 lines, so it also fails in release builds.
  • Parser correctness:
    • a_top_level_await_parsed_again_replaces_the_statements_it_takes_in covers whole statements, foo();, let, and the two eval sources above.
    • a_top_level_await_parsed_again_records_its_dynamic_import_once.
    • pure_comments_on_nested_wrappers_mark_the_call_inside_them, which passes with and without the cache.
  • Analysis (mod.rs):
    • code_after_a_top_level_await_parsed_again_is_analyzed checks that the eval sources are complete and report evaluation; it fails without the reparse change.
    • expressions_in_more_wrappers_than_real_code_has_are_read_without_parsing_in_linear_time.
    • expressions_in_as_many_wrappers_as_real_code_could_have_are_parsed pins 32 against 33 for every wrapper kind, the sequence rule, and type annotations.
    • a_source_in_too_many_wrappers_keeps_its_comments_and_literals_masked.

Review

Fable 5.2, Sol 6.2 and Opus 5.5 reviewed the first commit. Every finding was verified and fixed test-first in the commits after it:

  • the evictable one-entry pure cache;
  • code lost after a top-level await parsed again;
  • duplicate dynamic imports;
  • the unmasked fallback;
  • runs carrying into type annotations;
  • the unused import index;
  • a thin margin in the reparse timing test.

Checks run locally (macOS)

  • cargo clippy --workspace --all-targets --locked -- -D warnings: clean.
  • cargo fmt --check: clean.
  • cargo test -p lpm-security: 864 pass.
  • cargo test -p lpm-runner: 652 pass. Two dev_endpoint tests that bind real ports failed once under load; they passed in three reruns, and this PR doesn't touch them.
  • lpm-stack-floor.patch applied to the published 0.160.0 crate reproduces vendor/oxc_parser.
  • The full workspace gate runs before the stack is marked ready.

🤖 Generated with Claude Code

…sions and late module syntax

Some work in oxc's parser and in lpm-security's rules grew with what
came before it rather than with the bytes read, so neither the
speculation budget nor the stack floor bounded it. Measured in release
builds:

- Resolving exports searched every import entry for each local export:
  40,000 of each took 3 s. The import entries are indexed by local name
  once there are more than 16, keeping the first entry for each name.
- A /*#__PURE__*/ comment before nested parentheses walked every
  parenthesis inside them: 40,000 levels took 1.1 s. The walk for the
  last comment is remembered, and a walk that reaches where it started
  takes its answer.
- When module syntax is found late, each statement that starts with
  `await` is parsed again, including those an earlier statement parsed
  again had taken in: 8,000 lines took 0.5 s and 1 GB, and the program
  kept overlapping statements. Those candidates are skipped now, and
  covered statements dropped.
- The rules read through parentheses and TypeScript wrappers, and read
  a binding's initializer again for every reference to it. lpm-security
  now reads a source in which an expression sits in more than 32 such
  wrappers directly without parsing it, as it does one over its other
  budgets. Across 370,776 files of a local package store, real code
  wraps at most 5 deep.

The parser also checks both of its limits with one comparison per
token: the lexer's stop_below starts at the stack floor and goes past
every address once either limit is reached. On 300 MB of real files the
patched parser is about 2% slower than the published crate, where it was
2–3% slower.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@tolgaergin
tolgaergin added this pull request to stack #1328 November 25, 2026 03:03
tolgaergin and others added 3 commits November 25, 2026 03:19
…acks, and scope wrapper runs

Review of the previous commit:

- A pure comment inside a computed key at each level replaced the one
  remembered walk, so outer comments walked the whole chain again: 30,000
  levels took 6.8 s in a debug build. Every walk's start is remembered
  now, so no node is walked twice.
- A statement parsed again as a module could end inside a statement the
  first parse read, as `await\nawait /x/` ends inside the script's
  `await / x / foo()`, and dropping covered statements then lost
  `foo();`, as upstream's reparse also did. The reparse now parses on to
  the end of a statement the first parse read, and the module record
  keeps a dynamic import or `import.meta` recorded again only once.
- A source over the wrapper bound was read as raw text, so a string
  holding `eval(y)` became an evaluation finding. It keeps the masked
  text now and only skips the rules that read through wrappers.
- A run of wrappers continued into a TypeScript type's computed key. It
  continues only into the expression a wrapper wraps, or the last
  expression of a sequence.
- The import index is built only when there is a local export to look up.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…iming test

A statement parsed again parses on to the end of one the first parse read,
so it covers every statement it overlaps. Should a parse again stop early
on an error, a statement that reaches past it now stays instead of being
dropped: an overlap loses no code. The AST test now also covers
`await\nlet\nx = eval(y)`, whose parse again ends inside the script's
`let x = eval(y)`.

The top-level-await timing test parses 10,000 lines, so it fails without
the fix in release builds too, not only in debug ones.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…every wrapper kind counts

A statement parsed again as a module can end inside one its script
reading took in, as `await / a; x = /;0/ + eval(y);` and
`await /a/\neval(y)` do. Since the parse again goes on to the end of a
statement the first parse read, the code after it is parsed again and
analyzed; tests now cover both, through the AST and through lpm-security,
which skips its text checks for evaluation when the parse is complete.

The wrapper bound's tests cover each kind the rules read through: `as`,
`satisfies`, `<T>`, `!` and an instantiation, besides parentheses and
sequences. LPM-PATCH.md and the state field say why an address in
pure_walks can't name another node.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@tolgaergin
tolgaergin marked this pull request as ready for review November 25, 2026 03:39

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant