Unverified Commit 8c96a740 authored by Tobias Koppers's avatar Tobias Koppers Committed by GitHub

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: 's avatarvercel-fleet-prod[bot] <318278635+vercel-fleet-prod[bot]@users.noreply.github.com>
Co-authored-by: 's avatarTobias Koppers <1365881+sokra@users.noreply.github.com>
parent 502b6b7d
...@@ -2468,6 +2468,23 @@ export default async function build( ...@@ -2468,6 +2468,23 @@ export default async function build(
} }
} }
// Reverse index of `appPathRoutes`: normalized pathname -> original app
// paths that have an entry in `mappedAppPages`. Built once up front so
// the per-page check below doesn't scan every route for every page.
const originalAppPathsByPage = new Map<string, string[]>()
if (mappedAppPages) {
for (const originalPath in appPathRoutes) {
if (!mappedAppPages[originalPath]) continue
const normalizedPath = appPathRoutes[originalPath]
const originalAppPaths = originalAppPathsByPage.get(normalizedPath)
if (originalAppPaths) {
originalAppPaths.push(originalPath)
} else {
originalAppPathsByPage.set(normalizedPath, [originalPath])
}
}
}
await Promise.all( await Promise.all(
Object.entries(pageKeys) Object.entries(pageKeys)
.reduce<Array<{ pageType: keyof typeof pageKeys; page: string }>>( .reduce<Array<{ pageType: keyof typeof pageKeys; page: string }>>(
...@@ -2513,19 +2530,9 @@ export default async function build( ...@@ -2513,19 +2530,9 @@ export default async function build(
let originalAppPath: string | undefined let originalAppPath: string | undefined
if (pageType === 'app' && mappedAppPages) { if (pageType === 'app' && mappedAppPages) {
const originalAppPaths: string[] = [] const originalAppPaths = originalAppPathsByPage.get(page)
for (const [originalPath, normalizedPath] of Object.entries(
appPathRoutes
)) {
if (
normalizedPath === page &&
mappedAppPages[originalPath]
) {
originalAppPaths.push(originalPath)
}
}
if (originalAppPaths.length > 0) { if (originalAppPaths) {
originalAppPath = selectAppPageEntry(page, originalAppPaths) originalAppPath = selectAppPageEntry(page, originalAppPaths)
pagePath = mappedAppPages[originalAppPath].replace( pagePath = mappedAppPages[originalAppPath].replace(
/^private-next-app-dir/, /^private-next-app-dir/,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment