Unverified Commit a64db6f7 authored by Janka Uryga's avatar Janka Uryga Committed by GitHub

refactor: process streaming prerenders more efficiently (#94764)

Introducing a brand new concept: processing a stream by having a
callback invoked whenever a chunk is emitted. No more tee()ing streams
just to run two loops over the chunks from a render. Turns out node
streams had it right all along

This lets us simplify the (stream) runtime prerender and static
prerender codepaths, and will be used further in #94738, where we need
to run a staged render with some special accumulation conditions on the
chunks. This now becomes easy to add, because we can just pick what we
do in our `collectChunk` callback instead of writing an entire loop

It will also let us move to switch these codepaths to node streams
easier -- the iteration code is the only part that depends on the stream
being web or node, the collection logic is generic

follow up work: refactor `accumulateStreamChunksInto` and
`countStageBytesUntilAbortNode` in the same way. I didn't do that here
yet to keep the change focused on what upstack needs
parent 5d694858
...@@ -1735,6 +1735,16 @@ async function finalRuntimeServerPrerender( ...@@ -1735,6 +1735,16 @@ async function finalRuntimeServerPrerender(
mode.type === 'session-shell-only' mode.type === 'session-shell-only'
? createStageByteLengths() ? createStageByteLengths()
: null : null
const collectChunk = (chunk: Uint8Array) => {
collectPrerenderChunk(collectedChunks, finalServerController.signal, chunk)
if (stageByteLengths) {
increaseChunkByteLengths(
stageByteLengths,
finalStageController.currentStage,
chunk.byteLength
)
}
}
await runInSequentialTasks( await runInSequentialTasks(
async () => { async () => {
...@@ -1754,24 +1764,13 @@ async function finalRuntimeServerPrerender( ...@@ -1754,24 +1764,13 @@ async function finalRuntimeServerPrerender(
} }
) )
if (stageByteLengths) {
let countStream: typeof stream
;[stream, countStream] = stream.tee()
void countStageBytesUntilAbortWeb(
stageByteLengths,
countStream,
finalStageController,
finalServerController.signal
).catch(() => {})
}
// Note: this await will only resolve after the last task (unless sync IO aborts the render earlier) // Note: this await will only resolve after the last task (unless sync IO aborts the render earlier)
// We await it here so that if the stream errors, it's not an unhandled rejection. // We await it here so that if the stream errors, it's not an unhandled rejection.
await collectPrerenderChunksWeb( await iterateStreamingPrerenderChunksWeb(
stream, stream,
collectedChunks, finalServerController.signal,
streamState, collectChunk,
finalServerController.signal streamState
) )
}, },
() => { () => {
...@@ -4982,11 +4981,11 @@ async function accumulateStreamChunksInto( ...@@ -4982,11 +4981,11 @@ async function accumulateStreamChunksInto(
try { try {
while (!cancelled) { while (!cancelled) {
const { done, value } = await reader.read() const { done, value } = await reader.read()
if (done) { if (done || cancelled) {
cancel() cancel()
break break
} }
accumulateChunk(stageController.currentStage, accumulator, value) collectStageChunk(accumulator, stageController.currentStage, value)
} }
} catch (err) { } catch (err) {
// When we cancel the reader we may reject the read. // When we cancel the reader we may reject the read.
...@@ -5013,7 +5012,7 @@ async function accumulateStreamChunksInto( ...@@ -5013,7 +5012,7 @@ async function accumulateStreamChunksInto(
try { try {
for await (const value of nodeStream) { for await (const value of nodeStream) {
if (cancelled) break if (cancelled) break
accumulateChunk(stageController.currentStage, accumulator, value) collectStageChunk(accumulator, stageController.currentStage, value)
} }
} catch (err) { } catch (err) {
if (!cancelled) { if (!cancelled) {
...@@ -5023,9 +5022,9 @@ async function accumulateStreamChunksInto( ...@@ -5023,9 +5022,9 @@ async function accumulateStreamChunksInto(
} }
} }
function accumulateChunk( function collectStageChunk(
stage: RenderStage,
accumulator: AccumulatedStreamChunks, accumulator: AccumulatedStreamChunks,
stage: RenderStage,
value: Uint8Array value: Uint8Array
): void { ): void {
switch (stage) { switch (stage) {
...@@ -5090,30 +5089,6 @@ function createStageByteLengths(): StageByteLengths { ...@@ -5090,30 +5089,6 @@ function createStageByteLengths(): StageByteLengths {
return result as StageByteLengths return result as StageByteLengths
} }
async function countStageBytesUntilAbortWeb(
byteLengths: StageByteLengths,
stream: ReadableStream<Uint8Array>,
stageController: StagedRenderingController,
abortSignal: AbortSignal
): Promise<void> {
const reader = stream.getReader()
abortSignal.addEventListener('abort', reader.cancel.bind(reader), {
once: true,
})
while (true) {
const { done, value } = await reader.read()
if (done || abortSignal.aborted) {
break
}
increaseChunkByteLengths(
byteLengths,
stageController.currentStage,
value.byteLength
)
}
}
async function countStageBytesUntilAbortNode( async function countStageBytesUntilAbortNode(
byteLengths: StageByteLengths, byteLengths: StageByteLengths,
stream: Readable, stream: Readable,
...@@ -7582,7 +7557,24 @@ async function prerenderToStream( ...@@ -7582,7 +7557,24 @@ async function prerenderToStream(
const streamState = createStreamPendingState() const streamState = createStreamPendingState()
const collectedChunks = createPrerenderChunksAccumulator() const collectedChunks = createPrerenderChunksAccumulator()
const collectedChunksByStage = createStageChunksAccumulator() const collectedChunksByStage = appShells
? createStageChunksAccumulator()
: null
const collectChunk = (chunk: Uint8Array) => {
collectPrerenderChunk(
collectedChunks,
finalServerReactController.signal,
chunk
)
if (collectedChunksByStage) {
collectStageChunk(
collectedChunksByStage,
finalStageController.currentStage,
chunk
)
}
}
let debugEndTime: number | undefined = undefined let debugEndTime: number | undefined = undefined
let didLinkDataUnblockNewContent = false let didLinkDataUnblockNewContent = false
...@@ -7628,25 +7620,13 @@ async function prerenderToStream( ...@@ -7628,25 +7620,13 @@ async function prerenderToStream(
{ once: true } { once: true }
) )
if (appShells) {
let teedStream: typeof stream
;[stream, teedStream] = stream.tee()
void accumulateStreamChunksInto(
collectedChunksByStage,
teedStream,
finalStageController,
finalServerRenderController.signal
).catch(() => {})
}
// Note: this await will only resolve after the last task (unless sync IO aborts the render earlier) // Note: this await will only resolve after the last task (unless sync IO aborts the render earlier)
// We await it here so that if the stream errors, it's not an unhandled rejection. // We await it here so that if the stream errors, it's not an unhandled rejection.
await collectPrerenderChunksWeb( await iterateStreamingPrerenderChunksWeb(
stream, stream,
collectedChunks, finalServerReactController.signal,
streamState, collectChunk,
finalServerReactController.signal streamState
) )
}, },
() => { () => {
...@@ -7675,9 +7655,11 @@ async function prerenderToStream( ...@@ -7675,9 +7655,11 @@ async function prerenderToStream(
// then the prerender uses link data. // then the prerender uses link data.
// NOTE: we must capture this *before* resolving staleTime/varyParams, // NOTE: we must capture this *before* resolving staleTime/varyParams,
// which always emit new static chunks. // which always emit new static chunks.
if (collectedChunksByStage) {
didLinkDataUnblockNewContent = didLinkDataUnblockNewContent =
collectedChunksByStage.staticChunks.length > collectedChunksByStage.staticChunks.length >
collectedChunksByStage.shellStaticChunks.length collectedChunksByStage.shellStaticChunks.length
}
// Now that the prerendering is complete, we know the final stale // Now that the prerendering is complete, we know the final stale
// time and vary params. Close the stale time iterable and resolve // time and vary params. Close the stale time iterable and resolve
...@@ -7689,7 +7671,8 @@ async function prerenderToStream( ...@@ -7689,7 +7671,8 @@ async function prerenderToStream(
if (staleTimeIterable !== undefined) { if (staleTimeIterable !== undefined) {
staleTimeIterable.close() staleTimeIterable.close()
} }
if (shellByteLengthDeferred) {
if (shellByteLengthDeferred && collectedChunksByStage) {
shellByteLengthDeferred.resolve( shellByteLengthDeferred.resolve(
didLinkDataUnblockNewContent didLinkDataUnblockNewContent
? collectedChunksByStage.shellStaticChunks.reduce( ? collectedChunksByStage.shellStaticChunks.reduce(
...@@ -7753,7 +7736,7 @@ async function prerenderToStream( ...@@ -7753,7 +7736,7 @@ async function prerenderToStream(
ctx.pagePath, ctx.pagePath,
metadata metadata
) )
if (appShells) { if (appShells && collectedChunksByStage) {
// If link data (static params) unblocked new content, then the shell has to be partial. // If link data (static params) unblocked new content, then the shell has to be partial.
// If not, then the shell prerender and the static prerender are the same except for staleTime/varyParams. // If not, then the shell prerender and the static prerender are the same except for staleTime/varyParams.
const shellIsPartial = didLinkDataUnblockNewContent const shellIsPartial = didLinkDataUnblockNewContent
...@@ -8924,19 +8907,36 @@ function createPrerenderChunksAccumulator(): PrerenderChunksAccumulator { ...@@ -8924,19 +8907,36 @@ function createPrerenderChunksAccumulator(): PrerenderChunksAccumulator {
allChunks: process.env.NODE_ENV === 'development' ? [] : null, allChunks: process.env.NODE_ENV === 'development' ? [] : null,
} }
} }
type PrerenderChunksAccumulator = { type PrerenderChunksAccumulator = {
prerenderChunks: Uint8Array[] prerenderChunks: Uint8Array[]
allChunks: Uint8Array[] | null allChunks: Uint8Array[] | null
} }
async function collectPrerenderChunksWeb( function collectPrerenderChunk(
stream: ReadableStream<Uint8Array>,
chunks: PrerenderChunksAccumulator, chunks: PrerenderChunksAccumulator,
streamState: StreamPendingState, signal: AbortSignal,
signal: AbortSignal chunk: Uint8Array
) {
// The chunks emitted after an abort are not part of the prerender...
if (!signal.aborted) {
chunks.prerenderChunks.push(chunk)
}
// ...but if they contain debug info, we still want to collect them
// to improve error messages.
chunks.allChunks?.push(chunk)
}
async function iterateStreamingPrerenderChunksWeb(
stream: ReadableStream<Uint8Array>,
signal: AbortSignal,
onChunk: (chunk: Uint8Array) => void,
streamState?: StreamPendingState
): Promise<void> { ): Promise<void> {
const reader = stream.getReader() const reader = stream.getReader()
if (streamState) {
streamState.isPending = true streamState.isPending = true
}
// In production, there's no debug info, so we don't need to capture // In production, there's no debug info, so we don't need to capture
// anything emitted after the abort and can cancel immediately. // anything emitted after the abort and can cancel immediately.
...@@ -8953,14 +8953,12 @@ async function collectPrerenderChunksWeb( ...@@ -8953,14 +8953,12 @@ async function collectPrerenderChunksWeb(
while (true) { while (true) {
const { done, value } = await reader.read() const { done, value } = await reader.read()
if (done) { if (done) {
streamState.isPending = false
break break
} }
onChunk(value)
if (!signal.aborted) {
chunks.prerenderChunks.push(value)
} }
chunks.allChunks?.push(value) if (streamState) {
streamState.isPending = false
} }
} }
......
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