Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content

perf(react-router): render createLink components in one layer - #9013

Open
schiller-manuel wants to merge 1 commit into
schiller-manuel-create-link-benchmarkfrom
schiller-manuel-create-link-single-layer-e87
Open

schiller-manuel wants to merge 1 commit into
schiller-manuel-create-link-benchmarkfrom
schiller-manuel-create-link-single-layer-e87

Conversation

@schiller-manuel

@schiller-manuel schiller-manuel commented Nov 27, 2026 •

Copy link
Copy Markdown
Collaborator

Agent assessment

Self-reported assessment, not an independent review.

Assessed revision: f651b053948ade7a7022c41d08c8a3e09486b7bf

Dimension Assessment Rationale
Change confidence 8.5/10 ↑ react-router test:unit, test:types, test:eslint, test:build pass locally.
↑ Link is now createLink('a'), so every existing Link test (refs, memo, SSR, state props) runs the created-link path; link-memo.test.tsx already drives memo bail-out through createLink(Host).
↑ _asChild was only ever passed through as any casts; never typed or documented.
↓ Local instruction counts are bimodal, so the create-link gain ranges from -3% to -10% by run mode; CodSpeed results not yet observed.
↓ No React e2e app uses createLink.

🎯 Changes

Stacked on #9010 (adds the create-link benchmark this PR is measured with).

createLink(Comp) used to wrap Link:

React.forwardRef((props, ref) => <Link {...props} _asChild={Comp} ref={ref} />)

That's 3 fibers per custom link (ForwardRef → Memo → ForwardRef) plus a props copy. Now createLink returns the memoized implementation with the host bound directly, and Link is createLink('a'):

export const createLink = (Comp) =>
  React.memo(React.forwardRef((props, ref) => { /* useLinkProps(props, ref, Comp) ... */ }), areLinkPropsEqual)

export const Link = /* @__PURE__ */ createLink('a')

Custom links drop to 2 fibers, same as Link. The internal _asChild prop goes away (also dropped from ROUTER_OPTION_KEYS). Memo now compares the user's props directly instead of the copy plus _asChild, so bail-out behavior is unchanged. In React DevTools, custom links now show up as the same anonymous Memo as Link instead of ForwardRef(CreatedLink) > Memo.

Benchmarks (React, local, baseline = #9010 head)

Instructions retired per lap (macOS /usr/bin/time -l, 300 laps minus setup-only runs, interleaved, 8 runs per side):

Scenario base this PR delta
create-link, median 64.8M 58.1M -10%
create-link, min 58.3M 55.2M -5%
link-mount, min run total 23.21B 23.16B ~0

Run totals cluster in two modes, so treat create-link as roughly -3% to -10%. A separate tighter run measured -3.4% with a same-commit spread of 0.8%. Every create-link candidate run except one beat every baseline run. link-mount's minimums match, as expected: Link keeps the same Memo → ForwardRef structure.

Walltime vitest bench, 4 interleaved runs per side: create-link median 232 → 258 hz (best 254.8 → 263.4 hz). link-mount moved both ways within ±3% rme (best 323 vs 303 hz) on a noisy machine.

Bundle size

Full benchmark:bundle-size:run, gzip: every scenario ≤ 0.

Scenario gzip
react-router.minimal -7
react-router.full -21
react-start.minimal -5
react-start.deferred-hydration -4
react-start.query-integration -7
react-start.full -28
react-start.rsbuild.minimal -48
react-start.rsbuild.minimal-iife -45
react-start.rsbuild.full -14
solid-*, vue-* 0

Attribution: keeping areLinkPropsEqual below Link costs +10 B on react-router.minimal. Keeping _asChild in ROUTER_OPTION_KEYS costs up to +8 B. A function createLink declaration costs +11 B on react-start.minimal.

Overlap with #9005

#9005 (perf(react-router): avoid Link's props copy and no-op preload handlers) touches the same render function. Rebasing onto it conflicts in one hunk of link.tsx: keep this PR's createLink body and take #9005's linkProps.children = ...; return jsx(Comp, linkProps) render tail.

Follow-ups (not changed here)

  • react-router route.tsx: RouteApi.Link, Route.Link and the root route's Link are still forwardRef((props, ref) => <Link ref={ref} from={...} {...props} />), the same extra layer plus a spread.
  • Solid's and Vue's createLink are unchanged; both still wrap Link with _asChild.

AI-authored (GitHub Copilot), reviewed by me.

✅ Checklist

  • I have followed the steps in the Contributing guide.
  • I have tested code changes locally with the relevant test commands, or tests do not apply to this pull request.
  • I fully understand the code in this pull request, including any code generated with AI assistance.

Validation:

  • CI=1 NX_DAEMON=false pnpm nx run-many -p @tanstack/react-router -t test:unit,test:types,test:eslint,test:build --skipRemoteCache: passed
  • pnpm benchmark:bundle-size:run (full, baseline vs this PR): passed, gzip ≤ 0 everywhere

🚀 Release Impact

  • This change affects published code, and I have generated a changeset.
  • This change is docs/CI/dev-only (no release).

Summary by CodeRabbit

  • Bug Fixes
    • Links created with createLink now render consistently with standard links, including conditional children that reflect whether a link is active.

createLink returned a forwardRef component that rendered a memoized Link
with a props copy and an internal `_asChild` host, so every custom Link
paid an extra component and a spread. createLink now returns the
memoized Link implementation with its host bound directly, and Link is
createLink('a').

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
@changeset-bot

changeset-bot Bot commented Nov 27, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: f651b05

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 5 packages
Name Type
@tanstack/react-router Patch
@tanstack/react-start-client Patch
@tanstack/react-start-rsc Patch
@tanstack/react-start-server Patch
@tanstack/react-start Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@coderabbitai

coderabbitai Bot commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

createLink renders its supplied host directly through useLinkProps and memoizes the component. Link now aliases createLink('a'). The changeset declares a patch release for @tanstack/react-router.

Changes: React link rendering

Layer / File(s) Summary
Direct host rendering and Link alias
packages/react-router/src/link.tsx, .changeset/react-create-link-single-layer.md
createLink renders its supplied host through useLinkProps and uses areLinkPropsEqual for memoization. _asChild is removed from the router-option exclusion set. Link aliases createLink('a'), and the changeset declares a patch release.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Refactor

Possibly related PRs

  • TanStack/router#9010: Adds a client-navigation benchmark for custom createLink links, which measures the behavior changed here.
  • TanStack/router#9005: Changes the same Link render path in link.tsx, so the overlapping implementation changes need reconciliation.

Suggested labels: package: react-router

Suggested reviewers: sheraff

Merge Risk: Minimal · up to f651b

This change reduces the number of React fibers used when rendering custom links and keeps the existing link behavior. No actionable merge-blocking risk was identified.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the main change: createLink components now render in one layer instead of wrapping Link. The changeset summary and the PR objectives both describe this change. The 'perf' prefix matche…
Description check Passed The description follows the repository template. It includes the Changes section with motivation and code examples, the checklist, and the release impact section with the changeset box checked. It als…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title names the main change: createLink components now render in one layer instead of wrapping Link. The changeset summary and the PR objectives both describe this change. The 'perf' prefix matches the stated goal of fewer fibers and less work per render.

Full details: Description check

Explanation

The description follows the repository template. It includes the Changes section with motivation and code examples, the checklist, and the release impact section with the changeset box checked. It also discloses AI authorship and gives validation steps and benchmark results. The template's own AI-authored rules ask for terse wording. The description is long, but the required content is present, and the author lists this PR as a changeset-backed release.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between fe20a62 and f651b05.

Files selected for processing: 2

  • .changeset/react-create-link-single-layer.md
  • packages/react-router/src/link.tsx

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 7 remain after this review.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 27, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
🔒 Security Review ✅ Completed 2026-11-27T18:08:18.074408Z f651b05 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@nx-cloud

nx-cloud Bot commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

View your CI Pipeline Execution ↗ for commit f651b05

Command Status Duration Result
nx affected --targets=test:eslint,test:unit,tes... ✅ Succeeded 10m 55s View ↗
nx run-many --target=build --exclude=examples/*... ✅ Succeeded 6s View ↗

💡 With Ultracache enabled, Nx Cloud learns each task's file inputs in CI and uses them on later runs. Turn on Ultracache in workspace settings ↗ · Read docs ↗


☁️ Nx Cloud last updated this comment at 2026-11-27 18:17:16 UTC

@github-actions

Copy link
Copy Markdown
Contributor

🚀 Changeset Version Preview

1 package(s) bumped directly, 4 bumped as dependents.

🟩 Patch bumps

Package Version Reason
@tanstack/react-router 1.170.46 → 1.170.47 Changeset
@tanstack/react-start 1.169.7 → 1.169.8 Dependent
@tanstack/react-start-client 1.168.45 → 1.168.46 Dependent
@tanstack/react-start-rsc 0.1.68 → 0.1.69 Dependent
@tanstack/react-start-server 1.167.52 → 1.167.53 Dependent

@pkg-pr-new

pkg-pr-new Bot commented Nov 27, 2026

Copy link
Copy Markdown
More templates

@tanstack/arktype-adapter

npm i https://pkg.pr.new/@tanstack/arktype-adapter@9013

@tanstack/eslint-plugin-router

npm i https://pkg.pr.new/@tanstack/eslint-plugin-router@9013

@tanstack/eslint-plugin-start

npm i https://pkg.pr.new/@tanstack/eslint-plugin-start@9013

@tanstack/history

npm i https://pkg.pr.new/@tanstack/history@9013

@tanstack/react-router

npm i https://pkg.pr.new/@tanstack/react-router@9013

@tanstack/react-router-devtools

npm i https://pkg.pr.new/@tanstack/react-router-devtools@9013

@tanstack/react-router-ssr-query

npm i https://pkg.pr.new/@tanstack/react-router-ssr-query@9013

@tanstack/react-start

npm i https://pkg.pr.new/@tanstack/react-start@9013

@tanstack/react-start-client

npm i https://pkg.pr.new/@tanstack/react-start-client@9013

@tanstack/react-start-rsc

npm i https://pkg.pr.new/@tanstack/react-start-rsc@9013

@tanstack/react-start-server

npm i https://pkg.pr.new/@tanstack/react-start-server@9013

@tanstack/router-cli

npm i https://pkg.pr.new/@tanstack/router-cli@9013

@tanstack/router-core

npm i https://pkg.pr.new/@tanstack/router-core@9013

@tanstack/router-devtools

npm i https://pkg.pr.new/@tanstack/router-devtools@9013

@tanstack/router-devtools-core

npm i https://pkg.pr.new/@tanstack/router-devtools-core@9013

@tanstack/router-generator

npm i https://pkg.pr.new/@tanstack/router-generator@9013

@tanstack/router-plugin

npm i https://pkg.pr.new/@tanstack/router-plugin@9013

@tanstack/router-ssr-query-core

npm i https://pkg.pr.new/@tanstack/router-ssr-query-core@9013

@tanstack/router-utils

npm i https://pkg.pr.new/@tanstack/router-utils@9013

@tanstack/router-vite-plugin

npm i https://pkg.pr.new/@tanstack/router-vite-plugin@9013

@tanstack/solid-router

npm i https://pkg.pr.new/@tanstack/solid-router@9013

@tanstack/solid-router-devtools

npm i https://pkg.pr.new/@tanstack/solid-router-devtools@9013

@tanstack/solid-router-ssr-query

npm i https://pkg.pr.new/@tanstack/solid-router-ssr-query@9013

@tanstack/solid-start

npm i https://pkg.pr.new/@tanstack/solid-start@9013

@tanstack/solid-start-client

npm i https://pkg.pr.new/@tanstack/solid-start-client@9013

@tanstack/solid-start-server

npm i https://pkg.pr.new/@tanstack/solid-start-server@9013

@tanstack/start-client-core

npm i https://pkg.pr.new/@tanstack/start-client-core@9013

@tanstack/start-fn-stubs

npm i https://pkg.pr.new/@tanstack/start-fn-stubs@9013

@tanstack/start-plugin-core

npm i https://pkg.pr.new/@tanstack/start-plugin-core@9013

@tanstack/start-server-core

npm i https://pkg.pr.new/@tanstack/start-server-core@9013

@tanstack/start-static-server-functions

npm i https://pkg.pr.new/@tanstack/start-static-server-functions@9013

@tanstack/start-storage-context

npm i https://pkg.pr.new/@tanstack/start-storage-context@9013

@tanstack/valibot-adapter

npm i https://pkg.pr.new/@tanstack/valibot-adapter@9013

@tanstack/virtual-file-routes

npm i https://pkg.pr.new/@tanstack/virtual-file-routes@9013

@tanstack/vue-router

npm i https://pkg.pr.new/@tanstack/vue-router@9013

@tanstack/vue-router-devtools

npm i https://pkg.pr.new/@tanstack/vue-router-devtools@9013

@tanstack/vue-router-ssr-query

npm i https://pkg.pr.new/@tanstack/vue-router-ssr-query@9013

@tanstack/vue-start

npm i https://pkg.pr.new/@tanstack/vue-start@9013

@tanstack/vue-start-client

npm i https://pkg.pr.new/@tanstack/vue-start-client@9013

@tanstack/vue-start-server

npm i https://pkg.pr.new/@tanstack/vue-start-server@9013

@tanstack/zod-adapter

npm i https://pkg.pr.new/@tanstack/zod-adapter@9013

commit: f651b05

@github-actions

Copy link
Copy Markdown
Contributor

Bundle Size Benchmarks

  • Commit: 381ea79cb9a4
  • Measured at: 2026-11-27T18:12:20.407Z
  • Baseline source: history:10b22c276ea7
  • Dashboard: bundle-size history

The following scenarios have bundle-size changes compared with the baseline:

Scenario Current (gzip) Initial (gzip) Raw Brotli Trend
react-router.minimal 84.0 KiB
-7 B
83.9 KiB
-6 B
261.5 KiB
-29 B
73.3 KiB
+6 B
▂▂▂▂▂▁▁████▅
react-router.full 87.6 KiB
-21 B
87.4 KiB
-22 B
273.3 KiB
-114 B
76.4 KiB
-16 B
▁▁▁▁▁▁▁████▂
react-start.minimal 96.8 KiB
-5 B
96.7 KiB
-2 B
302.4 KiB
-23 B
84.0 KiB
-162 B
▁▁▁▁▁▁▁████▅
react-start.query-integration 104.2 KiB
-7 B
104.1 KiB
-6 B
329.0 KiB
-23 B
90.5 KiB
+32 B
▃▃▃▃▃▁▁████▄
react-start.deferred-hydration 97.5 KiB
-4 B
96.7 KiB
-4 B
303.8 KiB
-23 B
84.7 KiB
-120 B
▁▁▁▁▁▂▂████▇
react-start.full 99.8 KiB
-28 B
99.7 KiB
-26 B
311.6 KiB
-114 B
86.7 KiB
+44 B
▅▅▅▅▅▅▅████▁
react-start.rsbuild.minimal 96.6 KiB
-48 B
96.5 KiB
-48 B
298.2 KiB
-84 B
83.5 KiB
+59 B
██▁▁▁▁▁▁▁▁▁▁
react-start.rsbuild.minimal-iife 97.0 KiB
-45 B
96.9 KiB
-45 B
299.2 KiB
-84 B
83.7 KiB
-86 B
██▁▁▁▁▁▁▁▁▁▁
react-start.rsbuild.full 100.0 KiB
-14 B
99.8 KiB
-14 B
307.9 KiB
-73 B
86.2 KiB
+145 B
██▁▁▁▁▁▁▁▁▁▁

Current gzip tracks all emitted client JS chunks. Initial gzip tracks only the entry/import graph. Trend sparkline is historical current gzip ending with this PR measurement; lower is better.

@codspeed

codspeed Bot commented Nov 27, 2026

Copy link
Copy Markdown

Merging this PR will regress 1 benchmark

2 warnings, 1 note

⚠️ 54 benchmarks are dominated by system calls

System calls cannot be simulated consistently and are left out of the measure. The reported value understates the real cost. Switch to the Walltime instrument to measure them.

Learn more about system calls

See affected benchmarks

⚠️ 1 benchmark with a significant change ran on different hardware

Its base and head results were measured on machines with a different CPU, core count or memory, so the change might not come from the code. Run base and head on the same runner type.

Learn more about Macro Runners

See affected benchmarks

ℹ️ 20 benchmarks have no flamegraph

The flamegraph wasn't generated: most of the time was spent in runtime internals, which would leave nothing useful to analyze.

See affected benchmarks

⚡ 1 improved benchmark
❌ 1 regressed benchmark
✅ 201 untouched benchmarks
⏩ 3 skipped benchmarks1

Warning

Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Mode Benchmark BASE HEAD Efficiency
❌ Memory mem server error-paths redirect (solid) 11 KB 11.5 KB -4.48%
⚡ CPU Simulation client-create-link loop (react) 222.5 ms 212.5 ms +4.7%

Tip

Investigate this regression by commenting @codspeedbot fix this regression on this PR, or directly use the CodSpeed MCP with your agent.


Comparing schiller-manuel-create-link-single-layer-e87 (f651b05) with schiller-manuel-create-link-benchmark (fe20a62)

Open in CodSpeed

Footnotes

  1. 3 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant