perf: pre-compute Vary header and reduce per-request header overhead#91607
Open
benfavre wants to merge 1 commit intovercel:canaryfrom
Open
perf: pre-compute Vary header and reduce per-request header overhead#91607benfavre wants to merge 1 commit intovercel:canaryfrom
benfavre wants to merge 1 commit intovercel:canaryfrom
Conversation
Three optimizations targeting the HTTP header setting hot path (622ms / 2.5%
in static serving profiles):
1. Pre-compute Vary header strings at module level
- `setVaryHeader` in base-server.ts and `getVaryHeader` in app-page
module.ts both constructed the same template literal on every request
from 4-5 constant header names. Hoist to module-level constants
(`STATIC_VARY_HEADER`, `STATIC_VARY_HEADER_WITH_NEXT_URL`).
2. Inline addRequestMeta to skip redundant get/set wrapper
- `addRequestMeta` was called ~89 times per request cycle, each time
doing: `getRequestMeta()` (symbol read + `|| {}` fallback) -> set
property -> `setRequestMeta()` (symbol write of same reference).
After first initialization the get/set wrapper is pure overhead.
Now reads the symbol directly, branches on existence, and skips
the redundant reassignment.
3. Pre-compute X-Powered-By header name/value constants
- Hoist the `'X-Powered-By'` and `'Next.js'` string literals to
module-level constants in send-payload.ts.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Collaborator
|
Allow CI Workflow Run
Note: this should only be enabled once the PR is ready to go and can only be enabled by a maintainer |
Contributor
Author
Performance ImpactProfiling setup: Node.js v25.7.0, HTTP header operations cost 622ms (2.5%) of CPU in the static serving profile:
Changes: 1. Pre-computed Vary header strings (
2. Simplified
3. Hoisted constants in
Test Verification
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Three optimizations targeting the HTTP header setting hot path, which accounts for 622ms (2.5%) in static serving profiles (
_storeHeader: 244ms,setHeader: 209ms,checkInvalidHeaderChar: 169ms):Pre-compute Vary header strings at module level —
setVaryHeader(base-server.ts) andgetVaryHeader(app-page/module.ts) both constructed the same 4-header template literal on every request via string interpolation. Hoisted to module-level constantsSTATIC_VARY_HEADERandSTATIC_VARY_HEADER_WITH_NEXT_URL.Inline
addRequestMetato skip redundant get/set wrapper — Called ~89 times per request cycle, each invocation didgetRequestMeta()(symbol lookup +|| {}fallback) → property set →setRequestMeta()(symbol write of the same object reference). After first initialization the wrapper functions are pure overhead. Now reads the symbol directly, branches on existence, and skips the redundant reassignment.Pre-compute
X-Powered-Byheader name/value constants — Hoisted string literals to module-level constants insend-payload.ts.Test plan
curl -vagainst a running dev serverautocannonorclinic flameto confirm reduced time in_storeHeader/setHeader🤖 Generated with Claude Code