-
Notifications
You must be signed in to change notification settings - Fork 0
docs: sender-key backlog Arc-COW perf (whatsapp-rust#881) #342
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
d8441b6
docs(changelog): group decrypt backlog Arc-COW perf (#881)
jlucaso1 8a24273
fix(changelog): use ### sub-headings per CodeRabbit convention
jlucaso1 ded25e0
fix(changelog): sentence-case title (arc, not Arc)
jlucaso1 4192d90
fix(changelog): restore Arc capitalisation (Rust type name, not a com…
jlucaso1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| --- | ||
| title: "June 16, 2026 — Group decrypt: sender-key backlog shared behind arc" | ||
| description: "SenderKeyState now keeps its skipped-message-key backlog in an Arc<Vec>, so loading a sender-key record is a refcount bump instead of a deep clone. In-order group decrypts with a large backlog are ~3x faster, and the per-decrypt allocation spike disappears." | ||
| --- | ||
|
|
||
| ## Performance | ||
|
|
||
| **Group decrypt: sender-key message backlog behind `Arc` COW ([#881](https://github.com/oxidezap/whatsapp-rust/pull/881))** | ||
|
|
||
| Every group decrypt calls `load_sender_key`, which clones the `SenderKeyRecord` out of the in-memory cache. `SenderKeyState` previously stored its skipped-message-key backlog (`sender_message_keys`) directly inside the protobuf struct, so each clone deep-copied the whole backlog — up to `MAX_MESSAGE_KEYS` entries (~82 KB at capacity). | ||
|
|
||
| This cost is recurring: once a receiver falls behind and catches up, the backlog remains populated. Every subsequent in-order message pays the full allocation even though the in-order path never reads the backlog at all — it only advances the chain key. | ||
|
|
||
| ### What changed | ||
|
|
||
| `SenderKeyState` now holds the backlog in a dedicated `Arc<Vec<SenderMessageKey>>` field. The protobuf struct's `sender_message_keys` list is kept empty in memory; it is only reassembled at serialization (`as_protobuf`). A `debug_assert` guards this invariant. | ||
|
|
||
| - **In-order decrypt** (the hot path): clone = refcount bump. No allocation. | ||
| - **Mutation** (skip-ahead caching or out-of-order removal): `Arc::make_mut` copies the Vec exactly once, leaving the cache's shared clone untouched. | ||
| - **Serialization / persistence**: unchanged — `as_protobuf` reassembles the full list from the `Arc` before writing. | ||
|
|
||
| ### Benchmark results | ||
|
|
||
| (`bench_group_in_order_decrypt_with_backlog`, ~2000-key backlog, same machine A/B): | ||
|
|
||
| | | Before | After | | ||
| |---|---|---| | ||
| | Median latency | 97 µs | 33 µs | | ||
| | Mean latency | 110 µs | 33 µs | | ||
| | Worst case | 600 µs | 38 µs | | ||
|
|
||
| The ~3× median speedup and the elimination of the long allocator tail both come from the ~82 KB `Vec` allocation no longer occurring on every in-order decrypt. The out-of-order worst-case bench is flat (the load-time clone simply becomes one `make_mut` clone of the same size). The empty-backlog decrypt is unchanged. | ||
|
|
||
| CodSpeed tracks the new `bench_group_in_order_decrypt_with_backlog` bench going forward. | ||
|
|
||
| ### No breaking changes | ||
|
|
||
| No public API surface changed. No new dependencies added. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.