Skip to content

Faster crawl: per module export index and parsing on the prefetch workers - #94

Merged
Bobris merged 3 commits into
bobril:masterfrom
sedlakr:perf/crawl-export-index
Oct 29, 2026
Merged

Bobris merged 3 commits into
bobril:masterfrom
sedlakr:perf/crawl-export-index

Conversation

@sedlakr

@sedlakr sedlakr commented Oct 29, 2026

Copy link
Copy Markdown
Contributor

Problem

The crawl of sources (BuildModuleCtx.Crawl) is sequential, and in a cold build of a large project it is ~9.4 s of ~25-30 s. A dotnet-trace sampled-thread-time profile of phoenix nsi-bobwai (2650 sources, tsrs) shows it is CPU bound on that one thread, not waiting for tsrs (Monitor.Wait ~0.3 s, tsrs itself ~1 s). Per crawled file it does:

  • GatherBobrilSourceInfo.Gather ~3.4 s, nearly all of it ExportFinder: for each looked up export of another module (import {x} used in b.asset, b.sprite, translation parameters) ResolvingConstEvalCtx.ConstValue walks the whole module. There were 5386 lookups of 1673 distinct (module, export) pairs, e.g. the icon module with 4572 statements is walked once per icon.
  • parse + scope analysis of the transpiled JavaScript ~2 s.

Change

Two commits:

  1. Parse transpiled output on the prefetch worker threads. After the native transpile the worker parses the JavaScript and runs FigureOutScope, and stores it in the existing TranspileResult.Ast, which Transpile(info) already uses when set. On any failure Ast stays null and the crawl parses again and reports the error exactly as before.
  2. ExportIndex. One pass over a module records everything ExportFinder reacts to in source order (exports.x = ... assignments, __export/__exportStar calls, module.exports = ...), with the same StopDescending rules. Find(export, ctx) replays the entries of that export plus the ones which can set any export, in source order, so the result is the same and re-exported modules are evaluated through ctx in the same order and at the same moments (ResolveAndLoad has side effects, e.g. ReportTranspilationDependency, so I did not cache lookup results). The index is kept per parsed module in a ConditionalWeakTable. ExportFinder itself stays as it was (Result made public so a test can compare).

Results

phoenix nsi-bobwai, bb b --nocache, Windows, ReadyToRun publish of 7.1.0 with and without the change:

before after
crawl thread (trace) 9.4 / 9.4 s 6.6 / 6.4 s
Gather 3.5 / 3.4 s 0.7 / 0.7 s
whole traced run 29.4 s 25.8 s
Build done, 4 alternating untraced pairs 33.6 / 26.3 / 25.6 / 25.8 s 27.8 / 27.3 / 24.5 / 24.5 s

The untraced build times are noisy (about +-2 s on this machine), the profile numbers are the more reliable ones. Output directory is byte identical (diff -rq).

  • Njsast.Test ConstEval, SourceInfo and Bundler tests pass, Lib.Test 209 of 209.
  • New ExportIndexTest compares ExportIndex.Find with ExportFinder on 12 module shapes x 7 export names (void 0 chains, module.exports, shadowed exports, __export/__exportStar incl. chained and missing modules, nested functions, ordering of overrides). I checked that it fails when the merge order is broken.

Please review

  • The index mirrors ExportFinder.Visit step by step; a review of whether any case differs would be appreciated. The one deliberate equivalence: a non string AstDot.Property made the old walk throw (caught, lookup answered with null), the index marks the module and throws on lookup too.
  • Remaining on the crawl thread after this: File.ReadAllBytes ~2.3 s (source read in TryGetSourceToPrefetch, which also has to decide about the build cache), resolving imports ~1.3 s. Not included.
  • The CHANGELOG entry sits next to the one from the server GC PR (Use server GC and turn off dynamic PGO in released binaries #93) under [unreleased], whichever is merged second needs a trivial rebase.

🤖 Generated with Claude Code

sedlakr and others added 3 commits October 29, 2026 09:01
Parsing and scope analysis of the transpiled JavaScript is a pure function of its text, so it runs
after the native transpile on the worker instead of on the sequential crawl.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
ExportFinder walked the whole module for each looked up export (5386 lookups of 1673 distinct exports in a
2650 source project, ~3 s on the crawl thread). ExportIndex records everything the walk reacts to in one pass and
answers each lookup from the entries of that export and the ones which can set any export, in source order, so
the result and the evaluation of re-exported modules are the same.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>

@Bobris Bobris left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed the export-index traversal/replay ordering, constant-evaluation and dependency-resolution paths, AST cache lifetime, and native prefetch parsing/scope analysis. No blocking findings. The merge from master preserves both changelog entries and introduces no further implementation changes beyond the already reviewed runtime configuration from #93.

Local validation: all 1,637 Njsast tests and all 214 Lib tests passed (with TSRS_BINARY configured), and dotnet build Bbcore.Lib/Bbcore.Lib.csproj --no-restore succeeded without warnings. An additional native-tsrs build of 84 sources covering chained re-exports and an imported asset produced byte-identical output, including source maps, against the pre-change implementation; the combined changes from #93 and #94 passed the same comparison. CI is green on Linux, Windows and macOS for this commit.

@Bobris
Bobris merged commit 1fc3e1d into bobril:master Oct 29, 2026
3 checks passed
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.

2 participants