Repository navigation
Conversation
The coordinator setup that Turbopack waits for read the project's sources from disk several times over: the import graph, file numbering, the shared slot reservation, the production prewarm and the module resolver each read them again. #1033 had routed these reads through plugin-utils' stamp-checked source cache, but a later squash merge (#1037) disconnected it, leaving source-cache.ts unused. The setup now runs inside one withSourceCache run, and the graph, numbering, reservation, prewarm and resolver read through readSource. Inside such a run the module resolver also looks each project file up once per importing directory and specifier, instead of again for every importer and every pass of an extraction; it still reads a file again once its stamp changes. Packages keep their existing stamp-checked resolution. Large Turbopack benchmark app, 16 cold builds each: config done -> Creating 552-564ms -> 410-422ms median; the profiled setup 478 -> 345ms (prewarm reads 37 -> 10ms, prewarm extraction 292 -> 195ms). Output is unchanged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Codecov Report✅ All modified and coverable lines are covered by tests.
🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Stacked on #1104. Under Turbopack, Next.js cannot start compiling until the coordinator setup ends. The setup runs on Next's main thread between
next.configreturning and "Creating an optimized production build". This PR makes that setup cheaper by reading each source file once.The setup used to read the project's sources from disk several times: the import graph, file numbering (
usesAny), the shared slot reservation, the production prewarm, and the module resolver that extraction calls back into. #1033 had routed these reads through plugin-utils' stamp-checked source cache (readSourceinsidewithSourceCache). The squash merge of #1037 dropped that wiring and leftsource-cache.tsunused (only its tests imported it).setupCoordinatornow runs inside onewithSourceCacherun. The graph, numbering, reservation, prewarm and resolver read throughreadSource. Outside a run,readSourceis still a plainreadFileSync, so webpack, Vite and the other plugins are unchanged.createModuleResolverlooks each project file (relative, absolute or tsconfig-alias specifier) up once per importing directory and specifier. Before, it repeated the lookup for every importer and every pass of an extraction: the large app made 2,400 resolver calls for 334 files. File contents are still stamp-checked, so a file that changes is read again. Packages keep their existing stamp-checked resolution.Results
Large Turbopack benchmark app (
benchmark/large-app.mjs, 334 files), Next 16.4.6, Windows. Cold builds alternating base/change(/Tailwind), run one at a time, 2 × 8 rounds. Base is #1104's head with the same native binding.create-dist-dirOn this machine the compile phase swings by about ±1s, so wall time cannot show a ~150ms change at this n. The setup phase is the steady metric, and it drops by about 150ms (−27%).
Setup breakdown (
DEVUP_UI_PROFILE=1, 8 rounds each):Most of the gain is in the prewarm. Its extraction calls the JS module resolver about 7 times per file, and each call used to re-stat and re-read the resolved module.
Output
Built with base and this PR's dists in the same worktree (same paths, same binding). I compared SHA-1s of
.next/static,.next/server(build ID normalized) anddf/:next-devup-ui-large-turbo: identical (677 + 5 files)next-devup-ui(--webpack): identicalnext-devup-ui-turbo,next-devup-ui-single-turbo: identical except_not-found.html/404.html. Those two flip between two hashes from run to run on base alone too, because Next orders their<title>tags nondeterministically.apps/landing(LANDING_BUILD_MODE=next): the multiset of CSS contents,devup-ui.cssand every static chunk are identical. Its file numbers (and sofileMap.json) already vary between two base builds, so a byte compare is not meaningful there.next dev(turbo app): editingbgon the page shows up in the served CSS (HMR works).Tests
bun test --coverage packages/plugin-utils packages/next-plugin: 1085 pass. Every changed file is at 100% lines/functions.Not in this PR
요약
#1104 위에 쌓인 PR입니다. Turbopack에서 Next.js는 코디네이터 설정이 끝나야 컴파일을 시작할 수 있습니다. 이 설정은
next.config반환과 "Creating an optimized production build" 사이에 Next 메인 스레드에서 실행됩니다. 이 PR은 각 소스 파일을 한 번만 읽게 해서 이 설정을 가볍게 합니다.지금까지는 import 그래프, 파일 번호 매기기(
usesAny), 공유 슬롯 예약, 프로덕션 prewarm, 추출 중 호출되는 모듈 리졸버가 같은 소스를 디스크에서 각각 다시 읽었습니다. #1033이 이 읽기들을 plugin-utils의 stamp 검사 소스 캐시(withSourceCache안의readSource)로 연결했지만, #1037의 squash merge에서 그 연결이 빠졌습니다. 그 뒤로source-cache.ts는 테스트에서만 쓰이고 있었습니다.setupCoordinator가 이제 하나의withSourceCache실행 안에서 돌고, 그래프·번호 매기기·예약·prewarm·리졸버가readSource로 읽습니다. 실행 밖의readSource는 그대로readFileSync라서 webpack, Vite 등 다른 플러그인은 바뀌지 않습니다.createModuleResolver는 프로젝트 파일(상대·절대·tsconfig alias 지정자)을 import하는 디렉터리와 지정자 조합마다 한 번만 찾습니다. 이전에는 import하는 파일마다, 추출 패스마다 다시 찾았습니다(대형 앱에서 334개 파일에 리졸버 호출 2,400번). 파일 내용은 계속 stamp로 검사하므로 바뀐 파일은 다시 읽고, 패키지는 기존 stamp 검사 방식으로 해석합니다.결과
대형 Turbopack 벤치마크 앱(334개 파일), Next 16.4.6, Windows입니다. base/변경(/Tailwind)을 번갈아 하나씩 콜드 빌드했고(2 × 8라운드), base는 같은 네이티브 바인딩을 쓴 #1104 head입니다.
create-dist-dir: 498 → 342ms.출력
같은 worktree에서 base와 이 PR의 dist로 각각 빌드해 비교했습니다.
_not-found.html/404.html만 다릅니다. 이 두 파일은 Next가<title>순서를 비결정적으로 정해서 base끼리 빌드해도 두 해시 사이를 오갑니다.devup-ui.css, 모든 static chunk가 동일합니다. 파일 번호는 base끼리 빌드해도 달라서 바이트 비교는 의미가 없습니다.next devHMR이 동작함을 확인했습니다.테스트
bun test --coverage packages/plugin-utils packages/next-plugin: 1085개 통과, 변경된 파일은 모두 100% 커버리지입니다.이 PR에 포함하지 않은 것
🤖 Generated with Claude Code