Skip to content

perf(history): avoid temporary arrays in memory history - #8352

Open
LadyBluenotes wants to merge 1 commit into
mainfrom
perf/history-truncate-forward-entries
Open

perf(history): avoid temporary arrays in memory history#8352
LadyBluenotes wants to merge 1 commit into
mainfrom
perf/history-truncate-forward-entries

Conversation

@LadyBluenotes

@LadyBluenotes LadyBluenotes commented Sep 11, 2026

Copy link
Copy Markdown
Member

🎯 Changes

Write the next memory-history URL and state directly at the destination index, then truncate any remaining forward entries. This avoids the two unused arrays returned by splice when a push creates a new branch. Backward/forward navigation and retained state stay unchanged.

Performance

Local Node.js 24.12.0 comparison of 6494e75362 and ea0bcb8690. Allocation rows are medians of three heap-sampling profiles with a 4,096-byte sampling interval; history setup is outside the measurement window.

Workload Before After Change
100,000 one-entry branches, sampled allocation bytes 174,676,312 163,961,904 -6.1%
4,096 pushes discarding 64 forward entries each, sampled allocation bytes 9,232,904 4,443,224 -51.9%
Standalone memory-history bundle, gzip 1,225 bytes 1,220 bytes -5 bytes

These allocation estimates include objects collected by GC; they are not retained-heap measurements. The standalone bundle uses Vite 8.0.14, minified ESM, es2022, and gzip level 9. Browser-history-only output is byte-identical.

Longer ordinary-push measurements were effectively unchanged: median elapsed time was 80.392 ms before and 80.315 ms after; CPU time was 100.221 ms before and 100.421 ms after, across nine counterbalanced samples of 150,000 pushes. This PR claims allocation savings, not a stable navigation-speed improvement.

✅ 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.

🚀 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
    • Improved memory-history behavior when navigating back and creating a new entry, correctly removing forward entries while preserving the active history.
    • Back and forward navigation now maintains the expected entries and state.

@coderabbitai

coderabbitai Bot commented Sep 11, 2026

Copy link
Copy Markdown
Contributor

Review Change StackReview Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Advanced

Run ID: c56dc9d1-7262-40be-a1a7-f998b5bddcd5

📥 Commits

Reviewing files that changed from the base of the PR and between 6494e75 and ea0bcb8.

📒 Files selected for processing (3)
  • .changeset/history-forward-entries.md
  • packages/history/src/index.ts
  • packages/history/tests/createMemoryHistory.test.ts

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


📝 Walkthrough

Walkthrough

Memory history now replaces forward entries without temporary arrays. A regression test verifies branch truncation, state retention, and navigation. A patch changeset records the update.

Changes

Memory history updates

Layer / File(s) Summary
Forward branch replacement and validation
packages/history/src/index.ts, packages/history/tests/createMemoryHistory.test.ts, .changeset/history-forward-entries.md
createMemoryHistory writes new entries directly and truncates forward history. Tests verify discarded entries, retained states, and navigation. The changeset declares a patch release.

Priority: ⬇️ Low

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

Suggested reviewers: sheraff

Merge Risk: ⚪ Minimal · up to ea0bc

The optimized memory-history branch replacement preserves the tested navigation and state behavior, with no current merge-blocking risk identified.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description check ✅ Passed The description follows the repository template. It explains the change and motivation, reports performance measurements, completes the checklist, and identifies the generated changeset for the publis…
Title check ✅ Passed The title clearly and concisely identifies the main change: avoiding temporary arrays in memory history. It matches the implementation and performance objective.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 1 functions across 2 files. (1 skipped: 1 …
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 perf/history-truncate-forward-entries

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.

@nx-cloud

nx-cloud Bot commented Sep 11, 2026

Copy link
Copy Markdown
Contributor

View your CI Pipeline Execution ↗ for commit ea0bcb8

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

☁️ Nx Cloud last updated this comment at 2026-09-11 03:14:38 UTC

@github-actions

Copy link
Copy Markdown
Contributor

🚀 Changeset Version Preview

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

🟩 Patch bumps

Package Version Reason
@tanstack/history 1.162.3 → 1.162.4 Changeset
@tanstack/react-router 1.170.35 → 1.170.36 Dependent
@tanstack/react-start 1.168.52 → 1.168.53 Dependent
@tanstack/react-start-client 1.168.33 → 1.168.34 Dependent
@tanstack/react-start-rsc 0.1.51 → 0.1.52 Dependent
@tanstack/react-start-server 1.167.40 → 1.167.41 Dependent
@tanstack/router-cli 1.167.35 → 1.167.36 Dependent
@tanstack/router-core 1.171.29 → 1.171.30 Dependent
@tanstack/router-generator 1.167.35 → 1.167.36 Dependent
@tanstack/router-plugin 1.168.37 → 1.168.38 Dependent
@tanstack/router-vite-plugin 1.167.37 → 1.167.38 Dependent
@tanstack/solid-router 1.170.33 → 1.170.34 Dependent
@tanstack/solid-start 1.168.50 → 1.168.51 Dependent
@tanstack/solid-start-client 1.168.32 → 1.168.33 Dependent
@tanstack/solid-start-server 1.167.39 → 1.167.40 Dependent
@tanstack/start-client-core 1.170.29 → 1.170.30 Dependent
@tanstack/start-plugin-core 1.171.42 → 1.171.43 Dependent
@tanstack/start-server-core 1.169.34 → 1.169.35 Dependent
@tanstack/start-static-server-functions 1.167.34 → 1.167.35 Dependent
@tanstack/start-storage-context 1.167.31 → 1.167.32 Dependent
@tanstack/vue-router 1.170.32 → 1.170.33 Dependent
@tanstack/vue-start 1.168.49 → 1.168.50 Dependent
@tanstack/vue-start-client 1.167.35 → 1.167.36 Dependent
@tanstack/vue-start-server 1.167.39 → 1.167.40 Dependent

@github-actions

Copy link
Copy Markdown
Contributor

Bundle Size Benchmarks

This pull request does not affect bundle size in any measured scenario.

@pkg-pr-new

pkg-pr-new Bot commented Sep 11, 2026

Copy link
Copy Markdown
More templates

@tanstack/arktype-adapter

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

@tanstack/eslint-plugin-router

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

@tanstack/eslint-plugin-start

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

@tanstack/history

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

@tanstack/nitro-v2-vite-plugin

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

@tanstack/react-router

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

@tanstack/react-router-devtools

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

@tanstack/react-router-ssr-query

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

@tanstack/react-start

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

@tanstack/react-start-client

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

@tanstack/react-start-rsc

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

@tanstack/react-start-server

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

@tanstack/router-cli

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

@tanstack/router-core

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

@tanstack/router-devtools

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

@tanstack/router-devtools-core

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

@tanstack/router-generator

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

@tanstack/router-plugin

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

@tanstack/router-ssr-query-core

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

@tanstack/router-utils

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

@tanstack/router-vite-plugin

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

@tanstack/solid-router

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

@tanstack/solid-router-devtools

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

@tanstack/solid-router-ssr-query

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

@tanstack/solid-start

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

@tanstack/solid-start-client

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

@tanstack/solid-start-server

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

@tanstack/start-client-core

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

@tanstack/start-fn-stubs

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

@tanstack/start-plugin-core

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

@tanstack/start-server-core

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

@tanstack/start-static-server-functions

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

@tanstack/start-storage-context

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

@tanstack/valibot-adapter

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

@tanstack/virtual-file-routes

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

@tanstack/vue-router

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

@tanstack/vue-router-devtools

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

@tanstack/vue-router-ssr-query

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

@tanstack/vue-start

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

@tanstack/vue-start-client

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

@tanstack/vue-start-server

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

@tanstack/zod-adapter

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

commit: ea0bcb8

@codspeed-hq

codspeed-hq Bot commented Sep 11, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 180 untouched benchmarks


Comparing perf/history-truncate-forward-entries (ea0bcb8) with main (6494e75)

Open in CodSpeed

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.

2 participants