Unverified Commit 93a50607 authored by Aurora Scharff's avatar Aurora Scharff Committed by GitHub

perf: reduce build tree view allocations (#99930)

## Summary

`printTreeView` copied generated-route lists several times and created a
temporary object for every route before printing a small preview in the
build output.

This change writes the preview directly to the output table. When routes
need to be reordered, it sorts one list of numeric indexes instead of
copying the routes into objects.

The build output does not change. The function creates less temporary
data and puts less pressure on garbage collection during builds with
many generated routes.

## Impact

In a focused A/B of the compiled `printTreeView` with 50,000 generated
routes, median temporary heap growth per call dropped:

- from 460 KB to 68 KB when preserving route order (-85%)
- from 4.53 MB to 2.14 MB when ordering slow routes first (-53%)

These results were repeated across five fresh Node.js processes with 50
samples per process. This is a memory-allocation improvement; no
CPU-time improvement is claimed.

## Verification

- `pnpm build-all`
- `HEADLESS=true pnpm test-start-turbo
test/e2e/app-dir/build-output-truncated-generated-paths/build-output-truncated-generated-paths.test.ts`
- `HEADLESS=true pnpm test-start-webpack
test/e2e/app-dir/build-output-truncated-generated-paths/build-output-truncated-generated-paths.test.ts`

<!-- NEXT_JS_LLM -->
parent 1720b9bb
...@@ -440,108 +440,122 @@ export async function printTreeView( ...@@ -440,108 +440,122 @@ export async function printTreeView(
]) ])
if (pageInfo?.ssgPageRoutes?.length) { if (pageInfo?.ssgPageRoutes?.length) {
const totalRoutes = pageInfo.ssgPageRoutes.length const ssgPageRoutes = pageInfo.ssgPageRoutes
const totalRoutes = ssgPageRoutes.length
const contSymbol = i === arr.length - 1 ? ' ' : '│' const contSymbol = i === arr.length - 1 ? ' ' : '│'
const routeDurations = pageInfo.ssgPageDurations
let hasDuration = false
if (routeDurations) {
for (let index = 0; index < routeDurations.length; index++) {
if (routeDurations[index] > MIN_DURATION) {
hasDuration = true
break
}
}
}
const getRouteSymbol = (route: string) => const maxPreviewPages = hasDuration ? 7 : 3
getTreeViewSymbol(route, pageInfos.get(route) ?? pageInfo) const previewPages =
const getSharedRouteSymbol = (routes: string[]) => { totalRoutes === maxPreviewPages + 1
const firstSymbol = routes[0] ? getRouteSymbol(routes[0]) : undefined ? totalRoutes
: Math.min(totalRoutes, maxPreviewPages)
let routeIndexes: number[] | undefined
if (hasDuration) {
// Keep the source arrays immutable. Sorting only their indexes avoids
// allocating a route object for every generated path.
routeIndexes = []
for (let index = 0; index < totalRoutes; index++) {
routeIndexes.push(index)
}
routeIndexes.sort((indexA, indexB) => {
const durationA = routeDurations![indexA] || 0
const durationB = routeDurations![indexB] || 0
return routes.every((route) => getRouteSymbol(route) === firstSymbol) // Keep too small durations in their original order at the end.
? firstSymbol return durationA <= MIN_DURATION && durationB <= MIN_DURATION
: undefined ? 0
: durationB - durationA
})
} }
let routes: { const hasSummary = totalRoutes > previewPages
route: string let summarySymbol: string | undefined
duration: number let avgDuration: number | undefined
avgDuration?: number
symbol?: string if (hasSummary) {
}[] const firstHiddenIndex = routeIndexes?.[previewPages] ?? previewPages
if (pageInfo.ssgPageDurations?.some((d) => d > MIN_DURATION)) { const firstHiddenRoute = ssgPageRoutes[firstHiddenIndex]
const previewPages = totalRoutes === 8 ? 8 : Math.min(totalRoutes, 7) summarySymbol = getTreeViewSymbol(
const routesWithDuration = pageInfo.ssgPageRoutes firstHiddenRoute,
.map((route, idx) => ({ pageInfos.get(firstHiddenRoute) ?? pageInfo
route, )
duration: pageInfo.ssgPageDurations![idx] || 0,
})) let hiddenTotalDuration = 0
.sort(({ duration: a }, { duration: b }) => for (let index = previewPages; index < totalRoutes; index++) {
// Sort by duration const routeIndex = routeIndexes?.[index] ?? index
// keep too small durations in original order at the end const route = ssgPageRoutes[routeIndex]
a <= MIN_DURATION && b <= MIN_DURATION ? 0 : b - a
) if (
routes = routesWithDuration.slice(0, previewPages) index > previewPages &&
const remainingRoutes = routesWithDuration.slice(previewPages) summarySymbol !== undefined &&
if (remainingRoutes.length) { getTreeViewSymbol(route, pageInfos.get(route) ?? pageInfo) !==
const remaining = remainingRoutes.length summarySymbol
const avgDuration = Math.round( ) {
remainingRoutes.reduce( summarySymbol = undefined
(total, { duration }) => total + duration, }
0
) / remainingRoutes.length if (hasDuration) {
) hiddenTotalDuration += routeDurations![routeIndex] || 0
routes.push({ }
route: `[+${remaining} more paths]`,
duration: 0,
avgDuration,
symbol: getSharedRouteSymbol(
remainingRoutes.map(({ route }) => route)
),
})
} }
} else {
const previewPages = totalRoutes === 4 ? 4 : Math.min(totalRoutes, 3) if (hasDuration) {
routes = pageInfo.ssgPageRoutes avgDuration = Math.round(
.slice(0, previewPages) hiddenTotalDuration / (totalRoutes - previewPages)
.map((route) => ({ route, duration: 0 })) )
if (totalRoutes > previewPages) {
const remaining = totalRoutes - previewPages
routes.push({
route: `[+${remaining} more paths]`,
duration: 0,
symbol: getSharedRouteSymbol(
pageInfo.ssgPageRoutes.slice(previewPages)
),
})
} }
} }
routes.forEach( // Emit the preview and optional summary directly into the table instead
( // of allocating intermediate route arrays.
{ route, duration, avgDuration, symbol: sharedSymbol }, const displayedRoutes = previewPages + (hasSummary ? 1 : 0)
index, for (let index = 0; index < displayedRoutes; index++) {
{ length } const isSummary = index === previewPages
) => { const routeIndex = routeIndexes?.[index] ?? index
const innerSymbol = index === length - 1 ? '└' : '├' const route = isSummary
// Generated child paths can have more precise metadata than the ? `[+${totalRoutes - previewPages} more paths]`
// parent route pattern. A summary row can use that metadata only : ssgPageRoutes[routeIndex]
// when all of the collapsed paths have the same symbol. const duration = isSummary
const routeSymbol = sharedSymbol ?? getRouteSymbol(route) ? 0
usedSymbols.add(routeSymbol) : hasDuration
? routeDurations![routeIndex] || 0
const initialCacheControl = : 0
pageInfos.get(route)?.initialCacheControl const routeSymbol =
(isSummary ? summarySymbol : undefined) ??
messages.push([ getTreeViewSymbol(route, pageInfos.get(route) ?? pageInfo)
`${contSymbol} ${innerSymbol} ${routeSymbol} ${route}${ const innerSymbol = index === displayedRoutes - 1 ? '└' : '├'
duration > MIN_DURATION const initialCacheControl = pageInfos.get(route)?.initialCacheControl
? ` (${getPrettyDuration(duration)})`
: '' usedSymbols.add(routeSymbol)
}${ messages.push([
avgDuration && avgDuration > MIN_DURATION `${contSymbol} ${innerSymbol} ${routeSymbol} ${route}${
? ` (avg ${getPrettyDuration(avgDuration)})` duration > MIN_DURATION ? ` (${getPrettyDuration(duration)})` : ''
: '' }${
}`, isSummary && avgDuration && avgDuration > MIN_DURATION
showRevalidate && initialCacheControl ? ` (avg ${getPrettyDuration(avgDuration)})`
? formatRevalidate(initialCacheControl) : ''
: '', }`,
showExpire && initialCacheControl showRevalidate && initialCacheControl
? formatExpire(initialCacheControl) ? formatRevalidate(initialCacheControl)
: '', : '',
]) showExpire && initialCacheControl
} ? formatExpire(initialCacheControl)
) : '',
])
}
} }
}) })
} }
......
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