Skip to content

perf(message): reduce hot-path allocations - #1015

Merged
jlucaso1 merged 1 commit into
mainfrom
agent/reduce-message-path-allocations
Jul 10, 2026
Merged

perf(message): reduce hot-path allocations#1015
jlucaso1 merged 1 commit into
mainfrom
agent/reduce-message-path-allocations

Conversation

@jlucaso1

Copy link
Copy Markdown
Collaborator

Summary

  • process unobserved ACK and receipt nodes inline and bypass hot async_trait allocations
  • batch in-memory Signal session writes and remove temporary lookup and message-secret allocations
  • preserve concurrent dispatch for observed events and synchronous receipt ACKs

Impact

DHAT ping-pong allocation cost per cycle dropped from 46,088 to 40,542 bytes (-12.0%) and from 254.84 to 231.60 blocks (-9.1%).

Validation

  • cargo test -p wacore --lib
  • cargo test -p whatsapp-rust --lib
  • cargo check -p whatsapp-rust --lib --features tracing
  • cargo clippy -p whatsapp-rust -p wacore --tests -- -D warnings
  • direct, group, and receipt-observer benchmark smoke tests

@coderabbitai

coderabbitai Bot commented Jul 10, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI (base), Organization UI (inherited)

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: f9d440c0-430b-41da-8efc-1bcf3dd1eb71

📥 Commits

Reviewing files that changed from the base of the PR and between 3a6ef95 and 65aaae0.

📒 Files selected for processing (7)
  • src/client/device_registry.rs
  • src/client/node_io.rs
  • src/handlers/message.rs
  • src/msg_secret_buffer.rs
  • src/receipt.rs
  • src/send/mod.rs
  • wacore/src/store/in_memory.rs

📝 Walkthrough

Summary by CodeRabbit

  • Improvements
    • Improved responsiveness when processing messages, acknowledgements, and delivery receipts.
    • Enhanced message handling reliability during high-volume or backlogged conditions.
    • Improved device lookup consistency across alternate identifiers and account changes.
    • Streamlined secure message buffering, including more efficient single-message processing.
    • Improved session updates when multiple records are saved together.
  • Bug Fixes
    • Improved cache invalidation so device-related updates are reflected more consistently.

Walkthrough

The changes optimize device lookup iteration, route selected stanzas through inline handlers, add single-entry message-secret buffering, and implement batched in-memory session updates with tests.

Changes

Runtime path and storage updates

Layer / File(s) Summary
Device lookup iteration and alias tracking
src/client/device_registry.rs
Lookup keys are produced and consumed as iterators, while device-cache alias tracking now uses lookup.all_keys().
Inline stanza and acknowledgement dispatch
src/client/node_io.rs, src/handlers/message.rs, src/receipt.rs
Acknowledgements, receipts, and messages gain direct inline handling paths with async wrappers retained for trait and call-site compatibility.
Single-entry secret buffering
src/msg_secret_buffer.rs, src/send/mod.rs
Pending-entry insertion is centralized, and outbound secret persistence uses the new single-entry queue method.
Batch session updates and validation
wacore/src/store/in_memory.rs
The in-memory store updates session batches under one lock and tests insertion, replacement, and retention of other entries.

Estimated code review effort: 4 (Complex) | ~45 minutes

Sequence Diagram(s)

sequenceDiagram
  participant MessageLoop
  participant NodeProcessor
  participant InlineHandler
  participant ChatLane
  MessageLoop->>NodeProcessor: process decrypted node
  NodeProcessor->>InlineHandler: handle ack, receipt, or message inline
  InlineHandler->>ChatLane: enqueue message for chat
  ChatLane-->>InlineHandler: enqueue result
  InlineHandler-->>NodeProcessor: handled status and cancellation
Loading

Possibly related PRs

Suggested labels: performance

Suggested reviewers: cubic-dev-ai, greptile-apps

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly matches the PR’s main goal of reducing hot-path allocations in message handling.
Description check ✅ Passed The description is directly related to the changeset and accurately summarizes the allocation-focused refactors.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch agent/reduce-message-path-allocations

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@jlucaso1
jlucaso1 marked this pull request as ready for review July 10, 2026 21:11
@greptile-apps

greptile-apps Bot commented Jul 10, 2026

Copy link
Copy Markdown

Greptile Summary

This PR reduces allocations on message and receipt hot paths. The main changes are:

  • Inline handling for unobserved ack, receipt, and message nodes.
  • Borrowed lookup-key iteration in the device registry.
  • Single-entry message-secret buffering for outbound sends.
  • Batched in-memory Signal session writes.

Confidence Score: 5/5

This looks safe to merge.

  • No blocking issues found in the changed code.
  • The inline handler paths keep the same handler bodies and preserve observer-aware dispatch.
  • The buffer and batch changes keep the existing write and lookup semantics.

Important Files Changed

Filename Overview
src/client/node_io.rs Adds inline fast paths for built-in node handling while keeping observer and waiter gates.
src/handlers/message.rs Extracts the message handler body into a reusable inline method.
src/receipt.rs Moves receipt handling into a synchronous inline helper with the async wrapper preserved.
src/msg_secret_buffer.rs Refactors pending insertion and scheduling, and adds a single-entry queue path.
src/send/mod.rs Uses the single-entry message-secret queue path for outbound secret persistence.
src/client/device_registry.rs Replaces temporary lookup-key vectors with borrowed iterators.
wacore/src/store/in_memory.rs Adds a batched in-memory Signal session write implementation.

Reviews (1): Last reviewed commit: "perf(message): reduce hot-path allocatio..." | Re-trigger Greptile

@cubic-dev-ai cubic-dev-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All reported issues were addressed across 7 files

Confidence score: 5/5

  • Safe to merge after the addressed issues were fixed.

Reply with feedback, questions, or to request a fix.

Re-trigger cubic

Comment thread src/client/node_io.rs
@github-actions

Copy link
Copy Markdown

📦 Binary size report

Metric main PR Δ
bin size (stripped) 10.83 MiB 10.83 MiB +1.97 KiB (+0.02%) 🔺
bin .text 8.83 MiB 8.83 MiB +1.62 KiB (+0.02%) 🔺
bin allocated (text+data+bss) 10.83 MiB 10.83 MiB +104 B (+0.00%) 🔺
llvm-lines wacore 505,440 505,783 +343 (+0.07%) 🔺
llvm-lines wacore copies 17,345 17,358 +13 (+0.07%) 🔺
llvm-lines whatsapp-rust lib 767,242 767,999 +757 (+0.10%) 🔺
llvm-lines whatsapp-rust lib copies 24,947 24,916 -31 (-0.12%) 🔽
deps crates (Cargo.lock) 472 472 0
.text per crate
Crate main PR Δ
.text whatsapp_rust 1.65 MiB 1.65 MiB +2.31 KiB (+0.14%) 🔺
.text wacore 526.09 KiB 526.43 KiB +348 B (+0.06%) 🔺
.text wacore_binary 157.88 KiB 157.88 KiB 0
.text wacore_libsignal 179.42 KiB 179.42 KiB 0
.text wacore_appstate 158.25 KiB 158.25 KiB 0
.text wacore_noise 26.05 KiB 26.05 KiB 0
.text waproto 1.60 MiB 1.60 MiB 0
.text whatsapp_rust_sqlite_storage 513.00 KiB 512.90 KiB -98 B (-0.02%) 🔽
.text whatsapp_rust_tokio_transport 43.79 KiB 43.89 KiB +98 B (+0.22%) 🔺
.text whatsapp_rust_ureq_http_client 10.47 KiB 10.47 KiB 0
.text std 1.00 MiB 1.00 MiB -1.47 KiB (-0.14%) 🔽
.text other deps 2.95 MiB 2.95 MiB +428 B (+0.01%) 🔺
Top movers (cargo-bloat attribution)
Crate main PR Δ
whatsapp_rust 1.65 MiB 1.65 MiB +2.31 KiB (+0.14%)
std 1.00 MiB 1.00 MiB -1.47 KiB (-0.14%)

Baseline: 3a6ef95ee (latest main run) · Head: 79683c668 · Graphs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant