perf(desktop): observed_unread_ingest called 7–40×/s with no-op mutations; each call reopens the DB, projects twice and prunes — pins a core
Describe the bug
On a long-running desktop session with many channels, buzz-desktop pins a full CPU core (70–95 % of one core, sustained for hours, with no build or other load on the box). Sampling the process shows the time is in buzz_lib::observed_unread::{ingest_locked, projections, prune, open_db} → SQLite (sqlite3BtreeTableMoveto, sqlite3VdbeExec, pcache1Fetch, pread, fsync) plus SipHash / HashMap::insert.
The renderer is invoking the native observed_unread_ingest command 7–40 times per second, continuously, and the overwhelming majority are no-ops (idempotent marker / membership / channel-latest writes that change no rows). Each no-op still pays the full per-call cost in desktop/src-tauri/src/observed_unread.rs:
open_db()—Connection::open+busy_timeout+journal_mode=WALpragmas + 7×CREATE TABLE/INDEX IF NOT EXISTS+ schema-versionSELECT— on every call (open_scope_locked,ingest_locked,load_membership,flush). Because the connection is dropped after each call, SQLite also checkpoints and deletes the-wal/-shmeach time (the live-walis 0 bytes), so the page cache is cold on every call →pread/fsyncchurn.projections()is run twice per ingest (before/after diff). Each run loads every row ofread_markersfor the scope into aHashMapand does a full index scan ofobserved_events, allocatingformat!("msg:{id}")/format!("thread:{root}")per event.prune()runs on every ingest — threeDELETEs including aROW_NUMBER() OVER (PARTITION BY channel_id …)window scan of the whole scope — even when no event was inserted.read_markersis never pruned.prune()only touchesobserved_events;msg:markers accumulate forever (18,244msg:rows on the affected profile) and are fully loaded twice per ingest.
Renderer side (desktop/src/features/channels/useObservedUnreadPersistence.ts): events are coalesced with a 1 s timer (schedule → flushNative), but syncMarkers, advanceLatest and updateMembership each call enqueueNative immediately, one native transaction per call, with no coalescing and no dedupe against the last value sent — although IngestRequest already carries events, channelLatest, markers and membership in one payload.
Steps to reproduce
- Run Desktop against a busy community (this profile: 3 relay scopes, ~60 channels, 25 agents posting) for a few hours.
- Observe
buzz-desktopin Activity Monitor: ~70–95 % CPU with the app idle in the background. sample <pid> 3→ hot frames as above.- Inspect the store:
On this profile the primary scope showssqlite3 "~/Library/Application Support/xyz.block.buzz.app/observed-unread.db" \ "select scope, revision, last_sequence from scope_state;"last_sequence = 19,099,339(the other two scopes: 9,226 and 72). Re-running 10 s later showslast_sequenceadvancing by 70–400 whilecount(*)/max()onobserved_events,read_markers,channel_latestandunread_membershipare unchanged — i.e. no-op ingests.
Expected behavior
An idle desktop should not burn a core on unread bookkeeping. No-op ingests should be (nearly) free, and the store should not be re-opened, re-DDL'd, fully re-projected twice and re-pruned on every call.
Version and platform
- Buzz version: 0.5.28 (desktop-v0.5.28,
f87c10895);observed_unread.rsis byte-identical onorigin/main@8599efb6a - OS: macOS, Apple Silicon (18-core, 128 GB)
Logs / additional context
sample of pid 23382 (3 s), hottest in-process leaf frames:
287 sqlite3BtreeTableMoveto
96 sqlite3VdbeExec
72 <core::hash::sip::Hasher as core::hash::Hasher>::write
50 pcache1Fetch
34 sqlite3GetVarint
25 core::hash::BuildHasher::hash_one
20 hashbrown::map::HashMap::insert
Rust symbols in stacks: buzz_lib::observed_unread::projections ×86, ingest_locked ×56, prune ×19, open_db ×13.
Live store on the affected profile (primary scope): observed_events 1,098 · read_markers 18,875 (152 channel / 479 thread / 18,244 msg) · channel_latest 61 · unread_membership 4,389. Measured ingest rate: 40/s at one point, 7/s a few minutes later; zero row changes in either window.
Proposed fix
Native (robust regardless of which renderer path triggers it):
- Hold one
ConnectioninObservedUnreadStore(behind the existingwrite_lock) instead ofopen_dbper call; run DDL once at open. - Early-return a
Deltawith emptyupserts/removed(still advancingrevision/last_sequence) when the mutation produced zero row changes (total_changes()before/after), skippingpruneand bothprojections. - Cache the last
Vec<ChannelProjection>per scope so thebeforeprojection is free; computeafteronly when rows changed. - Only
prune()when events were inserted (or every N ingests). - Bound
read_markers: dropmsg:/thread:markers older thanHORIZON_SECONDS(the events they gate are pruned at the same horizon) and/or cap per scope.
Renderer:
- Route
syncMarkers/advanceLatest/updateMembershipthrough the same 1 s coalescing window as events, and skip markers/latest values equal to the last acknowledged value.
Happy to take the native half as a PR.