Skip to content

WIP: fix: avoid repeated Acorn location scans - #18586

Draft
jschaf wants to merge 1 commit into
sveltejs:mainfrom
jschaf:joe/x/compiler-bottleneck-investigation
Draft

WIP: fix: avoid repeated Acorn location scans#18586
jschaf wants to merge 1 commit into
sveltejs:mainfrom
jschaf:joe/x/compiler-bottleneck-investigation

Conversation

@jschaf

@jschaf jschaf commented Jul 24, 2026

Copy link
Copy Markdown
Contributor

NOTE: not ready for review yet. I'm using this to explore whether we should adjust Acorn's API in acornjs/acorn#1448.

Svelte parses JavaScript embedded in component markup by repeatedly starting Acorn at different offsets in the same source string. Acorn initializes each parser by scanning the source prefix to recover the starting line, so components with many expression or declaration tags accumulate approximately quadratic parsing work.

This is the behavior described upstream in acornjs/acorn#1448.

  • Reuse Svelte's existing source locator to initialize Acorn's line and token position when parsing directly from the component source.
  • Keep Acorn's existing constructor path for adjusted source strings used by destructured contexts and TypeScript annotations.
  • Fall back to Acorn's existing path for bare CR, U+2028, and U+2029 line separators, whose location semantics differ from Svelte's locator.
  • Add regression coverage for multiline expressions, declaration tags, LF, CRLF, bare CR, and Unicode line separators.
  • Add a patch changeset for svelte.

This preserves absolute offsets, AST locations, comments, diagnostics, and source-map behavior while avoiding the repeated prefix scan for normal LF/CRLF component sources.

Performance

The patch shows 5-9% faster full compilation with no output differences for Immich, Open WebUI, and Skeleton.

Project Corpus Mode Before After Median improvement
Immich 411 files / 1.40 MB Parse 263.5 ms 243.5 ms 7.7% faster
Immich 411 files / 1.40 MB Client compile 758.9 ms 727.3 ms 4.7% faster
Immich 411 files / 1.40 MB Server compile 691.4 ms 644.2 ms 7.3% faster
Open WebUI 593 files / 3.36 MB Parse 627.3 ms 492.3 ms 19.0% faster
Open WebUI 592 files / 3.36 MB Client compile 1,751.3 ms 1,621.9 ms 8.4% faster
Open WebUI 592 files / 3.36 MB Server compile 1,580.8 ms 1,450.3 ms 9.4% faster
Skeleton 686 files / 0.83 MB Parse 141.1 ms 135.5 ms 5.0% faster
Skeleton 686 files / 0.83 MB Client compile 429.8 ms 424.3 ms 2.3% faster
Skeleton 686 files / 0.83 MB Server compile 378.3 ms 366.9 ms 3.0% faster

Large individual components benefited substantially:

  • Open WebUI MessageInput.svelte: 44% faster parsing
  • Open WebUI Documents.svelte: 52% faster
  • Immich album page: 25% faster
  • Immich MemoryViewer.svelte: 27% faster
  • Skeleton typography controls: 34% faster

Measured on macOS arm64 with Node 26.3.1 and pnpm 10.4.0. Each result is the median of 9 measurements after 3 warmups.

Synthetic benchmark to show quadratic behavior
Expression tags Parse before Parse after Doubling ratio before Doubling ratio after
128 0.758 ms 0.646 ms
256 1.633 ms 1.102 ms 2.16× 1.71×
512 4.132 ms 2.136 ms 2.53× 1.94×
1,024 12.534 ms 3.885 ms 3.03× 1.82×
2,048 41.135 ms 6.695 ms 3.28× 1.72×
4,096 148.524 ms 11.479 ms 3.61× 1.71×

The fitted parse-time exponent drops from 1.53 to 0.84. At 4,096 expression tags, the parse phase is 12.9× faster.

@pkg-svelte-dev

Copy link
Copy Markdown

Install the latest version of svelte from d7acfb0:

pnpm add https://pkg.svelte.dev/svelte/c/d7acfb026d62655b59fffca01ef0ac8854fbe0dc

Open in pkg.svelte.dev: https://pkg.svelte.dev/repos/svelte/pr/18586

Note

This PR is from a fork. A maintainer must approve approve each commit before it can be built and installed.

@changeset-bot

changeset-bot Bot commented Jul 24, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: d7acfb0

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
svelte Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@jschaf jschaf changed the title fix: avoid repeated Acorn location scans WIP: fix: avoid repeated Acorn location scans Jul 24, 2026
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