Skip to content

perf(router-core): optimize route tree processing - #7984

Merged
Sheraff merged 1 commit into
mainfrom
codex/consolidate-route-tree-optimizations
Aug 6, 2026
Merged

perf(router-core): optimize route tree processing#7984
Sheraff merged 1 commit into
mainfrom
codex/consolidate-route-tree-optimizations

Conversation

@Sheraff

@Sheraff Sheraff commented Aug 6, 2026

Copy link
Copy Markdown
Collaborator

Summary

  • fuse static and dynamic route-node construction while preserving case sensitivity, parser reuse, wildcard aliases, parent links, and trie depth
  • record dynamic sibling arrays only when they first need sorting, then sort them once after construction instead of walking the full trie
  • derive matcher frame depth from trie nodes instead of carrying duplicate depth state
  • consolidate and expand regression coverage for sibling ordering, parser priorities and fallback, static-node reuse, route masks, case sensitivity, and pathless/optional depth
  • add a patch changeset for @tanstack/router-core

Consolidates

This replaces the overlapping work in:

#7974's fused dynamic-node implementation subsumes #7969's production sorting change; #7969's unique ordering tests are retained.

Runtime performance

Compared with origin/main at 697ebb6d using the same focused Vitest benchmark files in separate worktrees:

  • general route-tree construction: 7.6% to 10.7% faster across mostly-static, dense-dynamic, reused-shape, and route-mask cases
  • static construction: 3.5% to 14.1% faster across sensitive/insensitive and shared/unique-prefix cases
  • route matching: 5.4% to 8.4% faster across deep-static and optional-segment distributions

No focused runtime case regressed.

Validation

  • router-core unit: 105 files passed; 1,536 tests passed; 3 expected failures
  • router-core types: TypeScript 5.6 through 7.0 passed
  • router-core ESLint: 0 errors (26 existing warnings outside this diff)
  • focused construction/static/matching benchmarks compared against origin/main
  • Prettier, git diff --check, and changeset status passed
  • five independent reviews found no correctness or runtime-performance blocker

Summary by CodeRabbit

  • Performance

    • Improved route-tree construction and route matching performance.
    • Enhanced prioritization of dynamic, optional, and wildcard routes for more accurate matching.
  • Bug Fixes

    • Improved handling of case-insensitive paths, pathless routes, route masks, and fallback matching.
    • Prevented route ordering issues when multiple routes have similar patterns.
  • Tests

    • Expanded coverage for route specificity, node reuse, wildcard handling, and route-mask behavior.

@coderabbitai

coderabbitai Bot commented Aug 6, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

Route-tree construction now resolves route-local options, reuses trie nodes, and sorts only newly populated dynamic sibling lists. Matching derives depth from trie nodes. Tests cover route and route-mask specificity, fallback, reuse, case handling, and pathless routes.

Changes

Route tree optimization

Layer / File(s) Summary
Route parsing and selective sibling sorting
packages/router-core/src/new-process-route-tree.ts, .changeset/faster-route-trees.md
Route parsing groups dynamic, optional, and wildcard nodes into recorded sibling lists. Construction sorts only those lists instead of recursively sorting the full tree.
Trie-derived matcher specificity
packages/router-core/src/new-process-route-tree.ts
Match frames no longer store depth. Optional skip masks and specificity comparisons derive depth from trie nodes.
Route and mask matching coverage
packages/router-core/tests/new-process-route-tree.test.ts
Tests cover specificity ordering, parser fallback, node reuse, case handling, pathless tie-breaking, and route-mask matching.

Estimated code review effort: 4 (Complex) | ~45 minutes

Sequence Diagram(s)

sequenceDiagram
  participant RouteParser
  participant Trie
  participant RouteMatcher
  RouteParser->>Trie: Build nodes from resolved route paths
  Trie-->>RouteParser: Record dynamic sibling lists
  RouteParser->>Trie: Sort recorded lists
  RouteMatcher->>Trie: Read node depth during matching
  Trie-->>RouteMatcher: Return trie-derived specificity
Loading

Possibly related PRs

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the main change: optimizing route tree processing in router-core.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
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.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch codex/consolidate-route-tree-optimizations

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

Comment @coderabbitai help to get the list of available commands.

@Sheraff
Sheraff marked this pull request as ready for review August 6, 2026 10:46
@github-actions

github-actions Bot commented Aug 6, 2026

Copy link
Copy Markdown
Contributor

🚀 Changeset Version Preview

5 package(s) bumped directly, 18 bumped as dependents.

🟩 Patch bumps

Package Version Reason
@tanstack/react-router 1.170.20 → 1.170.21 Changeset
@tanstack/router-core 1.171.17 → 1.171.18 Changeset
@tanstack/solid-router 1.170.20 → 1.170.21 Changeset
@tanstack/start-server-core 1.169.20 → 1.169.21 Changeset
@tanstack/vue-router 1.170.19 → 1.170.20 Changeset
@tanstack/react-start 1.168.37 → 1.168.38 Dependent
@tanstack/react-start-client 1.168.18 → 1.168.19 Dependent
@tanstack/react-start-rsc 0.1.36 → 0.1.37 Dependent
@tanstack/react-start-server 1.167.25 → 1.167.26 Dependent
@tanstack/router-cli 1.167.23 → 1.167.24 Dependent
@tanstack/router-generator 1.167.23 → 1.167.24 Dependent
@tanstack/router-plugin 1.168.25 → 1.168.26 Dependent
@tanstack/router-vite-plugin 1.167.25 → 1.167.26 Dependent
@tanstack/solid-start 1.168.36 → 1.168.37 Dependent
@tanstack/solid-start-client 1.168.18 → 1.168.19 Dependent
@tanstack/solid-start-server 1.167.25 → 1.167.26 Dependent
@tanstack/start-client-core 1.170.16 → 1.170.17 Dependent
@tanstack/start-plugin-core 1.171.28 → 1.171.29 Dependent
@tanstack/start-static-server-functions 1.167.21 → 1.167.22 Dependent
@tanstack/start-storage-context 1.167.19 → 1.167.20 Dependent
@tanstack/vue-start 1.168.35 → 1.168.36 Dependent
@tanstack/vue-start-client 1.167.21 → 1.167.22 Dependent
@tanstack/vue-start-server 1.167.25 → 1.167.26 Dependent

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
packages/router-core/src/new-process-route-tree.ts (1)

262-305: 🩺 Stability & Availability | 🟡 Minor | ⚡ Quick win

Pass dynamicListsToSort through findSingleMatch.

parseSegments is not exported, but findSingleMatch calls it without dynamicListsToSort. A single top-level flat route containing two or more sibling dynamic segments can remain unsorted, so matches may pick lower-priority siblings before declaration order is restored.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@packages/router-core/src/new-process-route-tree.ts` around lines 262 - 305,
Update findSingleMatch to accept and pass dynamicListsToSort into its
parseSegments call, preserving the existing sorting behavior for sibling dynamic
segments in top-level flat routes.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@packages/router-core/src/new-process-route-tree.ts`:
- Around line 1092-1096: The skipped-bit mask in extractParams is keyed by trie
depth, while buildBranch/nodeIndex consume it by buildBranch order. Align the
producer and consumer by using one consistent key: preferably record skipped
optional nodes in buildBranch order and update nodeIndex accordingly, or change
both sides to depth-based indexing; ensure skipping behavior remains correct
across optional traversal.

---

Outside diff comments:
In `@packages/router-core/src/new-process-route-tree.ts`:
- Around line 262-305: Update findSingleMatch to accept and pass
dynamicListsToSort into its parseSegments call, preserving the existing sorting
behavior for sibling dynamic segments in top-level flat routes.
🪄 Autofix

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 64521571-feff-4e63-b35e-f9342c940fb2

📥 Commits

Reviewing files that changed from the base of the PR and between 697ebb6 and fb90b1e.

📒 Files selected for processing (3)
  • .changeset/faster-route-trees.md
  • packages/router-core/src/new-process-route-tree.ts
  • packages/router-core/tests/new-process-route-tree.test.ts

Comment thread packages/router-core/src/new-process-route-tree.ts
@nx-cloud

nx-cloud Bot commented Aug 6, 2026

Copy link
Copy Markdown
Contributor

View your CI Pipeline Execution ↗ for commit fb90b1e

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

☁️ Nx Cloud last updated this comment at 2026-08-06 11:06:15 UTC

@github-actions

github-actions Bot commented Aug 6, 2026

Copy link
Copy Markdown
Contributor

Bundle Size Benchmarks

  • Commit: 9bfeb59af84f
  • Measured at: 2026-08-06T10:51:47.156Z
  • Baseline source: history:697ebb6ddbd4
  • Dashboard: bundle-size history

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

Scenario Current (gzip) Delta vs baseline Initial gzip Raw Brotli Trend
react-router.minimal 87.00 KiB -112 B (-0.13%) 86.86 KiB 268.05 KiB 75.67 KiB ████▃▃▃▃▃▃▃▁
react-router.full 90.76 KiB -143 B (-0.15%) 90.62 KiB 280.00 KiB 79.04 KiB ████▅▅▅▅▅▄▄▁
solid-router.minimal 34.67 KiB -129 B (-0.36%) 34.55 KiB 98.88 KiB 31.35 KiB ████▂▂▂▂▂▂▂▁
solid-router.full 39.76 KiB -139 B (-0.34%) 39.64 KiB 114.06 KiB 35.84 KiB ████▃▃▃▃▃▂▂▁
vue-router.minimal 51.96 KiB -135 B (-0.25%) 51.83 KiB 142.04 KiB 46.78 KiB ████▂▂▂▂▂▂▂▁
vue-router.full 57.90 KiB -159 B (-0.27%) 57.77 KiB 160.71 KiB 51.95 KiB ████▂▂▂▂▂▂▂▁
react-start.minimal 100.47 KiB -132 B (-0.13%) 100.33 KiB 311.19 KiB 87.24 KiB ███████▂▂▂▂▁
react-start.deferred-hydration 101.21 KiB -131 B (-0.13%) 100.35 KiB 312.57 KiB 87.99 KiB ███████▂▂▂▂▁
react-start.full 103.80 KiB -125 B (-0.12%) 103.66 KiB 321.14 KiB 90.08 KiB ███████▂▂▁▂▁
react-start.rsbuild.minimal 100.04 KiB -197 B (-0.19%) 99.87 KiB 315.69 KiB 86.32 KiB ▁▁▁▁▂▂▂████▅
react-start.rsbuild.minimal-iife 100.46 KiB -193 B (-0.19%) 100.29 KiB 316.63 KiB 86.63 KiB ▁▁▁▁▂▂▂████▆
react-start.rsbuild.full 103.37 KiB -208 B (-0.20%) 103.19 KiB 325.78 KiB 89.05 KiB ▁▁▁▁▃▃▃████▆
solid-start.minimal 48.03 KiB -152 B (-0.31%) 47.90 KiB 141.02 KiB 42.72 KiB ███████▂▂▂▂▁
solid-start.deferred-hydration 51.30 KiB -147 B (-0.28%) 47.96 KiB 149.06 KiB 45.70 KiB ███████▂▂▂▂▁
solid-start.full 53.30 KiB -138 B (-0.25%) 53.17 KiB 156.75 KiB 47.24 KiB ████▆▆▆▂▂▁▁▁
vue-start.minimal 68.81 KiB -168 B (-0.24%) 68.68 KiB 194.03 KiB 61.21 KiB ████▆▆▆▂▂▂▂▁
vue-start.full 72.79 KiB -140 B (-0.19%) 72.66 KiB 206.53 KiB 64.67 KiB ████▆▆▆▂▂▁▁▁

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.

@pkg-pr-new

pkg-pr-new Bot commented Aug 6, 2026

Copy link
Copy Markdown
More templates

@tanstack/arktype-adapter

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

@tanstack/eslint-plugin-router

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

@tanstack/eslint-plugin-start

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

@tanstack/history

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

@tanstack/nitro-v2-vite-plugin

npm i https://pkg.pr.new/@tanstack/nitro-v2-vite-plugin@7984

@tanstack/react-router

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

@tanstack/react-router-devtools

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

@tanstack/react-router-ssr-query

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

@tanstack/react-start

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

@tanstack/react-start-client

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

@tanstack/react-start-rsc

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

@tanstack/react-start-server

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

@tanstack/router-cli

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

@tanstack/router-core

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

@tanstack/router-devtools

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

@tanstack/router-devtools-core

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

@tanstack/router-generator

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

@tanstack/router-plugin

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

@tanstack/router-ssr-query-core

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

@tanstack/router-utils

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

@tanstack/router-vite-plugin

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

@tanstack/solid-router

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

@tanstack/solid-router-devtools

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

@tanstack/solid-router-ssr-query

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

@tanstack/solid-start

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

@tanstack/solid-start-client

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

@tanstack/solid-start-server

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

@tanstack/start-client-core

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

@tanstack/start-fn-stubs

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

@tanstack/start-plugin-core

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

@tanstack/start-server-core

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

@tanstack/start-static-server-functions

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

@tanstack/start-storage-context

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

@tanstack/valibot-adapter

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

@tanstack/virtual-file-routes

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

@tanstack/vue-router

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

@tanstack/vue-router-devtools

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

@tanstack/vue-router-ssr-query

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

@tanstack/vue-start

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

@tanstack/vue-start-client

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

@tanstack/vue-start-server

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

@tanstack/zod-adapter

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

commit: fb90b1e

@codspeed-hq

codspeed-hq Bot commented Aug 6, 2026

Copy link
Copy Markdown

Merging this PR will regress 0 benchmarks

⚠️ Different runtime environments detected

Some benchmarks with significant performance changes were compared across different runtime environments,
which may affect the accuracy of the results.

Open the report in CodSpeed to investigate

⚡ 9 improved benchmarks
❌ 4 (👁 4) regressed benchmarks
✅ 167 untouched benchmarks

Performance Changes

Mode Benchmark BASE HEAD Efficiency
Memory mem server server-fn-churn (vue) 4,147.3 KB 263.1 KB ×16
Memory mem server error-paths unmatched (vue) 2,142.1 KB 479.4 KB ×4.5
Memory mem server peak-large-page (react) 1,737.5 KB 953.4 KB +82.23%
Memory mem server error-paths redirect (solid) 388 KB 278.2 KB +39.49%
Memory mem server error-paths redirect (vue) 338.6 KB 293.5 KB +15.37%
Memory mem server error-paths not-found (react) 277.7 KB 255.5 KB +8.66%
Simulation ssr server-fn multipart (solid) 64.2 ms 61.3 ms +4.64%
Simulation ssr server-fn raw-stream (solid) 113.2 ms 108.5 ms +4.3%
Simulation ssr control-flow unmatched 404 (react) 58.1 ms 56.4 ms +3%
👁 Memory mem server error-paths not-found (solid) 415.1 KB 789.1 KB -47.4%
👁 Simulation ssr server-fn during document ssr (react) 62.9 ms 65.9 ms -4.62%
👁 Memory mem server error-paths redirect (react) 197 KB 203.7 KB -3.31%
👁 Memory mem server error-paths unmatched (react) 317.5 KB 492.1 KB -35.49%

Tip

Curious why this is faster? Comment @codspeedbot explain why this is faster on this PR, or directly use the CodSpeed MCP with your agent.


Comparing codex/consolidate-route-tree-optimizations (fb90b1e) with main (697ebb6)

Open in CodSpeed

@Sheraff
Sheraff merged commit 84db4a8 into main Aug 6, 2026
26 checks passed
@Sheraff
Sheraff deleted the codex/consolidate-route-tree-optimizations branch August 6, 2026 11:37
@github-actions github-actions Bot mentioned this pull request Aug 6, 2026
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