Skip to content

Recompute only the subscriptions that read a changed app-db path - #1

Open
yuxi-liu-wired wants to merge 1 commit into
logseq:logseq/forkfrom
yuxi-liu-wired:perf/incremental-subscription-updates
Open

yuxi-liu-wired wants to merge 1 commit into
logseq:logseq/forkfrom
yuxi-liu-wired:perf/incremental-subscription-updates

Conversation

@yuxi-liu-wired

Copy link
Copy Markdown

What changes

next-state! in the atom store no longer empties the subscription cache and re-evaluates every listened subscription on every write. It evicts and recomputes only the subscriptions that read a changed app-db path and leaves every other cached value in place.

Mechanism

The store indexes each cached subscription by the app-db paths it reads, as [root second] pairs (second nil when the subscription reads the whole root value). The pairs come from an application supplied store/!sub-key-fn; a subscription that fn does not know inherits the pairs of its declared signals; a subscription with neither is marked unknown and is recomputed on every write.

On next-state! the store builds a change map. When the caller binds store/*changed-paths* the map comes from those paths. Otherwise the store compares the previous and next db two levels deep with identical? (persistent maps share the values of untouched keys, so this is exact for assoc / update-in style writes and conservative for the rest). Subscriptions under a changed pair, under [root nil] of a changed root, or with unknown paths are evicted; the listened ones among them are recomputed and their listeners notified. Subscriptions are indexed once, when first cached; evictions leave the index alone. A listeners-by-sub index replaces the scan over all listeners at notification time.

A registry change (a late reg-sub, a hot reload) clears the cached values together with the index, and a listened subscription that is not indexed is recomputed on the next write, so every listener stays current under the new registry. When a subscription's last listener leaves, its cache, index and memo entries are dropped, so subscriptions of unmounted components do not accumulate over a long session.

Two side fixes in the same code path: a cached false or nil is now served from the cache (contains? instead of or / if-let), and without a !sub-key-fn the store behaves exactly as before (full sweep).

Numbers

Logseq registers every state key k as a subscription [k & path] with the signal [::state-key k]. On a 204 KB page of a 2,000 page graph, each keystroke performed 5 store writes and each write swept every mounted subscription. CPU profile of typing there: 36 % of the sampled time inside next-state! before this change. With the change plus Logseq binding its changed paths, next-state! no longer appears above 1 % of the profile. Moving the editing cursor between blocks (each move rewrites :editor/editing?, which every mounted block subscribes to keyed by its own id) went from 44.4 / 50.2 / 76.5 ms to 20.8 / 31.2 / 47 ms key press to next frame (p50 / p90 / max), with 0 tasks over 50 ms in 40 moves instead of 38.

Measured in a Chromium 148 renderer over the DevTools protocol with requestAnimationFrame after each keydown, 83 keys at a 60 ms cadence.

Tests

test/rfx/store_incremental_test.clj (7 tests) covers: a bound path recomputes only the subscriptions reading it; a sibling path under the same root and other roots stay cached; a subscription with unknown paths is always recomputed; the unbound two level comparison; removed keys; notification only when the value changed; the full sweep without a !sub-key-fn; pruning of a subscription's cache and index entries when its last listener leaves; recomputation of listened subscriptions after a registry change. All 8 JVM tests in the repository pass (32 assertions).

Allow edits from maintainers.

next-state! emptied the subscription cache and re-evaluated every
subscription with a listener on every write. An application whose
components hold thousands of subscriptions ran that sweep on each
keystroke.

The atom store now indexes each cached subscription by the app-db paths
it reads, as [root second] pairs, derived from an application-supplied
!sub-key-fn and from declared signals. On next-state! it computes what
changed, from *changed-paths* when the caller binds it or by comparing
the previous and next db two levels deep, evicts the subscriptions that
read those paths, recomputes the listened ones, and leaves the rest
cached. Subscriptions whose paths are unknown are recomputed on every
write, and without a !sub-key-fn the store behaves as before.

A registry change clears the cached values along with the index, and a
listened subscription that is not indexed is recomputed on the next
write, so hot reloads and late registrations keep every listener
current. When a subscription's last listener leaves, its cache, index
and memo entries are dropped, so subscriptions of unmounted components
do not accumulate. Also cache false and nil results (contains? instead
of or) and keep a listeners-by-sub index so notification does not scan
all listeners.

Tests: test/rfx/store_incremental_test.clj.
@yuxi-liu-wired

Copy link
Copy Markdown
Author

@tiensonqin logseq/logseq#13213 depends on this change and can't merge until it lands here. Could you review it?

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