perf(relay,desktop): bulk latest-message-per-channel lookup for get_channels#1465
Open
tlongwell-block wants to merge 2 commits into
Open
perf(relay,desktop): bulk latest-message-per-channel lookup for get_channels#1465tlongwell-block wants to merge 2 commits into
tlongwell-block wants to merge 2 commits into
Conversation
…hannels get_channels populated last_message_at with one limit:1 filter per channel — N top-1 DB queries per sidebar refresh, re-run every 60s (bounded-concurrent since #1457, but still O(N) queries). Replace the N filters with a single bridge extension filter, `latest_per_channel: true` (same raw-JSON pattern as before_id / depth_limit / feed_types): - buzz-db: get_latest_event_per_channel — unnest + LATERAL top-1 per channel riding idx_events_community_channel_created. Unlike DISTINCT ON, the LATERAL LIMIT 1 cannot degrade to a sort; EXPLAIN confirms ordered index scans on every partition. Per-channel ordering (created_at DESC, id ASC LIMIT 1) is byte-identical to query_events with limit:1, so the winning event per channel is unchanged. - bridge /query: new phase ahead of the catch-all. Requested #h channels intersect accessible_channels (same outcome as the catch-all access-scope skip); every returned event passes the identical four per-event gates (filters_match, reader_authorized_for_event, is_author_only_event, event_in_accessible_channel). Top-1-per-channel is guaranteed by construction, so the multi-#h limit-budget hazard that rules out plain multi-#h pushdown does not apply. - desktop get_channels: N filters -> 1 filter. Verified: Postgres-backed equivalence test (bulk winners == serial per-channel winners, incl. same-second id-ASC tie-break, soft-delete and wrong-kind exclusion, kinds None/empty semantics); cargo test -p buzz-relay -p buzz-db green; desktop tauri tests 864 pass; workspace clippy --all-targets -D warnings clean. Co-authored-by: Tyler Longwell <tlongwell@block.xyz> Signed-off-by: Tyler Longwell <tlongwell@block.xyz>
7baf6a9 to
9ce97bd
Compare
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
get_channelspopulatedlast_message_atwith onelimit:1filter per channel — N top-1 DB queries per sidebar refresh, re-run every 60s (bounded-concurrent since #1457, but still O(N) queries). This replaces the N filters with one bridge extension filter and one indexed DB round trip. This is theDISTINCT ONfollow-up deliberately split out of #1457.get_latest_event_per_channel—unnest($channels) CROSS JOIN LATERAL (... ORDER BY created_at DESC, id ASC LIMIT 1)ridingidx_events_community_channel_created./query: newlatest_per_channel: trueextension field (same raw-JSON pattern asbefore_id/depth_limit/feed_types), handled in a dedicated phase ahead of the catch-all.get_channels: N filters →{"kinds":[9,40002],"#h":[...all ids],"latest_per_channel":true}. Rust-only change; no collision with perf(desktop): instant channel switching — non-blocking first paint, persisted snapshots #1452's TS surface.Correctness
created_at DESC, id ASC LIMIT 1) is byte-identical toquery_eventswithlimit:1, which is what each per-channel filter executed before. A Postgres-backed equivalence test asserts bulk winners == serial winners, including the same-secondid ASCtie-break, soft-delete exclusion, wrong-kind exclusion, and empty-channel omission.#hchannels are intersected withaccessible_channelsbefore the query (same outcome as the catch-all's access-scope skip), and every returned event passes the identical four per-event gates:event_in_accessible_channel,filters_match,reader_authorized_for_event,is_author_only_event.#hhazard doesn't apply. Plain multi-#hpushdown is rejected in this codebase because quieter channels get silently dropped under a shared limit budget. Here top-1-per-channel is guaranteed by construction (LATERAL per unnest row), so there is no shared budget to starve.kindsabsent = any kind; empty = match nothing.latest_per_channel: trueare handled exactly as before; the WS REQ path is untouched.Plan shape
EXPLAINshows ordered index scans on(community_id, channel_id, created_at DESC, id)under aLimitnode on every partition — no sort. LATERAL was chosen overDISTINCT ONspecifically because the per-groupLIMIT 1cannot degrade to a full sort as channel count/history grows.Verified
cargo test -p buzz-db latest_event_per_channel -- --ignored→ greencargo test -p buzz-relay -p buzz-db: 451 + 79 pass, 0 fail (re-run after merging maine42dae3fon top)cargo clippy --workspace --all-targets -- -D warnings: clean