Repository navigation
Faster crawl: per module export index and parsing on the prefetch workers - #94
Conversation
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
left a comment
There was a problem hiding this comment.
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.
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. Adotnet-tracesampled-thread-time profile of phoenixnsi-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 itExportFinder: for each looked up export of another module (import {x}used inb.asset,b.sprite, translation parameters)ResolvingConstEvalCtx.ConstValuewalks 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.Change
Two commits:
FigureOutScope, and stores it in the existingTranspileResult.Ast, whichTranspile(info)already uses when set. On any failureAststaysnulland the crawl parses again and reports the error exactly as before.ExportIndex. One pass over a module records everythingExportFinderreacts to in source order (exports.x = ...assignments,__export/__exportStarcalls,module.exports = ...), with the sameStopDescendingrules.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 throughctxin the same order and at the same moments (ResolveAndLoadhas side effects, e.g.ReportTranspilationDependency, so I did not cache lookup results). The index is kept per parsed module in aConditionalWeakTable.ExportFinderitself stays as it was (Resultmade 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:GatherBuild done, 4 alternating untraced pairsThe 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.TestConstEval, SourceInfo and Bundler tests pass,Lib.Test209 of 209.ExportIndexTestcomparesExportIndex.FindwithExportFinderon 12 module shapes x 7 export names (void 0 chains,module.exports, shadowedexports,__export/__exportStarincl. chained and missing modules, nested functions, ordering of overrides). I checked that it fails when the merge order is broken.Please review
ExportFinder.Visitstep by step; a review of whether any case differs would be appreciated. The one deliberate equivalence: a non stringAstDot.Propertymade the old walk throw (caught, lookup answered withnull), the index marks the module and throws on lookup too.File.ReadAllBytes~2.3 s (source read inTryGetSourceToPrefetch, which also has to decide about the build cache), resolving imports ~1.3 s. Not included.[unreleased], whichever is merged second needs a trivial rebase.🤖 Generated with Claude Code