Unverified Commit 43dc6643 authored by Tim Neutkens's avatar Tim Neutkens Committed by GitHub

Perf: Move chunkloading instrumentation to separate function (#91382)

## What?

The chunkloading instrumentation causes a GC root because of writing to
`globalThis.__next_require__` / `globalThis.__next_chunk_load__`.
Splitting it into a separate function reduces the scope being retained
in memory.

Switch onHeaders to use for loop which is faster.
parent 58a4c7b1
...@@ -2129,6 +2129,79 @@ function ErrorApp<T>({ ...@@ -2129,6 +2129,79 @@ function ErrorApp<T>({
// eslint-disable-next-line @typescript-eslint/no-unused-vars // eslint-disable-next-line @typescript-eslint/no-unused-vars
export type BinaryStreamOf<T> = ReadableStream<Uint8Array> export type BinaryStreamOf<T> = ReadableStream<Uint8Array>
/**
* Extracted to a separate function to prevent V8 from retaining the entire
* `renderToHTMLOrFlightImpl` closure scope through globalThis.__next_require__.
* V8 shares a single Context object per scope for all closures; by creating
* these closures in their own function scope, the globalThis references only
* retain `instrumented` and `cacheComponents`, not request-specific data like
* req/res/workStore.
*/
function installGlobalModuleLoadingHandlers(
ComponentMod: AppPageModule,
cacheComponents: boolean
) {
const instrumented = wrapClientComponentLoader(ComponentMod)
// When we are prerendering if there is a cacheSignal for tracking
// cache reads we track calls to `loadChunk` and `require`. This allows us
// to treat chunk/module loading with similar semantics as cache reads to avoid
// module loading from causing a prerender to abort too early.
const shouldTrackModuleLoading = () => {
if (!cacheComponents) {
return false
}
if (process.env.__NEXT_DEV_SERVER) {
return true
}
const workUnitStore = workUnitAsyncStorage.getStore()
if (!workUnitStore) {
return false
}
switch (workUnitStore.type) {
case 'prerender':
case 'prerender-client':
case 'validation-client':
case 'prerender-runtime':
case 'cache':
case 'private-cache':
return true
case 'prerender-ppr':
case 'prerender-legacy':
case 'request':
case 'unstable-cache':
case 'generate-static-params':
return false
default:
workUnitStore satisfies never
}
}
// @ts-expect-error
globalThis.__next_require__ = (
...args: Parameters<typeof instrumented.require>
) => {
const exportsOrPromise = instrumented.require(...args)
if (shouldTrackModuleLoading()) {
trackPendingImport(exportsOrPromise)
}
return exportsOrPromise
}
// @ts-expect-error
globalThis.__next_chunk_load__ = (
...args: Parameters<typeof instrumented.loadChunk>
) => {
const loadingChunk = instrumented.loadChunk(...args)
if (shouldTrackModuleLoading()) {
trackPendingChunkLoad(loadingChunk)
}
return loadingChunk
}
}
async function renderToHTMLOrFlightImpl( async function renderToHTMLOrFlightImpl(
req: BaseNextRequest, req: BaseNextRequest,
res: BaseNextResponse, res: BaseNextResponse,
...@@ -2170,65 +2243,7 @@ async function renderToHTMLOrFlightImpl( ...@@ -2170,65 +2243,7 @@ async function renderToHTMLOrFlightImpl(
// We need to expose the bundled `require` API globally for // We need to expose the bundled `require` API globally for
// react-server-dom-webpack. This is a hack until we find a better way. // react-server-dom-webpack. This is a hack until we find a better way.
if (ComponentMod.__next_app__) { if (ComponentMod.__next_app__) {
const instrumented = wrapClientComponentLoader(ComponentMod) installGlobalModuleLoadingHandlers(ComponentMod, cacheComponents)
// When we are prerendering if there is a cacheSignal for tracking
// cache reads we track calls to `loadChunk` and `require`. This allows us
// to treat chunk/module loading with similar semantics as cache reads to avoid
// module loading from causing a prerender to abort too early.
const shouldTrackModuleLoading = () => {
if (!cacheComponents) {
return false
}
if (process.env.__NEXT_DEV_SERVER) {
return true
}
const workUnitStore = workUnitAsyncStorage.getStore()
if (!workUnitStore) {
return false
}
switch (workUnitStore.type) {
case 'prerender':
case 'prerender-client':
case 'validation-client':
case 'prerender-runtime':
case 'cache':
case 'private-cache':
return true
case 'prerender-ppr':
case 'prerender-legacy':
case 'request':
case 'unstable-cache':
case 'generate-static-params':
return false
default:
workUnitStore satisfies never
}
}
const __next_require__: typeof instrumented.require = (...args) => {
const exportsOrPromise = instrumented.require(...args)
if (shouldTrackModuleLoading()) {
// requiring an async module returns a promise.
trackPendingImport(exportsOrPromise)
}
return exportsOrPromise
}
// @ts-expect-error
globalThis.__next_require__ = __next_require__
const __next_chunk_load__: typeof instrumented.loadChunk = (...args) => {
const loadingChunk = instrumented.loadChunk(...args)
if (shouldTrackModuleLoading()) {
trackPendingChunkLoad(loadingChunk)
}
return loadingChunk
}
// @ts-expect-error
globalThis.__next_chunk_load__ = __next_chunk_load__
} }
if (process.env.__NEXT_DEV_SERVER && setIsrStatus && !cacheComponents) { if (process.env.__NEXT_DEV_SERVER && setIsrStatus && !cacheComponents) {
...@@ -3377,9 +3392,9 @@ async function renderToStream( ...@@ -3377,9 +3392,9 @@ async function renderToStream(
onError: htmlRendererErrorHandler, onError: htmlRendererErrorHandler,
nonce, nonce,
onHeaders: (headers: Headers) => { onHeaders: (headers: Headers) => {
headers.forEach((value, key) => { for (const [key, value] of headers) {
appendHeader(key, value) appendHeader(key, value)
}) }
}, },
maxHeadersLength: reactMaxHeadersLength, maxHeadersLength: reactMaxHeadersLength,
bootstrapScriptContent, bootstrapScriptContent,
......
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