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