perf(signal-cache): share cached sessions via Arc, peek without deep clone - #809
Conversation
…clone SessionEntry stored Box<SessionRecord>, so peek_session deep-cloned the record (KBs with archived states + skipped message keys) on every call — it runs on the retry-receipt and LID-migration check paths. Entries are now Arc<SessionRecord>, matching the sender-key cache's existing pattern: - peek_session returns Option<Arc<SessionRecord>> — a refcount bump on cache hit, and the backend-miss path no longer clones to populate the cache either. - get_session (checkout) unwraps via Arc::try_unwrap: unique in steady state (a move, exactly like the old Box), cloning only if a peek's short-lived Arc is still alive. - flush serializes through the Arc unchanged. Callers (retry.rs, message tests) only read through the record, so the signature change is source-compatible at every site.
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Repository UI Review profile: ASSERTIVE Plan: Pro Plus Run ID: 📒 Files selected for processing (1)
📝 WalkthroughSummary by CodeRabbit
Walkthrough
ChangesSession cache Arc refactor
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related PRs
Suggested labels
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Benchmark Results67 unchanged benchmark(s)
|
Problem
The session object cache stores
SessionEntry::Present(Box<SessionRecord>), sopeek_session— the non-destructive read used by retry-receipt handling and LID-migration checks — deep-clones the record on every call (signal_cache.rs): aSessionRecordcarries archived session states plus skipped message keys, typically 1-2 KB. The backend-miss path cloned a second time to populate the cache. The sender-key cache already solved this exact problem withArc("bumps a refcount instead of deep-cloning the record'sVecDeque<SenderKeyState>"); sessions just never got the same treatment.Change
SessionEntry::Present(Arc<SessionRecord>), aligning sessions with the sender-key cache pattern:Option<Arc<SessionRecord>>— cache hit is a refcount bump; the backend-miss path wraps once and shares the sameArcbetween the cache slot and the return value (the old code cloned here too).get_session):Arc::try_unwrap(record).unwrap_or_else(|arc| (*arc).clone())— the entry is unique in steady state, so this is a move exactly like the oldBoxderef; it clones only if a peek's short-livedArcis still alive at checkout time, which is the rare race the old code paid for on every peek instead.Arc).Verification
Callers (
retry.rs×3, message tests ×2) only call read methods through the record, so theArcreturn is source-compatible at every site — no caller changes beyond what deref provides.cargo test -p wacore --lib: 947 passed;cargo test -p whatsapp-rust --lib: 744 passed;cargo clippy --all-targets -- -D warningsclean; fmt clean.Breaking
peek_sessionreturn type changesOption<SessionRecord>→Option<Arc<SessionRecord>>(pre-1.0; read-only consumers are unaffected by deref).Generated by Claude Code