Unverified Commit a2536e65 authored by Tobias Koppers's avatar Tobias Koppers Committed by GitHub

perf(build): lazily require page-data helpers in build/utils (#100822)

### What?

Defer the requires in `packages/next/src/build/utils.ts` that are only
needed while collecting page data, so the main `next build` process no
longer evaluates them at startup. The deferred modules are the edge
sandbox, `load-components`, the incremental cache helper, app segment
collection, the app/pages static-path builders and the HTTP agent setup.

### Why?

`build/utils` is loaded eagerly by the main process, through
`build/index` and through route discovery → `get-page-static-info`. Its
top-level imports pulled in large dependency graphs, including
`compiled/edge-runtime`, the incremental cache / response cache, the
tracer and OpenTelemetry. Those are only used by `isPageStatic` and the
component-loading helpers, which run in the build workers. The main
process paid for evaluating them before `build()` started, then never
used them.

### How?

- Each of those dependencies is now `require`d at its call site, keeping
the original call order. The types still come from `import type`.
- The side-effect imports at the top of the file (`require-hook`,
`node-polyfill-crypto`, `node-environment`) are untouched, so
initialization order is the same in the main process and in workers.
- After the change, the deferred modules are never loaded in the main
process for the whole build. The work is removed, not moved inside
`build()`.

### Measurements

These are local measurements on one machine, alternating base and change
runs. `bench/nested-deps-app-router-many-pages` has 1,000 pages and uses
Turbopack. The metric is the time from profiler start to the first
sample inside `build()`, taken from `next build
--experimental-cpu-prof`.

| Pair | Before | After |
|---|---:|---:|
| 1 | 244.7ms | 200.5ms |
| 2 | 243.4ms | 184.4ms |
| 3 | 255.6ms | 184.6ms |

- **Mean:** 247.9 → 189.8ms (−23%).
- **Total build time:** unchanged (~52–55s).
- **`cli/next-build.js` require time:** median 251.8 → 207.3ms in a
fresh process (30 interleaved samples each).
- **Main-process modules, whole build of a small mixed app:**
  - Turbopack: 424 → 348 `next/dist` modules.
  - Webpack: 551 → 476 `next/dist` modules.

### Verification

- **Smoke builds:** a small app with app router, edge, SSG, pages
`getStaticProps`/`getStaticPaths` and middleware produces an identical
route table before and after, with both Turbopack and webpack.
- `pnpm test-start-turbo test/production/app-dir-edge-runtime-with-wasm
test/production/conflicting-ssg-paths`
- `pnpm test-start-webpack test/e2e/app-dir/app/standalone.test.ts
test/production/build-output`
- `packages/next/src/build/analysis/get-page-static-info.test.ts` unit
tests
- **Not run:** a webpack build of the 1,000-page benchmark. It fails
during compilation with a native `std::bad_alloc` on the base commit as
well, so it is unrelated to this change.

<!-- NEXT_JS_LLM -->


<!-- fleet 60ff1dd9-ad2b-411e-8a67-e3842d2a1798 -->
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 ebcc2840
......@@ -52,12 +52,9 @@ import { findPageFile } from '../server/lib/find-page-file'
import { isEdgeRuntime } from '../lib/is-edge-runtime'
import * as Log from './output/log'
import type { LoadComponentsReturnType } from '../server/load-components'
import { loadComponents } from '../server/load-components'
import { trace } from '../trace'
import { setHttpClientAndAgentOptions } from '../server/setup-http-agent-env'
import { Sema } from 'next/dist/compiled/async-sema'
import { normalizePagePath } from '../shared/lib/page-path/normalize-page-path'
import { getRuntimeContext } from '../server/web/sandbox'
import { RouteKind } from '../server/route-kind'
import type { PageExtensions } from './page-extensions-type'
import type { FallbackMode } from '../lib/fallback'
......@@ -67,11 +64,8 @@ import type {
AppSegment,
ParamMatching,
} from './segment-config/app/app-segments'
import { collectSegments } from './segment-config/app/app-segments'
import { createIncrementalCache } from '../export/helpers/create-incremental-cache'
import type { collectSegments } from './segment-config/app/app-segments'
import { collectRootParamKeys } from './segment-config/app/collect-root-param-keys'
import { buildAppStaticPaths } from './static-paths/app'
import { buildPagesStaticPaths } from './static-paths/pages'
import type {
PrerenderRouteMatcher,
PrerenderedRoute,
......@@ -94,6 +88,17 @@ import { isStaticMetadataFile } from '../lib/metadata/is-metadata-route'
import { normalizeAppPath } from '../shared/lib/router/utils/app-paths'
import { mapNftFileEntries, type NftJson, resolveNftOutputPath } from './nft'
// Modules only needed when collecting page data / static paths (which runs in
// build workers) are required lazily, so the main `next build` process doesn't
// pay for evaluating them (and their dependency graphs) at startup.
function loadComponents(
...args: Parameters<typeof import('../server/load-components').loadComponents>
) {
return (
require('../server/load-components') as typeof import('../server/load-components')
).loadComponents(...args)
}
/**
* Get the display path for build output. For static metadata files under
* dynamic routes, this normalizes the path to use "-" placeholder.
......@@ -897,6 +902,8 @@ export async function isPageStatic({
}
}
const { createIncrementalCache } =
require('../export/helpers/create-incremental-cache') as typeof import('../export/helpers/create-incremental-cache')
await createIncrementalCache({
cacheHandler,
cacheHandlers,
......@@ -909,6 +916,8 @@ export async function isPageStatic({
const isPageStaticSpan = trace('is-page-static-utils', parentId)
return isPageStaticSpan
.traceAsyncFn(async (): Promise<PageIsStaticResult> => {
const { setHttpClientAndAgentOptions } =
require('../server/setup-http-agent-env') as typeof import('../server/setup-http-agent-env')
setHttpClientAndAgentOptions({
httpAgentOptions,
})
......@@ -923,6 +932,8 @@ export async function isPageStatic({
const pathIsEdgeRuntime = isEdgeRuntime(pageRuntime)
if (pathIsEdgeRuntime) {
const { getRuntimeContext } =
require('../server/web/sandbox') as typeof import('../server/web/sandbox')
const runtime = await getRuntimeContext({
paths: edgeInfo.files.map((file: string) => path.join(distDir, file)),
edgeFunctionEntry: {
......@@ -982,7 +993,9 @@ export async function isPageStatic({
let collectedSegments: Awaited<ReturnType<typeof collectSegments>>
try {
collectedSegments = await collectSegments(
const { collectSegments: collectAppSegments } =
require('./segment-config/app/app-segments') as typeof import('./segment-config/app/app-segments')
collectedSegments = await collectAppSegments(
// We know this is an app page or app route module because we
// checked above that the page type is 'app'.
routeModule as AppPageRouteModule | AppRouteRouteModule,
......@@ -1048,7 +1061,9 @@ export async function isPageStatic({
prerenderRouteMatchers,
paramMatching,
fallbackMode: prerenderFallbackMode,
} = await buildAppStaticPaths({
} = await (
require('./static-paths/app') as typeof import('./static-paths/app')
).buildAppStaticPaths({
dir,
page,
route,
......@@ -1117,14 +1132,15 @@ export async function isPageStatic({
}
if (hasStaticProps && hasStaticPaths) {
;({ prerenderedRoutes, fallbackMode: prerenderFallbackMode } =
await buildPagesStaticPaths({
page,
locales,
defaultLocale,
configFileName,
getStaticPaths: componentsResult.getStaticPaths!,
}))
;({ prerenderedRoutes, fallbackMode: prerenderFallbackMode } = await (
require('./static-paths/pages') as typeof import('./static-paths/pages')
).buildPagesStaticPaths({
page,
locales,
defaultLocale,
configFileName,
getStaticPaths: componentsResult.getStaticPaths!,
}))
}
const isNextImageImported = (globalThis as any).__NEXT_IMAGE_IMPORTED
......
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