Repository navigation
perf(router-core): keep location-independent buildLocation results on the options object - #9003
schiller-manuel wants to merge 2 commits into
Conversation
… the options object Links rebuild their href from a stable options object. The router cached locations that don't read the current location in a WeakMap keyed by that object. Store the entry on the options object itself instead, tagged with its owner (so spread copies miss) and the router's lightweightCache (whose replacement on route-tree and option updates invalidates every entry). Also trims buildLocation so the client bundles don't grow: one helper resolves the `true`/updater/literal search, hash and state options, the route-mask path stops copying cached match params that nothing mutates, and the search validation loop drops its callback. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
🦋 Changeset detectedLatest commit: 2c725bd The changes in this PR will be included in the next version bump. This PR includes changesets to release 23 packages
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 |
|
No actionable comments were generated in the recent review. Walkthrough
Merge Risk:
|
|
View your CI Pipeline Execution ↗ for commit 2c725bd
💡 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 |
Bundle Size Benchmarks
The following scenarios have bundle-size changes compared with the baseline:
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. |
🚀 Changeset Version Preview1 package(s) bumped directly, 22 bumped as dependents. 🟩 Patch bumps
|
| @@ -1963,7 +1983,7 @@ export class RouterCore< | |||
| to[0] === '/' | |||
| ? '' | |||
| : dest.unsafeRelative === 'path' | |||
| ? current().pathname | |||
| ? current('pathname') | |||
There was a problem hiding this comment.
this change feels a bit unnecessary, why not keep current().pathname? does it save bytes?
There was a problem hiding this comment.
Reverted to current().pathname; the shared helper now takes a getter (() => current().hash / () => current().state) instead of a key. That turned out smaller too: gzip is now −12 to −54 B per bundle-size scenario (−548 B total, was −342 B).
Merging this PR will degrade performance by 1.46%2 warnings, 1 note
Warning Please fix the performance issues or acknowledge them on CodSpeed. Performance Changes
Tip Investigate this regression by commenting Comparing Footnotes
|
Keep `current()` argument-free and let callers read the hash or state from it inside a getter, so literal values still never read the current location. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Agent assessment
Self-reported assessment, not an independent review.
Assessed revision:
2c725bd8946102c3d3692bd7105eed536bf8d0datest:unit,test:typesandtest:eslintpass for router-core at this revision, and for React, Solid and Vue Router at01da715af0(the follow-up touches only router-core internals).↑ New tests cover spread copies and cross-router isolation; existing mask/cache tests cover invalidation on
router.update.↑ Read-only review of all invalidation paths (constructor,
update,setRoutes/HMR) and spread-copy sites found no issues.↓ e2e suites not run locally; CI and CodSpeed for this revision not inspected.
↓ Local machine was shared: same-commit spread was 7–31%, so per-case magnitudes are rough. CodSpeed numbers pending.
↓ Measured at
01da715af0; the follow-up getter refactor (two small closures per uncached build) was not re-measured locally.🎯 Changes
buildLocationcaches locations that don't read the current location (Links with a stable options object). The cache was a per-routerWeakMapkeyed by the options object. It now lives on the options object itself:{...opts, _includeValidateSearch: true}inbuildAndCommitLocation,matchRouteandnavigatemiss the cache.lightweightCache. It was already replaced insetRoutes, which HMR also calls. It is now also replaced inupdate()on the client, which is where the oldWeakMapwas reset, so invalidation is unchanged and the cache stays per-router._fromLocation, which are Link-owned or internal. The server is unchanged, becauseisServerstill folds both the read and the write away.To keep every client bundle at or below its current size, the PR also trims
buildLocationwithout changing behavior:resolveFromCurrent(value, read)helper resolves thetrue/updater/literal search, hash and state options.readis a getter (fromSearch,() => current().hash,() => current().state) that runs only fortrueor updaters, so literal values still never read the current location andusedCurrenttracking is unchanged.hashis normalized once (|| '') instead of|| undefinedand later?? ''.match.rawParams.resolveNextParamscopies before calling updaters, andbuild()copiesdest.params, so nothing mutates the cached object.Object.create(null)→createNull();forEach→for…ofinfromSearch.Benchmarks (
benchmarks/client-nav, local)Measured at
01da715af0, before the review follow-up. The baseline ismain, which includes #8999'slink-mountscenario. The candidate is this branch. Base and candidate builds were interleaved three times, and each cell is the median mean time per iteration. Lower is better. The machine was shared, so the same-commit spread column shows the noise. CodSpeed in CI now has a reallink-mountbaseline frommain's push run, so its report is the authoritative comparison.main(ms)Bundle size (
pnpm benchmark:bundle-size:run, all scenarios)gzip is smaller in every scenario: −12 to −54 B, total −548 B. Raw is −128 to −136 B in each scenario.
Per-scenario
react-router.fullreact-router.minimalreact-start.deferred-hydrationreact-start.fullreact-start.minimalreact-start.query-integrationreact-start.rsbuild.fullreact-start.rsbuild.minimalreact-start.rsbuild.minimal-iifesolid-router.fullsolid-router.minimalsolid-start.deferred-hydrationsolid-start.fullsolid-start.minimalvue-router.fullvue-router.minimalvue-start.fullvue-start.minimalValidation
CI=1 NX_DAEMON=false pnpm nx run-many -p @tanstack/router-core,@tanstack/react-router,@tanstack/solid-router,@tanstack/vue-router -t test:unit,test:types,test:eslint --skipRemoteCache: passed (17/17 tasks)pnpm format: no changesThis PR was written with AI assistance (GitHub Copilot).
✅ Checklist
🚀 Release Impact
Summary by CodeRabbit