Commit dddf2c76 authored by Jimmy Lai's avatar Jimmy Lai

Improve SSR throughput with micro-optimizations and webstreams improvements

Merges 6 separate TransformStream instances into a single unified
createUnifiedSSRTransformStream for the dynamic SSR path, eliminating
intermediate stream allocations and microtask hops per chunk.

Adds opt-in perf marks (NEXT_PERF_MARKS=1) with zero overhead when
disabled, benchmark runner with CPU profiling and phase timing, and
unit tests covering behavioral equivalence between legacy and unified
transform chains.
parent 6aeef8eb
last-run.json
*.cpuprofile
...@@ -271,7 +271,7 @@ async function createForwardedActionResponse( ...@@ -271,7 +271,7 @@ async function createForwardedActionResponse(
) { ) {
// copy the headers from the redirect response to the response we're sending // copy the headers from the redirect response to the response we're sending
for (const [key, value] of response.headers) { for (const [key, value] of response.headers) {
if (!actionsForbiddenHeaders.includes(key)) { if (!actionsForbiddenHeaders.has(key)) {
res.setHeader(key, value) res.setHeader(key, value)
} }
} }
...@@ -430,7 +430,7 @@ async function createRedirectRenderResult( ...@@ -430,7 +430,7 @@ async function createRedirectRenderResult(
) { ) {
// copy the headers from the redirect response to the response we're sending // copy the headers from the redirect response to the response we're sending
for (const [key, value] of response.headers) { for (const [key, value] of response.headers) {
if (!actionsForbiddenHeaders.includes(key)) { if (!actionsForbiddenHeaders.has(key)) {
res.setHeader(key, value) res.setHeader(key, value)
} }
} }
......
...@@ -156,6 +156,10 @@ import { createOpaqueFallbackRouteParams } from './request/fallback-params' ...@@ -156,6 +156,10 @@ import { createOpaqueFallbackRouteParams } from './request/fallback-params'
import { RouteKind } from './route-kind' import { RouteKind } from './route-kind'
import type { ErrorModule } from './load-default-error-components' import type { ErrorModule } from './load-default-error-components'
// Pre-computed Vary header strings to avoid string concatenation per request
const BASE_VARY_HEADER = `${RSC_HEADER}, ${NEXT_ROUTER_STATE_TREE_HEADER}, ${NEXT_ROUTER_PREFETCH_HEADER}, ${NEXT_ROUTER_SEGMENT_PREFETCH_HEADER}`
const BASE_VARY_HEADER_WITH_NEXT_URL = `${BASE_VARY_HEADER}, ${NEXT_URL}`
export type FindComponentsResult< export type FindComponentsResult<
NextModule extends GenericComponentMod = GenericComponentMod, NextModule extends GenericComponentMod = GenericComponentMod,
> = { > = {
...@@ -959,7 +963,10 @@ export default abstract class Server< ...@@ -959,7 +963,10 @@ export default abstract class Server<
// hello/world or backslashes to forward slashes, this does not // hello/world or backslashes to forward slashes, this does not
// handle trailing slash as that is handled the same as a next.config.js // handle trailing slash as that is handled the same as a next.config.js
// redirect // redirect
if (urlNoQuery?.match(/(\\|\/\/)/)) { if (
urlNoQuery &&
(urlNoQuery.includes('//') || urlNoQuery.includes('\\'))
) {
const cleanUrl = normalizeRepeatedSlashes(req.url!) const cleanUrl = normalizeRepeatedSlashes(req.url!)
res.redirect(cleanUrl, 308).body(cleanUrl).send() res.redirect(cleanUrl, 308).body(cleanUrl).send()
return return
...@@ -1768,15 +1775,17 @@ export default abstract class Server< ...@@ -1768,15 +1775,17 @@ export default abstract class Server<
const ctx: RequestContext<ServerRequest, ServerResponse> = { const ctx: RequestContext<ServerRequest, ServerResponse> = {
...partialContext, ...partialContext,
renderOpts: { // Use Object.create to avoid copying all ~30+ properties of renderOpts
...this.renderOpts, // per request. The per-request overrides become own properties on a
// prototypal child, while base properties are inherited via prototype.
renderOpts: Object.assign(Object.create(this.renderOpts), {
// `renderOpts.botType` is accumulated in `this.renderImpl()` // `renderOpts.botType` is accumulated in `this.renderImpl()`
supportsDynamicResponse: !this.renderOpts.botType, supportsDynamicResponse: !this.renderOpts.botType,
serveStreamingMetadata: shouldServeStreamingMetadata( serveStreamingMetadata: shouldServeStreamingMetadata(
ua, ua,
this.nextConfig.htmlLimitedBots this.nextConfig.htmlLimitedBots
), ),
}, }),
} }
const payload = await fn(ctx) const payload = await fn(ctx)
...@@ -1826,10 +1835,10 @@ export default abstract class Server< ...@@ -1826,10 +1835,10 @@ export default abstract class Server<
): Promise<string | null> { ): Promise<string | null> {
const ctx: RequestContext<ServerRequest, ServerResponse> = { const ctx: RequestContext<ServerRequest, ServerResponse> = {
...partialContext, ...partialContext,
renderOpts: { // Prototypal inheritance avoids copying all renderOpts properties per call
...this.renderOpts, renderOpts: Object.assign(Object.create(this.renderOpts), {
supportsDynamicResponse: false, supportsDynamicResponse: false,
}, }),
} }
const payload = await fn(ctx) const payload = await fn(ctx)
if (payload === null) { if (payload === null) {
...@@ -1989,7 +1998,6 @@ export default abstract class Server< ...@@ -1989,7 +1998,6 @@ export default abstract class Server<
isAppPath: boolean, isAppPath: boolean,
resolvedPathname: string resolvedPathname: string
): void { ): void {
const baseVaryHeader = `${RSC_HEADER}, ${NEXT_ROUTER_STATE_TREE_HEADER}, ${NEXT_ROUTER_PREFETCH_HEADER}, ${NEXT_ROUTER_SEGMENT_PREFETCH_HEADER}`
const isRSCRequest = getRequestMeta(req, 'isRSCRequest') ?? false const isRSCRequest = getRequestMeta(req, 'isRSCRequest') ?? false
let addedNextUrlToVary = false let addedNextUrlToVary = false
...@@ -1997,12 +2005,12 @@ export default abstract class Server< ...@@ -1997,12 +2005,12 @@ export default abstract class Server<
if (isAppPath && this.pathCouldBeIntercepted(resolvedPathname)) { if (isAppPath && this.pathCouldBeIntercepted(resolvedPathname)) {
// Interception route responses can vary based on the `Next-URL` header. // Interception route responses can vary based on the `Next-URL` header.
// We use the Vary header to signal this behavior to the client to properly cache the response. // We use the Vary header to signal this behavior to the client to properly cache the response.
res.appendHeader('vary', `${baseVaryHeader}, ${NEXT_URL}`) res.appendHeader('vary', BASE_VARY_HEADER_WITH_NEXT_URL)
addedNextUrlToVary = true addedNextUrlToVary = true
} else if (isAppPath || isRSCRequest) { } else if (isAppPath || isRSCRequest) {
// We don't need to include `Next-URL` in the Vary header for non-interception routes since it won't affect the response. // We don't need to include `Next-URL` in the Vary header for non-interception routes since it won't affect the response.
// We also set this header for pages to avoid caching issues when navigating between pages and app. // We also set this header for pages to avoid caching issues when navigating between pages and app.
res.appendHeader('vary', baseVaryHeader) res.appendHeader('vary', BASE_VARY_HEADER)
} }
if (!addedNextUrlToVary) { if (!addedNextUrlToVary) {
...@@ -2612,10 +2620,10 @@ export default abstract class Server< ...@@ -2612,10 +2620,10 @@ export default abstract class Server<
{ {
...ctx, ...ctx,
pathname: match.definition.pathname, pathname: match.definition.pathname,
renderOpts: { // Prototypal inheritance avoids copying all renderOpts per match
...ctx.renderOpts, renderOpts: Object.assign(Object.create(ctx.renderOpts), {
params: match.params, params: match.params,
}, }),
}, },
bubbleNoFallback bubbleNoFallback
) )
......
export const ipcForbiddenHeaders = [ // Use Sets for O(1) header lookups instead of O(n) Array.includes()
export const ipcForbiddenHeaders = new Set([
'accept-encoding', 'accept-encoding',
'keepalive', 'keepalive',
'keep-alive', 'keep-alive',
...@@ -8,17 +9,17 @@ export const ipcForbiddenHeaders = [ ...@@ -8,17 +9,17 @@ export const ipcForbiddenHeaders = [
'connection', 'connection',
// marked as unsupported by undici: https://github.com/nodejs/undici/blob/c83b084879fa0bb8e0469d31ec61428ac68160d5/lib/core/request.js#L354 // marked as unsupported by undici: https://github.com/nodejs/undici/blob/c83b084879fa0bb8e0469d31ec61428ac68160d5/lib/core/request.js#L354
'expect', 'expect',
] ])
export const actionsForbiddenHeaders = [ export const actionsForbiddenHeaders = new Set([
...ipcForbiddenHeaders, ...ipcForbiddenHeaders,
'content-length', 'content-length',
'set-cookie', 'set-cookie',
] ])
export const filterReqHeaders = ( export const filterReqHeaders = (
headers: Record<string, undefined | string | number | string[]>, headers: Record<string, undefined | string | number | string[]>,
forbiddenHeaders: string[] forbiddenHeaders: Set<string>
) => { ) => {
// Some browsers are not matching spec and sending Content-Length: 0. This causes issues in undici // Some browsers are not matching spec and sending Content-Length: 0. This causes issues in undici
// https://github.com/nodejs/undici/issues/2046 // https://github.com/nodejs/undici/issues/2046
...@@ -28,7 +29,7 @@ export const filterReqHeaders = ( ...@@ -28,7 +29,7 @@ export const filterReqHeaders = (
for (const [key, value] of Object.entries(headers)) { for (const [key, value] of Object.entries(headers)) {
if ( if (
forbiddenHeaders.includes(key) || forbiddenHeaders.has(key) ||
!(Array.isArray(value) || typeof value === 'string') !(Array.isArray(value) || typeof value === 'string')
) { ) {
delete headers[key] delete headers[key]
...@@ -39,7 +40,7 @@ export const filterReqHeaders = ( ...@@ -39,7 +40,7 @@ export const filterReqHeaders = (
// These are headers that are only used internally and should // These are headers that are only used internally and should
// not be honored from the external request // not be honored from the external request
const INTERNAL_HEADERS = [ const INTERNAL_HEADERS = new Set([
'x-middleware-rewrite', 'x-middleware-rewrite',
'x-middleware-redirect', 'x-middleware-redirect',
'x-middleware-set-cookie', 'x-middleware-set-cookie',
...@@ -49,13 +50,13 @@ const INTERNAL_HEADERS = [ ...@@ -49,13 +50,13 @@ const INTERNAL_HEADERS = [
'x-now-route-matches', 'x-now-route-matches',
'x-matched-path', 'x-matched-path',
'x-next-resume-state-length', 'x-next-resume-state-length',
] ])
export const filterInternalHeaders = ( export const filterInternalHeaders = (
headers: Record<string, undefined | string | string[]> headers: Record<string, undefined | string | string[]>
) => { ) => {
for (const header in headers) { for (const header in headers) {
if (INTERNAL_HEADERS.includes(header)) { if (INTERNAL_HEADERS.has(header)) {
delete headers[header] delete headers[header]
} }
} }
......
...@@ -4,16 +4,20 @@ import { ...@@ -4,16 +4,20 @@ import {
} from '../../shared/lib/router/utils/is-bot' } from '../../shared/lib/router/utils/is-bot'
import type { BaseNextRequest } from '../base-http' import type { BaseNextRequest } from '../base-http'
let cachedRegexSource: string | undefined
let cachedRegex: RegExp | undefined
export function shouldServeStreamingMetadata( export function shouldServeStreamingMetadata(
userAgent: string, userAgent: string,
htmlLimitedBots: string | undefined htmlLimitedBots: string | undefined
): boolean { ): boolean {
const blockingMetadataUARegex = new RegExp( const source = htmlLimitedBots || HTML_LIMITED_BOT_UA_RE_STRING
htmlLimitedBots || HTML_LIMITED_BOT_UA_RE_STRING, if (source !== cachedRegexSource) {
'i' cachedRegex = new RegExp(source, 'i')
) cachedRegexSource = source
}
// Only block metadata for HTML-limited bots // Only block metadata for HTML-limited bots
if (userAgent && blockingMetadataUARegex.test(userAgent)) { if (userAgent && cachedRegex!.test(userAgent)) {
return false return false
} }
return true return true
......
...@@ -140,7 +140,9 @@ export const NextVanillaSpanAllowlist = new Set([ ...@@ -140,7 +140,9 @@ export const NextVanillaSpanAllowlist = new Set([
NextNodeServerSpan.createComponentTree, NextNodeServerSpan.createComponentTree,
NextNodeServerSpan.findPageComponents, NextNodeServerSpan.findPageComponents,
NextNodeServerSpan.getLayoutOrPageModule, NextNodeServerSpan.getLayoutOrPageModule,
NextNodeServerSpan.startResponse, // startResponse removed: it wraps a no-op `() => undefined` in pipe-readable.ts,
// so the span has no meaningful duration or children. Removing it avoids
// context.with(), startActiveSpan(), and rootSpanAttributesStore overhead per request.
NextNodeServerSpan.clientComponentLoading, NextNodeServerSpan.clientComponentLoading,
]) ])
......
...@@ -9,6 +9,10 @@ import { getTracer } from './lib/trace/tracer' ...@@ -9,6 +9,10 @@ import { getTracer } from './lib/trace/tracer'
import { NextNodeServerSpan } from './lib/trace/constants' import { NextNodeServerSpan } from './lib/trace/constants'
import { getClientComponentLoaderMetrics } from './client-component-renderer-logger' import { getClientComponentLoaderMetrics } from './client-component-renderer-logger'
// Cache env var at module level to avoid per-request process.env lookups
const NEXT_OTEL_PERFORMANCE_PREFIX =
process.env.NEXT_OTEL_PERFORMANCE_PREFIX || ''
export function isAbortError(e: any): e is Error & { name: 'AbortError' } { export function isAbortError(e: any): e is Error & { name: 'AbortError' } {
return e?.name === 'AbortError' || e?.name === ResponseAbortedName return e?.name === 'AbortError' || e?.name === ResponseAbortedName
} }
...@@ -50,14 +54,11 @@ function createWriterFromResponse( ...@@ -50,14 +54,11 @@ function createWriterFromResponse(
if (!started) { if (!started) {
started = true started = true
if ( if (NEXT_OTEL_PERFORMANCE_PREFIX) {
'performance' in globalThis &&
process.env.NEXT_OTEL_PERFORMANCE_PREFIX
) {
const metrics = getClientComponentLoaderMetrics() const metrics = getClientComponentLoaderMetrics()
if (metrics) { if (metrics) {
performance.measure( performance.measure(
`${process.env.NEXT_OTEL_PERFORMANCE_PREFIX}:next-client-component-loading`, `${NEXT_OTEL_PERFORMANCE_PREFIX}:next-client-component-loading`,
{ {
start: metrics.clientComponentLoadStart, start: metrics.clientComponentLoadStart,
end: end:
......
/** /**
* Find the starting index of Uint8Array `b` within Uint8Array `a`. * Find the starting index of Uint8Array `b` within Uint8Array `a`.
* Uses first-byte indexOf to skip non-matching positions quickly,
* then verifies the full pattern match.
*/ */
export function indexOfUint8Array(a: Uint8Array, b: Uint8Array) { export function indexOfUint8Array(a: Uint8Array, b: Uint8Array) {
if (b.length === 0) return 0 if (b.length === 0) return 0
if (a.length === 0 || b.length > a.length) return -1 if (a.length === 0 || b.length > a.length) return -1
// start iterating through `a` const firstByte = b[0]
for (let i = 0; i <= a.length - b.length; i++) { const bLen = b.length
let completeMatch = true const limit = a.length - bLen
// from index `i`, iterate through `b` and check for mismatch
for (let j = 0; j < b.length; j++) { let i = 0
// if the values do not match, then this isn't a complete match, exit `b` iteration early and iterate to next index of `a`. while (i <= limit) {
if (a[i + j] !== b[j]) { // Use native indexOf to jump to next candidate position.
completeMatch = false // TypedArray.indexOf is V8-optimized and much faster than a JS byte-by-byte scan.
const idx = a.indexOf(firstByte, i)
if (idx === -1 || idx > limit) return -1
// Verify the full pattern at this position
let match = true
for (let j = 1; j < bLen; j++) {
if (a[idx + j] !== b[j]) {
match = false
break break
} }
} }
if (completeMatch) { if (match) return idx
return i i = idx + 1
}
} }
return -1 return -1
......
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