perf: replace structuredClone with shallow copy in handleRewrites#91572
Open
benfavre wants to merge 1 commit intovercel:canaryfrom
Open
perf: replace structuredClone with shallow copy in handleRewrites#91572benfavre wants to merge 1 commit intovercel:canaryfrom
benfavre wants to merge 1 commit intovercel:canaryfrom
Conversation
`structuredClone(parsedUrl)` in `handleRewrites` was the vercel#1 non-React CPU hotspot at 780ms (8.6% of CPU time) in production profiles under load. It runs on every request that goes through rewrite handling. `structuredClone` is expensive because it handles deep object graphs, circular references, and transferables — none of which apply here. `NextUrlWithParsedQuery` is a flat object: all properties are primitives (string | null | boolean) plus a single-level `query` record. Replace with object spread + query spread, which is semantically equivalent and reduces this hotspot to near-zero overhead. Also pre-compute the basePath RegExp outside `handleRewrites` so it isn't re-allocated on every rewrite check iteration. 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
Test Verification
All tests run on the |
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
structuredClone(parsedUrl)with object spread + query spread inhandleRewrites(packages/next/src/server/server-utils.ts)basePathRegExp once ingetServerUtilsinstead of allocating a newRegExpon every rewrite check iterationWhy
structuredClone(parsedUrl)was the #1 non-React CPU hotspot in production CPU profiles under load:structuredCloneis designed for deep object graphs with circular references, transferables, typed arrays, etc. None of that applies here —NextUrlWithParsedQueryis a flat object:A shallow copy (
{ ...parsedUrl, query: { ...parsedUrl.query } }) is semantically equivalent and reduces this hotspot to near-zero overhead.The
new RegExp()insidecheckRewritewas also being re-created on every rewrite rule check, despitebasePathbeing constant for the lifetime of the server utils. Hoisted to the outer scope.Test plan
NextUrlWithParsedQueryextendsLegacyUrl— all properties are primitives ornull, plus flatqueryrecordrewrittenParsedUrland itsqueryare independent copies, so mutations insidecheckRewritedon't affect the originalparsedUrl🤖 Generated with Claude Code