Unverified Commit aa13f252 authored by Andrew Clark's avatar Andrew Clark Committed by GitHub

Keep VaryPath monomorphic by making isRootParam required (#96122)

`VaryPath` nodes are created and traversed on a very hot path in the
segment cache, so their object shape matters for performance.
`isRootParam` was optional: most nodes left it undefined while a few set
it, so nodes had inconsistent shapes. That prevents V8 from sharing a
single hidden class across them and deopts the cache hot paths.

This makes `isRootParam` a required boolean, always set (`false` for
structural and search param nodes), so every `VaryPath` node has the
same shape and these paths stay monomorphic.
parent 6fd889b9
...@@ -41,10 +41,13 @@ export type VaryPath = { ...@@ -41,10 +41,13 @@ export type VaryPath = {
* Whether this node corresponds to a root param — a path param at or above * Whether this node corresponds to a root param — a path param at or above
* the application's root layout. Root params may appear in the App Shell, so * the application's root layout. Root params may appear in the App Shell, so
* the shell vary path keeps their concrete value instead of replacing it with * the shell vary path keeps their concrete value instead of replacing it with
* Fallback. See getShellSegmentVaryPath. Only set on path param nodes; * Fallback. See getShellSegmentVaryPath. Only ever true on path param nodes;
* undefined (falsy) for structural and search param nodes. * false for structural and search param nodes.
*
* Always a boolean (never undefined) so that every VaryPath node shares a
* single hidden class, keeping the cache hot paths monomorphic.
*/ */
isRootParam?: boolean isRootParam: boolean
parent: VaryPath | null parent: VaryPath | null
} }
...@@ -57,12 +60,15 @@ export type RouteVaryPath = Opaque< ...@@ -57,12 +60,15 @@ export type RouteVaryPath = Opaque<
{ {
id: null id: null
value: NormalizedPathname value: NormalizedPathname
isRootParam: false
parent: { parent: {
id: '?' id: '?'
value: NormalizedSearch value: NormalizedSearch
isRootParam: false
parent: { parent: {
id: null id: null
value: NormalizedNextUrl | null | FallbackType value: NormalizedNextUrl | null | FallbackType
isRootParam: false
parent: null parent: null
} }
} }
...@@ -75,6 +81,7 @@ export type LayoutVaryPath = Opaque< ...@@ -75,6 +81,7 @@ export type LayoutVaryPath = Opaque<
{ {
id: null id: null
value: string value: string
isRootParam: false
parent: PartialSegmentVaryPath | null parent: PartialSegmentVaryPath | null
}, },
'LayoutVaryPath' 'LayoutVaryPath'
...@@ -85,9 +92,11 @@ export type PageVaryPath = Opaque< ...@@ -85,9 +92,11 @@ export type PageVaryPath = Opaque<
{ {
id: null id: null
value: string value: string
isRootParam: false
parent: { parent: {
id: '?' id: '?'
value: NormalizedSearch | FallbackType value: NormalizedSearch | FallbackType
isRootParam: false
parent: PartialSegmentVaryPath | null parent: PartialSegmentVaryPath | null
} }
}, },
...@@ -109,12 +118,15 @@ export function getRouteVaryPath( ...@@ -109,12 +118,15 @@ export function getRouteVaryPath(
const varyPath: VaryPath = { const varyPath: VaryPath = {
id: null, id: null,
value: pathname, value: pathname,
isRootParam: false,
parent: { parent: {
id: '?', id: '?',
value: search, value: search,
isRootParam: false,
parent: { parent: {
id: null, id: null,
value: nextUrl, value: nextUrl,
isRootParam: false,
parent: null, parent: null,
}, },
}, },
...@@ -134,12 +146,15 @@ export function getFulfilledRouteVaryPath( ...@@ -134,12 +146,15 @@ export function getFulfilledRouteVaryPath(
const varyPath: VaryPath = { const varyPath: VaryPath = {
id: null, id: null,
value: pathname, value: pathname,
isRootParam: false,
parent: { parent: {
id: '?', id: '?',
value: search, value: search,
isRootParam: false,
parent: { parent: {
id: null, id: null,
value: couldBeIntercepted ? nextUrl : Fallback, value: couldBeIntercepted ? nextUrl : Fallback,
isRootParam: false,
parent: null, parent: null,
}, },
}, },
...@@ -169,6 +184,7 @@ export function finalizeLayoutVaryPath( ...@@ -169,6 +184,7 @@ export function finalizeLayoutVaryPath(
const layoutVaryPath: VaryPath = { const layoutVaryPath: VaryPath = {
id: null, id: null,
value: requestKey, value: requestKey,
isRootParam: false,
parent: varyPath, parent: varyPath,
} }
return layoutVaryPath as LayoutVaryPath return layoutVaryPath as LayoutVaryPath
...@@ -191,9 +207,11 @@ export function finalizePageVaryPath( ...@@ -191,9 +207,11 @@ export function finalizePageVaryPath(
const pageVaryPath: VaryPath = { const pageVaryPath: VaryPath = {
id: null, id: null,
value: requestKey, value: requestKey,
isRootParam: false,
parent: { parent: {
id: '?', id: '?',
value: renderedSearch, value: renderedSearch,
isRootParam: false,
parent: varyPath, parent: varyPath,
}, },
} }
...@@ -243,9 +261,11 @@ export function finalizeMetadataVaryPath( ...@@ -243,9 +261,11 @@ export function finalizeMetadataVaryPath(
// that we're not using a separate vary path part; it's unnecessary because // that we're not using a separate vary path part; it's unnecessary because
// these are not conceptually separate inputs. // these are not conceptually separate inputs.
value: pageRequestKey + HEAD_REQUEST_KEY, value: pageRequestKey + HEAD_REQUEST_KEY,
isRootParam: false,
parent: { parent: {
id: '?', id: '?',
value: renderedSearch, value: renderedSearch,
isRootParam: false,
parent: varyPath, parent: varyPath,
}, },
} }
...@@ -310,9 +330,11 @@ export function getSegmentVaryPathForRequest( ...@@ -310,9 +330,11 @@ export function getSegmentVaryPathForRequest(
const patchedVaryPath: VaryPath = { const patchedVaryPath: VaryPath = {
id: null, id: null,
value: originalVaryPath.value, value: originalVaryPath.value,
isRootParam: false,
parent: { parent: {
id: '?', id: '?',
value: Fallback, value: Fallback,
isRootParam: false,
parent: pathParamsVaryPath, parent: pathParamsVaryPath,
}, },
} }
...@@ -334,9 +356,11 @@ export function clonePageVaryPathWithNewSearchParams( ...@@ -334,9 +356,11 @@ export function clonePageVaryPathWithNewSearchParams(
const clonedVaryPath: VaryPath = { const clonedVaryPath: VaryPath = {
id: null, id: null,
value: originalVaryPath.value, value: originalVaryPath.value,
isRootParam: false,
parent: { parent: {
id: '?', id: '?',
value: newSearch, value: newSearch,
isRootParam: false,
parent: searchParamsVaryPath.parent, parent: searchParamsVaryPath.parent,
}, },
} }
......
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