buzz icon indicating copy to clipboard operation
buzz copied to clipboard

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

Open micspiral opened this issue 1 month ago • 0 comments

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:

  1. open_db() — Connection::open + busy_timeout + journal_mode=WAL pragmas + 7× CREATE TABLE/INDEX IF NOT EXISTS + schema-version SELECT — 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/-shm each time (the live -wal is 0 bytes), so the page cache is cold on every call → pread/fsync churn.
  2. projections() is run twice per ingest (before/after diff). Each run loads every row of read_markers for the scope into a HashMap and does a full index scan of observed_events, allocating format!("msg:{id}") / format!("thread:{root}") per event.
  3. prune() runs on every ingest — three DELETEs including a ROW_NUMBER() OVER (PARTITION BY channel_id …) window scan of the whole scope — even when no event was inserted.
  4. read_markers is never pruned. prune() only touches observed_events; msg: markers accumulate forever (18,244 msg: 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

  1. Run Desktop against a busy community (this profile: 3 relay scopes, ~60 channels, 25 agents posting) for a few hours.
  2. Observe buzz-desktop in Activity Monitor: ~70–95 % CPU with the app idle in the background.
  3. sample <pid> 3 → hot frames as above.
  4. Inspect the store:
    sqlite3 "~/Library/Application Support/xyz.block.buzz.app/observed-unread.db" \
      "select scope, revision, last_sequence from scope_state;"
    
    On this profile the primary scope shows last_sequence = 19,099,339 (the other two scopes: 9,226 and 72). Re-running 10 s later shows last_sequence advancing by 70–400 while count(*) / max() on observed_events, read_markers, channel_latest and unread_membership are 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.rs is byte-identical on origin/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 Connection in ObservedUnreadStore (behind the existing write_lock) instead of open_db per call; run DDL once at open.
  • Early-return a Delta with empty upserts/removed (still advancing revision/last_sequence) when the mutation produced zero row changes (total_changes() before/after), skipping prune and both projections.
  • Cache the last Vec<ChannelProjection> per scope so the before projection is free; compute after only when rows changed.
  • Only prune() when events were inserted (or every N ingests).
  • Bound read_markers: drop msg:/thread: markers older than HORIZON_SECONDS (the events they gate are pruned at the same horizon) and/or cap per scope.

Renderer:

  • Route syncMarkers / advanceLatest / updateMembership through 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.

micspiral avatar Oct 14 '26 04:10 micspiral