Repository navigation
Recompute only the subscriptions that read a changed app-db path - #1
Open
yuxi-liu-wired wants to merge 1 commit into
Open
yuxi-liu-wired wants to merge 1 commit into
yuxi-liu-wired wants to merge 1 commit into
Conversation
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
force-pushed
the
perf/incremental-subscription-updates
branch
from
September 15, 2026 18:03
3dbe738 to
a8a83b3
Compare
Author
|
@tiensonqin logseq/logseq#13213 depends on this change and can't merge until it lands here. Could you review it? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 (secondnil when the subscription reads the whole root value). The pairs come from an application suppliedstore/!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 bindsstore/*changed-paths*the map comes from those paths. Otherwise the store compares the previous and next db two levels deep withidentical?(persistent maps share the values of untouched keys, so this is exact forassoc/update-instyle 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. Alisteners-by-subindex 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
falseornilis now served from the cache (contains?instead ofor/if-let), and without a!sub-key-fnthe store behaves exactly as before (full sweep).Numbers
Logseq registers every state key
kas 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 insidenext-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
requestAnimationFrameafter eachkeydown, 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.