next.js
d8b6ae9c - Keep optimistic-route param handling monomorphic (#96169)

Commit
117 days ago
Keep optimistic-route param handling monomorphic (#96169) Found by `pnpm bench:deopt --scenario segment-cache` (V8 deopt/IC analysis of the client segment cache), with map-level IC evidence. Two related sources of megamorphism in the optimistic route matching path: (1) `String.prototype.split` returns arrays whose elements kind alternates between PACKED_ELEMENTS and HOLEY_ELEMENTS depending on internal fast paths, and `filter`/`slice` propagate the kind, so the arrays flowing through `matchKnownRoutePart` and `reifyRouteTree` had unstable maps, causing repeated "wrong map" eager deopts at the `slice` site in `matchKnownRoutePart` and the `join` site in `reifyRouteTree` (which runs once per tree node on every optimistic match); and (2) the private `ResolvedParams` map typed as `Map<string, string | string[]>` forced a polymorphic `Array.isArray`/`join` branch on every dynamic segment during reification. Semantics are preserved exactly. Catch-all param values are now joined into a single string at the moment they are resolved in `matchKnownRoutePart` instead of at read time in `reifyRouteTree` — the value was only ever read once and immediately collapsed to a joined string cache key, and the empty optional catch-all case writes `''`, which is identical to `[].join('/')`. The new `splitPathnameIntoParts` helper is byte-for-byte equivalent to `pathname.split('/').filter((p) => p !== '')`: empty tokens from leading, trailing, or consecutive slashes are skipped, but the parts array is built by pushing into an array literal so it always has a packed elements kind. Before/after `findings.txt` diff from the bench scenario: ```diff -high deopt-eager packages/next/src/client/components/segment-cache/optimistic-routes.ts matchKnownRoutePart wrong map -high deopt-eager packages/next/src/client/components/segment-cache/optimistic-routes.ts reifyRouteTree wrong map -info deopt-lazy packages/next/src/client/components/segment-cache/optimistic-routes.ts matchKnownRoutePart (unknown) -info deopt-lazy packages/next/src/client/components/segment-cache/optimistic-routes.ts reifyRouteTree (unknown) -info ic-polymorphic packages/next/src/client/components/segment-cache/cache.ts eh keys: filter -info ic-polymorphic packages/next/src/client/components/segment-cache/cache.ts eh keys: split -info ic-polymorphic packages/next/src/client/components/segment-cache/optimistic-routes.ts e keys: join -info ic-polymorphic packages/next/src/client/components/segment-cache/optimistic-routes.ts e keys: length -info ic-polymorphic packages/next/src/client/components/segment-cache/optimistic-routes.ts e keys: slice -info ic-polymorphic packages/next/src/client/components/segment-cache/optimistic-routes.ts g keys: filter -info ic-polymorphic packages/next/src/client/components/segment-cache/optimistic-routes.ts g keys: split -info ic-polymorphic packages/next/src/client/components/segment-cache/optimistic-routes.ts h keys: split +info ic-polymorphic packages/next/src/client/components/segment-cache/cache-key.ts u keys: charCodeAt +info ic-polymorphic packages/next/src/client/components/segment-cache/cache-key.ts u keys: length +info ic-polymorphic packages/next/src/client/components/segment-cache/cache-key.ts u keys: slice ``` Both high-severity wrong-map deopts in `optimistic-routes.ts` are gone (confirmed across two independent after-runs); the remaining new lines are info-level string-receiver ICs inside the splitter helper. Total ic-polymorphic findings dropped from 90 to 80. Tests (all passing, `test-start-turbo`): - `test/e2e/app-dir/segment-cache/optimistic-route-cache-keying-regression` — 1 passed - `test/e2e/app-dir/segment-cache/optimistic-routing-parallel-slot-catchall-regression` — 1 passed - `test/e2e/app-dir/segment-cache/encoded-slash-params` — 2 passed
Author
Parents
Loading