perf(daemon): avoid full chat scans for attention updates

Merged
JL
5d ago
+643-29

State publication and mute-timer reset rescanned every chat, making list hydration quadratic. Maintain an exact running unread total and aggregate mute-deadline buckets under the existing state lock. Updates adjust old/new contributions; time changes visit only crossed deadlines. Stale timer queries do not change cached time. Manual unread, status exclusion, saturation recovery, finite/indefinite mute and account cleanup remain covered. No new application dependencies or wire changes. Independent of #265.

Paired full release test executables on one Linux runner replayed 100/1,000/10,000 chats with none/half/all muted. Nine paired samples per scenario, plus excluded warmups, yielded 162 retained samples. At 10,000 chats the isolated state workload fell from 697–878 ms to 9.2–10.9 ms, 64–89x faster; median complete-process CPU fell about 98%. The 100-chat CPU distributions overlap. This does not measure a complete connected daemon or frontend startup.

The index trades memory for CPU. At 10,000 chats, post-workload PSS changed by +24/+400/+808 KiB and peak RSS by +68/+200/+504 KiB for none/half/all muted. The all-muted fixture uses a distinct deadline for every chat. Live heap and allocator retention were not measured separately. Measurements use no allocation instrumentation, CodSpeed, eBPF, external services, root or credentials. A small C launcher avoids inheriting Python's pre-exec RSS floor.

Reproduction, distributions, limitations and other inspected opportunities are in docs/attention-performance.md; raw paired data is in docs/measurements/attention.csv.

Local validation: 347 daemon all-feature library tests passed, one profiling workload ignored in ordinary tests; all-target/all-feature daemon Clippy, formatting and diff checks passed. All 180 fresh measurement processes, including warmups, passed correctness assertions. Codex completed the final-head review on 3ca1300 with no findings; there are no unresolved review threads. MSRV passed. Remaining platform CI and artifact builds are still running, so this PR remains draft.