Skip to content

parser: remove no-op work in modifier probes and parent fixup - #65832

Draft
demike wants to merge 2 commits into
microsoft:mainfrom
demike:parser-hotpath-efficiency
Draft

demike wants to merge 2 commits into
microsoft:mainfrom
demike:parser-hotpath-efficiency

Conversation

@demike

@demike demike commented Dec 6, 2026

Copy link
Copy Markdown

Part of #65830 (implements the two smallest code-level items from the profiling data; benchmark details in this comment).

This PR was authored with AI assistance (OpenCode harness, Qwen4.0 Max Preview, high reasoning) under human direction; the human reviewed the analysis, benchmarks, and diff at each step.

Motivation & Guidance

Motivation:
The user maintains TsUML2 (UML diagrams from TypeScript projects), which recently moved to the native 7.1 API. Profiling the engine-creation floor for that tool produced #65829 and #65830. The user then asked for the cache-free, GC-free, parallelism-free code-level wins to be implemented and benchmarked, and to combine the small ones into a PR.

Guidance:
The user's prompts, in order: "Perf Can we reduce the engine creation floor? Contribute to typescript 7.1?" → "and now perf improvements in code (no cache, no gc, no more cores)" → "Combine the small ones to a pr (benchmark before)". When prompted about the repo's milestone policy, the user chose the issue-comment route first; after reviewing the lean comment draft (and asking to cut a negative-result section as noise: "Isn't the pooling comment just generate noise"), they instructed opening this PR. Benchmark-before was an explicit requirement; a third change that regressed was dropped.

Analysis

CPU/alloc profiling of parse+bind (743k-line declaration-heavy corpus, --noCheck, data in #65830) showed two patterns of provably no-op work in tsc/internal/parser/parser.go:

  1. parseContextualModifier / parseAnyContextualModifier call p.mark() — copying the full ParserState — before checking whether the current token can match. On a mismatch (the common case: every non-accessor class/type member pays two failed get/set probes) the function immediately rewinds: a pure save/restore round-trip with no observable effect. Profile: mark 1.45% + rewind 2.18% flat CPU.

  2. finishNodeWithEnd → overrideParentInImmediateChildren walks ForEachChild for every finished node to set parent pointers. All kinds below ast.KindFirstNode (tokens, literals, identifiers, keywords) have no cases in the generated ForEachChild dispatch — the walk visits nothing. These are the majority of nodes in declaration-heavy code. Profile: 5.1% cum.

Fix

Two commits, one per pattern:

  1. Guard first: return false when the token cannot match, mark() only on a potential match. Observably identical — mark/rewind have no side effects beyond state save/restore.
  2. Skip the dispatch when node.Kind < ast.KindFirstNode. Conservative: any future leaf kind added above KindFirstNode simply falls through to the existing walk.

Benchmarks (interleaved A/B wall-clock medians, --noEmit --noCheck, no --extendedDiagnostics; noise floor 0.2%; linux-x64, 4 cores, Go 1.27.4; baseline main @ 7141cc0):

Workload baseline final delta
decl-heavy: dom ×16, 743k lines (n=21) 560.2 ms 542.3 ms −3.2%
decl-heavy (n=11, earlier session) 572.0 ms 542.3 ms −5.2%
expr-heavy: zod v4 src ×8, 312k lines, noResolve (n=15) 580.5 ms 572.8 ms −1.3%

Per-change within-session deltas: (1) −6.4% decl / −1.4% expr; (2) −3.0% decl / neutral expr. Shared host, ±2–3% session noise; combined effect −3 to −5% decl-heavy across sessions.

Alternate considered: pooling the temporary list buffers (parseListIndex/parseDelimitedList/parseModifiersEx) to cut the allocation churn also identified in #65830. Implemented and benchmarked: +29% regression (sync.Pool victimization under frequent GC; still +4% with GOGC=off), so it was dropped.

Tests: go test ./internal/parser/... ./internal/ast/... ./internal/binder/... pass (parser includes the fuzz seed corpus); gofmt clean; both benchmark corpora (~1M lines) parse exit-0 on every run. No behavior change intended — the changes remove no-op work only.


Opened as Draft per CONTRIBUTING: the linked issue has no assigned Milestone yet. Asking maintainers whether this direction is acceptable (and whether #65830 or a split-out issue could be milestoned); happy to run repo perf automation or adjust. If unwanted, feel free to close — the analysis and benchmarks remain in the issue for reference.

@typescript-automation typescript-automation Bot added the For Uncommitted Bug PR for untriaged, rejected, closed or missing bug label Dec 6, 2026
@jakebailey Jake Bailey (jakebailey) added the Domain: Performance Performance related label Dec 6, 2026
@demike

demike commented Dec 6, 2026

Copy link
Copy Markdown
Author

demike please read the following Contributor License Agreement(CLA). If you agree with the CLA, please reply with the following information.

@microsoft-github-policy-service agree [company="{your company}"]

Options:

  • (default - no company specified) I have sole ownership of intellectual property rights to my Submissions and I am not making Submissions in the course of work for my employer.
@microsoft-github-policy-service agree
  • (when company given) I am making Submissions in the course of work for my employer (or my employer has intellectual property rights in my Submissions by contract or applicable law). I have permission from my employer to make Submissions and enter into this Agreement on behalf of my employer. By signing below, the defined term “You” includes me and my employer.
@microsoft-github-policy-service agree company="Microsoft"

Contributor License Agreement

@microsoft-github-policy-service agree

@jakebailey

Copy link
Copy Markdown
Member

TypeScript Bot (@typescript-bot) perf test this

@typescript-automation

typescript-automation Bot commented Dec 6, 2026 •

Copy link
Copy Markdown
Contributor

Starting jobs; this comment will be updated as builds start and complete.

Command Status Results
perf test this ✅ Started

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

Domain: Performance Performance related For Uncommitted Bug PR for untriaged, rejected, closed or missing bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants