perf(build): avoid O(pages²) original app path scan in static check (#100615)
### What? Removes a quadratic scan from the `static-check` phase of `next build`. The original app paths for each App Router page are now looked up from an index that is built once, instead of being rediscovered by scanning every route for every page. ### Why? Inside the per-page `check-page` work, each app page walked all of `appPathRoutes` (allocating a fresh entries array each time) to find the original app paths that normalize to it. That is O(pages²), and it runs synchronously on the main thread before `isPageStatic` is dispatched, so build workers sit idle while it runs. On apps with thousands of routes this was a significant share of `static-check`. ### How? Before the pages are fanned out, the build makes one pass over `appPathRoutes` and groups original app paths by their normalized pathname. It applies the same `mappedAppPages` filter the old per-page loop used, and preserves the same per-group order. Each page then does a single lookup and hands the result to the existing `selectAppPageEntry`, which picks the winning entry exactly as before. Pages with no matching entry keep the previous behavior. Pages Router handling is unchanged. Cost goes from O(pages × routes) to O(routes) plus one map lookup per page. ### Verification `bench/nested-deps-app-router-many-pages` (3,000 generated pages, Turbopack, 8 cores). Compiled once, then ran `next build --experimental-build-mode=generate` 3× before and 3× after the change, each from the same compiled output: | | Run 1 | Run 2 | Run 3 | Mean | | --- | ---: | ---: | ---: | ---: | | `static-check` before | 4250 ms | 4228 ms | 4371 ms | 4283 ms | | `static-check` after | 2093 ms | 1907 ms | 1905 ms | 1968 ms | - Output is unchanged. `app-path-routes-manifest.json`, `routes-manifest.json`, `server/app-paths-manifest.json` and the printed route table are byte-identical. The `routes`, `dynamicRoutes` and `notFoundRoutes` in `prerender-manifest.json` are identical too. - `pnpm test-start-turbo test/e2e/app-dir/parallel-routes-and-interception/` passes (55/55). - Not measured: end-to-end wall time before the change, so the numbers above only cover `static-check`. <!-- NEXT_JS_LLM --> <!-- fleet 0150f4df-aec6-47ff-bac6-ad508f1b4900 --> Co-authored-by:vercel-fleet-prod[bot] <318278635+vercel-fleet-prod[bot]@users.noreply.github.com> Co-authored-by:
Tobias Koppers <1365881+sokra@users.noreply.github.com>
Showing
Please
register
or
sign in
to comment