Skip to content

Keep a large notes tree fast while typing - #264

Merged
BunsDev merged 6 commits into
mainfrom
notes-tree-render-cost
Oct 16, 2026
Merged

BunsDev merged 6 commits into
mainfrom
notes-tree-render-cost

Conversation

@BunsDev

@BunsDev BunsDev commented Oct 16, 2026

Copy link
Copy Markdown
Contributor

Part of #241 (finding 6: tree render cost grows with the square of the tree size, on every keystroke).

Problem

In NotesTree.tsx every row filtered all nodes for its children, built its Move to targets twice (each a scan for descendants plus a scan for targets), and named every target by walking its parents with tree.nodes.find. The Move to items were built even while the menu was closed. SpaceView re-renders on each page edit and passed a new pages array and a new actions object every time, so the whole tree re-rendered on every keystroke. The reviewer measured 1,056 nodes / 55 folders at 343 ms per render and 2,111 / 110 at 2.9 s.

Fix

  • space/tree-index.ts: indexTree(tree) builds id and parent→children maps in one pass; NotesTreeView memoizes it per tree. targetsFor keeps the same rule and order as before (a container that may hold the node, not its parent, not itself or anything under its path) and runs only when asked.
  • A row's menu builds its items only while it is open, so Move to lists cost nothing until a menu opens. A drag works out its drop targets once when it starts, not on every dragover.
  • The tree keeps its rows (useMemo over what they show: the index, page titles, selection, focus, the actions and the tree's own state). Page titles are compared by value, so editing a page body does not rebuild them. This does the job React.memo would, without rewriting the NotesTreeView signature line that the finding 9 change edits.
  • useTreeActions gives the tree one lasting actions object whose methods call Space's latest, so Space's re-renders do not invalidate the rows.

NotesTreeView's signature and the New workspace effect are unchanged, so this does not overlap the finding 9 change; the finding 8 code in SpaceView is untouched.

Before / after

Server-rendered with every menu closed, median of five, on a loaded Mac:

Tree Before After
1,051 nodes, 55 folders 547 ms 44 ms
2,101 nodes, 110 folders 3,362 ms 80 ms

On top of that, typing in a page body keeps the rows as they are, so a keystroke there does not redraw the tree at all. The new test cannot re-render (it renders on the server), so that part follows from the memoized rows and stable actions rather than from a measurement; the e2e specs below confirm the tree still behaves the same.

Regression test

apps/desktop/test/notes-tree-render.test.ts loads the real NotesTree.tsx through Vite's SSR loader (Vite and the React plugin are existing dev dependencies), renders the reviewer-sized trees with tree.nodes behind a proxy that counts element reads, and allows four reads per node. On the unmodified code it fails at once (1051 nodes: read more than 4204 nodes to render; an unbounded count is 94,126,002 reads, about 89,000 per node). After the fix a render reads each node once. The budget also catches Move to lists built for closed menus, since each one scans the nodes. The test checks too that every row renders, in tree order. A second test covers indexTree: children order, and Move to targets for a note, a folder (not its project, itself or its subfolder, but a sibling whose name starts the same), a project and a workspace.

Ran

  • pnpm build && pnpm test: all pass (desktop: 1,423 passed, 16 skipped, 0 failed; core, worktree scripts, site and store suites all pass)
  • pnpm --filter @modex/desktop typecheck: passes
  • pnpm exec playwright test e2e/notes-tree.spec.ts e2e/space.spec.ts e2e/notes.spec.ts e2e/notes-files.spec.ts e2e/space-fit.spec.ts: 32 passed; e2e/sidebar-workspaces.spec.ts (New workspace and Open notes focus through the tree): 2 passed

Coven-Harness: Claude Code / claude-opus-5-5

Each row of the Space notes tree scanned every node for its children,
built its Move to list twice, and named each target by walking its
parents with more scans, even with the menu closed. Space re-renders
the tree on every keystroke in a page, so typing paid for all of it.

The tree now indexes its nodes once per tree, builds a row's Move to
list only while its menu is open (and a drag's targets once per drag),
and keeps its rows while nothing they show changes. Space hands it one
lasting actions object, so editing a page body leaves the tree alone.

Server-rendered with every menu closed, median of five:
  1,051 nodes, 55 folders:  547 ms -> 44 ms
  2,101 nodes, 110 folders: 3,362 ms -> 80 ms

A new test renders the tree with its nodes behind a counting proxy.
Before, a render read each node about 89,000 times; now once.

Part of #241.

Coven-Harness: Claude Code / claude-opus-5-5
@vercel

vercel Bot commented Oct 16, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
modex Ready Ready Preview Oct 16, 2026 12:06pm UTC

Request Review

@github-code-quality

github-code-quality Bot commented Oct 16, 2026 •

Copy link
Copy Markdown

Code coverage overview

Languages: TypeScript

TypeScript / code-coverage-agent

main a60efbb notes-tree-render-co... 0d17745 +/-
61% 62% +1%
View coverage of the 10 most impacted files.
File main a60efbb notes-tree-render-co... 0d17745 +/-
apps/desktop/sr...owser-bridge.ts 0% 10% +10%
apps/desktop/sr...er/streamer.tsx 0% 21% +21%
apps/desktop/sr...nts/ui/Menu.tsx 0% 42% +42%
apps/desktop/sr...e/NotesTree.tsx 0% 51% +51%
apps/desktop/sr.../ui/Tooltip.tsx 0% 67% +67%
apps/desktop/sr...ents/ui/Kbd.tsx 0% 83% +83%
apps/desktop/sr...derer/bridge.ts 0% 94% +94%
apps/desktop/sr.../IconButton.tsx 0% 97% +97%
apps/desktop/sr...nts/ui/Icon.tsx 0% 99% +99%
apps/desktop/sr...e/tree-index.ts 0% 100% +100%

ℹ️ Calculations represent line coverage. Results may change as calculations are updated.


Updated October 16, 2026 12:27 UTC

@BunsDev
BunsDev merged commit ad90f09 into main Oct 16, 2026
15 of 16 checks passed
@BunsDev
BunsDev deleted the notes-tree-render-cost branch October 16, 2026 12:45
BunsDev added a commit that referenced this pull request Oct 19, 2026
… and the routines spec (#310)

v0.2.0 was published on 2026-10-18 (23:14Z) from the signed tag on 01b1b8a. This
follows Val's status layout (#290):

- Shipped gains a v0.2.0 row linking its release and its review.
- The 13 "On main, unreleased" items move unchanged to a new "Status summary"
  at the end of the v0.2.0 review, along with the old In flight entry. Each one
  was introduced by a commit in the tag (#248, #253, #254, #257 to #261, #263,
  #264, #266, #267, #269). The CF slices and #283's Markdown parser came after
  the tag, so they stay.
- In flight keeps a short follow-ups entry for the gates no receipt records yet:
  the independent download checks, the banner upgrade on the release Mac, and
  the Companion 0.2.0 (7) TestFlight upload.
- The routines spec and the README now say Phase 1, and most of Phase 0, ship
  from v0.2.0.

Unchanged: the website's static fallback (apps/site/release.json) and the
release evidence, which stay with the release owner. Claimed on #290.

Coven-Harness: Claude Code / claude-opus-5-5

This branch was successfully deployed

1 active deployment
Preview — 0d177459 Deployed Oct 16, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant