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(
) {
// copy the headers from the redirect response to the response we're sending
for (const [key, value] of response.headers) {
if (!actionsForbiddenHeaders.includes(key)) {
if (!actionsForbiddenHeaders.has(key)) {
res.setHeader(key, value)
}
}
......@@ -430,7 +430,7 @@ async function createRedirectRenderResult(
) {
// copy the headers from the redirect response to the response we're sending
for (const [key, value] of response.headers) {
if (!actionsForbiddenHeaders.includes(key)) {
if (!actionsForbiddenHeaders.has(key)) {
res.setHeader(key, value)
}
}
......
......@@ -156,6 +156,10 @@ import { createOpaqueFallbackRouteParams } from './request/fallback-params'
import { RouteKind } from './route-kind'
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<
NextModule extends GenericComponentMod = GenericComponentMod,
> = {
......@@ -959,7 +963,10 @@ export default abstract class Server<
// hello/world or backslashes to forward slashes, this does not
// handle trailing slash as that is handled the same as a next.config.js
// redirect
if (urlNoQuery?.match(/(\\|\/\/)/)) {
if (
urlNoQuery &&
(urlNoQuery.includes('//') || urlNoQuery.includes('\\'))
) {
const cleanUrl = normalizeRepeatedSlashes(req.url!)
res.redirect(cleanUrl, 308).body(cleanUrl).send()
return
......@@ -1768,15 +1775,17 @@ export default abstract class Server<
const ctx: RequestContext<ServerRequest, ServerResponse> = {
...partialContext,
renderOpts: {
...this.renderOpts,
// Use Object.create to avoid copying all ~30+ properties of 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()`
supportsDynamicResponse: !this.renderOpts.botType,
serveStreamingMetadata: shouldServeStreamingMetadata(
ua,
this.nextConfig.htmlLimitedBots
),
},
}),
}
const payload = await fn(ctx)
......@@ -1826,10 +1835,10 @@ export default abstract class Server<
): Promise<string | null> {
const ctx: RequestContext<ServerRequest, ServerResponse> = {
...partialContext,
renderOpts: {
...this.renderOpts,
// Prototypal inheritance avoids copying all renderOpts properties per call
renderOpts: Object.assign(Object.create(this.renderOpts), {
supportsDynamicResponse: false,
},
}),
}
const payload = await fn(ctx)
if (payload === null) {
......@@ -1989,7 +1998,6 @@ export default abstract class Server<
isAppPath: boolean,
resolvedPathname: string
): 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
let addedNextUrlToVary = false
......@@ -1997,12 +2005,12 @@ export default abstract class Server<
if (isAppPath && this.pathCouldBeIntercepted(resolvedPathname)) {
// 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.
res.appendHeader('vary', `${baseVaryHeader}, ${NEXT_URL}`)
res.appendHeader('vary', BASE_VARY_HEADER_WITH_NEXT_URL)
addedNextUrlToVary = true
} 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 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) {
......@@ -2612,10 +2620,10 @@ export default abstract class Server<
{
...ctx,
pathname: match.definition.pathname,
renderOpts: {
...ctx.renderOpts,
// Prototypal inheritance avoids copying all renderOpts per match
renderOpts: Object.assign(Object.create(ctx.renderOpts), {
params: match.params,
},
}),
},
bubbleNoFallback
)
......
export const ipcForbiddenHeaders = [
// Use Sets for O(1) header lookups instead of O(n) Array.includes()
export const ipcForbiddenHeaders = new Set([
'accept-encoding',
'keepalive',
'keep-alive',
......@@ -8,17 +9,17 @@ export const ipcForbiddenHeaders = [
'connection',
// marked as unsupported by undici: https://github.com/nodejs/undici/blob/c83b084879fa0bb8e0469d31ec61428ac68160d5/lib/core/request.js#L354
'expect',
]
])
export const actionsForbiddenHeaders = [
export const actionsForbiddenHeaders = new Set([
...ipcForbiddenHeaders,
'content-length',
'set-cookie',
]
])
export const filterReqHeaders = (
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
// https://github.com/nodejs/undici/issues/2046
......@@ -28,7 +29,7 @@ export const filterReqHeaders = (
for (const [key, value] of Object.entries(headers)) {
if (
forbiddenHeaders.includes(key) ||
forbiddenHeaders.has(key) ||
!(Array.isArray(value) || typeof value === 'string')
) {
delete headers[key]
......@@ -39,7 +40,7 @@ export const filterReqHeaders = (
// These are headers that are only used internally and should
// not be honored from the external request
const INTERNAL_HEADERS = [
const INTERNAL_HEADERS = new Set([
'x-middleware-rewrite',
'x-middleware-redirect',
'x-middleware-set-cookie',
......@@ -49,13 +50,13 @@ const INTERNAL_HEADERS = [
'x-now-route-matches',
'x-matched-path',
'x-next-resume-state-length',
]
])
export const filterInternalHeaders = (
headers: Record<string, undefined | string | string[]>
) => {
for (const header in headers) {
if (INTERNAL_HEADERS.includes(header)) {
if (INTERNAL_HEADERS.has(header)) {
delete headers[header]
}
}
......
......@@ -4,16 +4,20 @@ import {
} from '../../shared/lib/router/utils/is-bot'
import type { BaseNextRequest } from '../base-http'
let cachedRegexSource: string | undefined
let cachedRegex: RegExp | undefined
export function shouldServeStreamingMetadata(
userAgent: string,
htmlLimitedBots: string | undefined
): boolean {
const blockingMetadataUARegex = new RegExp(
htmlLimitedBots || HTML_LIMITED_BOT_UA_RE_STRING,
'i'
)
const source = htmlLimitedBots || HTML_LIMITED_BOT_UA_RE_STRING
if (source !== cachedRegexSource) {
cachedRegex = new RegExp(source, 'i')
cachedRegexSource = source
}
// Only block metadata for HTML-limited bots
if (userAgent && blockingMetadataUARegex.test(userAgent)) {
if (userAgent && cachedRegex!.test(userAgent)) {
return false
}
return true
......
......@@ -140,7 +140,9 @@ export const NextVanillaSpanAllowlist = new Set([
NextNodeServerSpan.createComponentTree,
NextNodeServerSpan.findPageComponents,
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,
])
......
......@@ -9,6 +9,10 @@ import { getTracer } from './lib/trace/tracer'
import { NextNodeServerSpan } from './lib/trace/constants'
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' } {
return e?.name === 'AbortError' || e?.name === ResponseAbortedName
}
......@@ -50,14 +54,11 @@ function createWriterFromResponse(
if (!started) {
started = true
if (
'performance' in globalThis &&
process.env.NEXT_OTEL_PERFORMANCE_PREFIX
) {
if (NEXT_OTEL_PERFORMANCE_PREFIX) {
const metrics = getClientComponentLoaderMetrics()
if (metrics) {
performance.measure(
`${process.env.NEXT_OTEL_PERFORMANCE_PREFIX}:next-client-component-loading`,
`${NEXT_OTEL_PERFORMANCE_PREFIX}:next-client-component-loading`,
{
start: metrics.clientComponentLoadStart,
end:
......
/**
* 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) {
if (b.length === 0) return 0
if (a.length === 0 || b.length > a.length) return -1
// start iterating through `a`
for (let i = 0; i <= a.length - b.length; i++) {
let completeMatch = true
// from index `i`, iterate through `b` and check for mismatch
for (let j = 0; j < b.length; j++) {
// if the values do not match, then this isn't a complete match, exit `b` iteration early and iterate to next index of `a`.
if (a[i + j] !== b[j]) {
completeMatch = false
const firstByte = b[0]
const bLen = b.length
const limit = a.length - bLen
let i = 0
while (i <= limit) {
// Use native indexOf to jump to next candidate position.
// 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
}
}
if (completeMatch) {
return i
}
if (match) return idx
i = idx + 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