Andrew ClarkandGitHub 2d2f501fa5 Store RouteTree slots in a Map to keep slot access monomorphic (#96168)
### Root cause

Parallel route slot names are app-defined, so a `RouteTree.slots` stored
as a plain object has an unbounded set of hidden classes: every distinct
combination of slot names (`{children}`, `{children, side}`, ...)
produces a different shape. Keyed access over these objects makes the
inline cache megamorphic. This was the single `ic-megamorphic` finding
reported by `pnpm bench:deopt --scenario segment-cache`, at the keyed
store in `convertTreePrefetchToRouteTree` (`cache.ts:1531`, keys
`children`, `side`).

This PR converts `RouteTree.slots` from `{ [parallelRouteKey: string]:
RouteTree }` to `Map<string, RouteTree>`. All reads, writes, and
iterations over RouteTree slots now go through monomorphic `Map` method
calls, and iteration order is preserved (Map iterates in insertion
order, matching plain-object string-key order).

### Scope

This was an approved design decision: `RouteTree` is an in-memory-only
structure, so only it changes. `CacheNode.slots` (spread into JSX props)
and the wire formats (`TreePrefetch.slots`, `FlightRouterState[1]`)
remain plain objects; the existing conversion functions bridge them into
and out of the Map.

### Before/after findings diff

```diff
--- bench/deopt/artifacts/slots-map-before/findings.txt
+++ bench/deopt/artifacts/slots-map-after/findings.txt
@@
-high  ic-megamorphic  packages/next/src/client/components/segment-cache/cache.ts  e  keys: children, side
+high  ic-megamorphic  packages/next/src/client/components/segment-cache/scheduler.ts  e  keys: children, side
@@
+info  deopt-dependency-change  packages/next/src/client/components/segment-cache/scheduler.ts  pingSharedPartOfCacheComponentsTree  dependent prototype chain changed
+info  deopt-eager  packages/next/src/client/components/segment-cache/scheduler.ts  _heapIndex  Insufficient type feedback for generic named access
+info  deopt-eager  packages/next/src/client/components/segment-cache/scheduler.ts  cancelPrefetchTask  Insufficient type feedback for generic named access
+info  ic-polymorphic  packages/next/src/client/components/segment-cache/cache.ts  eR  keys: push
```

The targeted finding at `cache.ts:1531` is cleared. The remaining
megamorphic site (stable across two runs) is `scheduler.ts:1068:23` in
`pingSharedPartOfCacheComponentsTree` — the keyed load
`oldTreeChildren[parallelRouteKey]`, where `oldTreeChildren` is
`FlightRouterState[1]`, i.e. the wire-format plain object that is
intentionally out of scope for this change. That access existed before
this PR; previously the `for...in` loop key qualified the sibling load
for V8's fast enum-cache path, and with Map iteration keys it now
surfaces as a generic keyed load. Eliminating it would require changing
the `FlightRouterState` children format, which is a separate decision.
The new info-level lines are one-time warm-up/prototype-registration
deopts from the introduced `Map` usage, not steady-state IC misses.

### Tests

- `test/e2e/app-dir/segment-cache/basic` (start-turbo): 10 passed, 1
skipped
-
`test/e2e/app-dir/segment-cache/optimistic-routing-parallel-slot-catchall-regression`
(start-turbo): 1 passed
- `test/e2e/app-dir/segment-cache/cached-navigations` (start-turbo): 16
passed
- `pnpm --filter=next types` passes (also re-run after rebasing onto
canary)
2026-07-24 21:02:01 +00:00
2025-10-13 10:43:51 +02:00
2024-06-28 04:41:35 -07:00
2026-07-14 08:03:43 +02:00
2026-07-23 23:33:24 +00:00
2025-01-02 14:31:08 -08:00
2026-07-23 23:33:24 +00:00
2018-10-01 01:02:10 +02:00
2024-08-14 13:05:42 +00:00

Next.js logo

Next.js

Vercel logo NPM version License Join the community on GitHub

Getting Started

Used by some of the world's largest companies, Next.js enables you to create full-stack web applications by extending the latest React features, and integrating powerful Rust-based JavaScript tooling for the fastest builds.

Documentation

Visit https://nextjs.org/docs to view the full documentation.

Community

The Next.js community can be found on GitHub Discussions where you can ask questions, voice ideas, and share your projects with other people.

To chat with other community members you can join the Next.js Discord server.

Do note that our Code of Conduct applies to all Next.js community channels. Users are highly encouraged to read and adhere to it to avoid repercussions.

Contributing

Contributions to Next.js are welcome and highly appreciated. However, before you jump right into it, we would like you to review our Contribution Guidelines to make sure you have a smooth experience contributing to Next.js.

Good First Issues:

We have a list of good first issues that contain bugs that have a relatively limited scope. This is a great place for newcomers and beginners alike to get started, gain experience, and get familiar with our contribution process.


Security

If you believe you have found a security vulnerability in Next.js, we encourage you to responsibly disclose this and NOT open a public issue.

To participate in our Open Source Software Bug Bounty program, please email [email protected]. We will add you to the program and provide further instructions for submitting your report.

S
Description
The React Framework
Readme
2.4 GiB
Languages
JavaScript 55.6%
TypeScript 30%
Rust 12.9%
MDX 0.7%
CSS 0.7%