web: store.notes full recompute on every journal entry #1034

Open
opened 2026-11-09 14:02:22 +00:00 by kayg · 0 comments
Owner

Found while measuring the folder-card drop (#1030); read from the code only, nothing in apps/web was changed. Port item, not urgent at today's note counts, but it is O(notes x lists) work on every edit.

What the code does (apps/web/src/lib/notes.svelte.ts, lists.ts)

  • store.notes is a getter: return [...map.values()]. map is a SvelteMap, so every read (a) subscribes the reader to ANY set/delete on the map and (b) builds a brand-new array of every note. Nothing is memoized.
  • listNotes(key, filter) (lists.ts) starts from store.notes and filters it for journal, pinned and each tag; every list view, count and sidebar row that calls it is one such reader (the same shape is used by the tag index, search and mentions).
  • applyEntry (called for every journal entry about a note) does notesState.rev++ and then a map.delete (deleted) or, after 250 ms, refreshRecent() which re-reads the newest 50 notes and calls put() (a map.set) for each, then saveList(map.values()) (serialises ALL notes to storage). So one edit = up to 50 map writes + a full serialise.
  • Result: a single note edit wakes EVERY reader of store.notes (all lists, counts, sidebar rows), each of which copies the whole map and re-filters it, even though one note changed and only the lists it is in (or was in) can have a different answer. notesState.rev is read only by InfoSheet.svelte and Editor.svelte (the mentions), so the bump itself is cheap; the cost is the map iteration.

Measured analogue (mockup, #1030)

The mockup's folder lists had the same shape; the CPU profile of a move showed the Svelte reaction code at ~15 ms in Chromium. Replacing the full recompute with per-folder member sets (apps/mockup/src/lib/folders.svelte.ts, commit 199db43a1) made folder lists O(branch). In the mockup the larger cost turned out to be forced layout, so treat the gain here as scaling (2,000+ notes), not as a fix for a frame hitch.

Proposed fix

  1. Memoize the notes array: keep one array that is rebuilt only when a note is ADDED or REMOVED (and keep order), not when a note's fields change; expose per-note reactive fields (a $state object per note) so an edit changes that note's own signals only.
  2. Keep per-list member sets updated by applyEntry/put: journal (ids of daily notes), pinned, and tag -> Set<id> (with a parent-tag union computed from the sets of its branch). put compares the old and new note and touches only the sets whose membership changed (pin toggled, tags changed, kind changed); an edit that changes none of them wakes no list.
  3. listNotes then reads only its own set (SvelteSet / SvelteMap<tag, SvelteSet<id>>), maps ids to notes, and applies the text filter on that small set.
  4. refreshRecent: apply only items whose updated_ms changed, and write saveList once per burst, debounced to idle (it serialises every note today).
  5. Test: a unit test that edits one note's text and asserts the other lists' derived results are not recomputed (count the filter calls), plus a perf check with 2,000 notes (journal entry -> list update ms).

Related: #1030 (folder-card drop; profiling and the incremental folders store).

Found while measuring the folder-card drop (#1030); read from the code only, nothing in `apps/web` was changed. Port item, not urgent at today's note counts, but it is O(notes x lists) work on every edit. ## What the code does (`apps/web/src/lib/notes.svelte.ts`, `lists.ts`) - `store.notes` is a getter: `return [...map.values()]`. `map` is a `SvelteMap`, so every read (a) subscribes the reader to ANY set/delete on the map and (b) builds a brand-new array of every note. Nothing is memoized. - `listNotes(key, filter)` (lists.ts) starts from `store.notes` and filters it for journal, pinned and each tag; every list view, count and sidebar row that calls it is one such reader (the same shape is used by the tag index, search and mentions). - `applyEntry` (called for every journal entry about a note) does `notesState.rev++` and then a `map.delete` (deleted) or, after 250 ms, `refreshRecent()` which re-reads the newest 50 notes and calls `put()` (a `map.set`) for each, then `saveList(map.values())` (serialises ALL notes to storage). So one edit = up to 50 map writes + a full serialise. - Result: a single note edit wakes EVERY reader of `store.notes` (all lists, counts, sidebar rows), each of which copies the whole map and re-filters it, even though one note changed and only the lists it is in (or was in) can have a different answer. `notesState.rev` is read only by `InfoSheet.svelte` and `Editor.svelte` (the mentions), so the bump itself is cheap; the cost is the map iteration. ## Measured analogue (mockup, #1030) The mockup's folder lists had the same shape; the CPU profile of a move showed the Svelte reaction code at ~15 ms in Chromium. Replacing the full recompute with per-folder member sets (`apps/mockup/src/lib/folders.svelte.ts`, commit 199db43a1) made folder lists O(branch). In the mockup the larger cost turned out to be forced layout, so treat the gain here as scaling (2,000+ notes), not as a fix for a frame hitch. ## Proposed fix 1. Memoize the notes array: keep one array that is rebuilt only when a note is ADDED or REMOVED (and keep order), not when a note's fields change; expose per-note reactive fields (a `$state` object per note) so an edit changes that note's own signals only. 2. Keep per-list member sets updated by `applyEntry`/`put`: `journal` (ids of daily notes), `pinned`, and `tag -> Set<id>` (with a parent-tag union computed from the sets of its branch). `put` compares the old and new note and touches only the sets whose membership changed (pin toggled, tags changed, kind changed); an edit that changes none of them wakes no list. 3. `listNotes` then reads only its own set (`SvelteSet` / `SvelteMap<tag, SvelteSet<id>>`), maps ids to notes, and applies the text filter on that small set. 4. `refreshRecent`: apply only items whose `updated_ms` changed, and write `saveList` once per burst, debounced to idle (it serialises every note today). 5. Test: a unit test that edits one note's text and asserts the other lists' derived results are not recomputed (count the filter calls), plus a perf check with 2,000 notes (journal entry -> list update ms). Related: #1030 (folder-card drop; profiling and the incremental folders store).
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
kayg/calternal#1034
No description provided.