Skip to content

bench: cover the receive path — plaintext decode and appstate index-MAC dedup - #856

Merged
jlucaso1 merged 1 commit into
mainfrom
claude/receive-path-benchmarks-l786xn
Jun 12, 2026
Merged

bench: cover the receive path — plaintext decode and appstate index-MAC dedup#856
jlucaso1 merged 1 commit into
mainfrom
claude/receive-path-benchmarks-l786xn

Conversation

@jlucaso1

Copy link
Copy Markdown
Collaborator

Bench-only PR (same flow as #854): map the baseline with CodSpeed before touching any code. Two inbound-side coverage gaps:

1. decode_plaintext — unpad + prost decode (wacore/src/messages.rs)

Runs on every received message and is the inbound mirror of the already-benched encode_and_pad, closing the send/receive coverage asymmetry. It's also where the #853 submessage boxing lands on the receive side, so the flamegraph doubles as a check on that.

Shapes match the send-side bench (text_reply, media_refs, large_text) plus group_skdm_text — the inline-SKDM group first-message.

Local numbers (median): text_reply 591 ns, large_text 644 ns, media_refs 1.11 µs, group_skdm_text 500 ns.

2. App-state index-MAC dedup — O(N²) scan (wacore/src/appstate_sync.rs)

The linear-scan dedup feeding the batched previous-value-MAC lookup, extracted from its two inline copies (inbound process_patch_list and outbound build_patch) into a pure collect_unique_index_macs so it can be measured. Byte-identical logic, no behavior change.

This is the measurement the known HashSet fix is blocked on: HashSet-vs-scan measured 6–120% worse at small N in this project, so both ends are pinned — N=10 (typical incremental patch) and N=1000 (resume-sync upper bound).

Local numbers (median): N=10 → 288 ns, N=1000 → 1.48 ms. The quadratic blowup is real at the upper bound (~500k Vec<u8> compares per resume patch); whether the fix pays without regressing small N is now CodSpeed's call.

Verification

  • cargo fmt / cargo clippy -p wacore --benches --tests clean (only the pre-existing clippy.toml chrono warning)
  • cargo test -p wacore: 987 + auxiliary suites, all green
  • cargo check --all passes
  • Both benches run locally (numbers above)

https://claude.ai/code/session_01EoJjbyorpCARCBTZSmUMRr


Generated by Claude Code

…AC dedup

Two gaps on the inbound side, benched before any optimization:

- decode_plaintext (unpad + prost decode): runs on every received message
  and is the inbound mirror of the already-benched encode_and_pad. Shapes
  match the send-side bench plus an inline-SKDM group first-message.
- collect_unique_index_macs: the O(N²) linear-scan dedup feeding the
  batched previous-value-MAC lookup, extracted from its two inline copies
  (inbound process_patch_list and outbound build_patch) into a pure
  function so it can be measured. At the 1000-mutation resume-sync upper
  bound the scan takes ~1.5ms; a HashSet swap measured 6-120% slower at
  small N in this codebase, so both ends are pinned (N=10 and N=1000)
  before deciding.

No behavior change: the extraction is byte-identical logic.

https://claude.ai/code/session_01EoJjbyorpCARCBTZSmUMRr
@coderabbitai

coderabbitai Bot commented Jun 12, 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

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: ec605cb6-0235-49f0-ae9a-5e7be5fec7b2

📥 Commits

Reviewing files that changed from the base of the PR and between 38cc31d and a179b51.

📒 Files selected for processing (4)
  • wacore/Cargo.toml
  • wacore/benches/appstate_sync_benchmark.rs
  • wacore/benches/message_utils_benchmark.rs
  • wacore/src/appstate_sync.rs

📝 Walkthrough

Summary by CodeRabbit

  • Tests

    • Added new performance benchmarks for app state synchronization and message processing operations to measure and optimize internal hot paths.
  • Chores

    • Updated build configuration to support additional benchmark targets.

Walkthrough

This PR extracts duplicate index MAC collection logic from two patch-processing sites into a single reusable helper, collect_unique_index_macs, then benchmarks that helper and message decoding costs under realistic load patterns. The changes are surgical: a focused helper reduces maintenance surface, and the benchmarks establish performance baselines for the orchestration layer's hot paths.

Changes

Appstate sync extraction and benchmarking

Layer / File(s) Summary
Extract and refactor deduplication helper
wacore/src/appstate_sync.rs
New public function collect_unique_index_macs extracts and deduplicates mutation index MAC blobs using first-seen order and linear-scan membership checks. Inbound patch processing and build_patch both refactored to call this helper instead of duplicating the deduplication loop.
Benchmark appstate_sync helper
wacore/Cargo.toml, wacore/benches/appstate_sync_benchmark.rs
Cargo.toml registers the new benchmark target with harness disabled. Benchmark file defines setup_mutations to generate n distinct Set mutations with 32-byte index MACs, then measures collect_unique_index_macs throughput for n in [10, 1000] under black_box isolation.
Benchmark message decode flow
wacore/benches/message_utils_benchmark.rs
Module docs updated to describe both encode (pad + prost) and decode (unpad) costs. New recv_shape helper generates receive message variants, including a group_skdm_text case that embeds a sender key distribution message. New bench_decode_plaintext benchmark measures pad + encode + decode round-trip for each shape.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Possibly related PRs

Suggested labels

performance

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately and specifically describes the main changes: adding benchmarks for plaintext decode and appstate index-MAC deduplication on the receive path.
Description check ✅ Passed The description is comprehensive and directly related to the changeset, clearly explaining both benchmarks, their purpose, local performance numbers, and verification steps performed.
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.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch claude/receive-path-benchmarks-l786xn

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 and usage tips.

@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.

No issues found across 4 files

Re-trigger cubic

@codspeed-hq

codspeed-hq Bot commented Jun 12, 2026

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

✅ 145 untouched benchmarks
🆕 12 new benchmarks

Performance Changes

Mode Benchmark BASE HEAD Efficiency
🆕 Memory bench_collect_unique_index_macs[10] N/A 560 B N/A
🆕 Memory bench_collect_unique_index_macs[1000] N/A 54.7 KB N/A
🆕 Memory bench_decode_plaintext[group_skdm_text] N/A 767 B N/A
🆕 Memory bench_decode_plaintext[large_text] N/A 4 KB N/A
🆕 Memory bench_decode_plaintext[media_refs] N/A 12.8 KB N/A
🆕 Memory bench_decode_plaintext[text_reply] N/A 3.4 KB N/A
🆕 Simulation bench_collect_unique_index_macs[10] N/A 4.4 µs N/A
🆕 Simulation bench_collect_unique_index_macs[1000] N/A 5.7 ms N/A
🆕 Simulation bench_decode_plaintext[group_skdm_text] N/A 14 µs N/A
🆕 Simulation bench_decode_plaintext[large_text] N/A 15.9 µs N/A
🆕 Simulation bench_decode_plaintext[media_refs] N/A 25.3 µs N/A
🆕 Simulation bench_decode_plaintext[text_reply] N/A 15.1 µs N/A

Comparing claude/receive-path-benchmarks-l786xn (a179b51) with main (38cc31d)

Open in CodSpeed

@jlucaso1
jlucaso1 merged commit b956722 into main Jun 12, 2026
14 checks passed
@jlucaso1
jlucaso1 deleted the claude/receive-path-benchmarks-l786xn branch June 12, 2026 02:24
jlucaso1 pushed a commit that referenced this pull request Jun 14, 2026
…detection

`detect_duplicate_index_in_patch` deduped index_macs with two
`HashSet<&[u8]>`. The keys are HMAC outputs (uniformly random), so SipHash
buys no distribution benefit over a trivial compare — it only costs the
per-key hash setup and a table allocation, which `bench_process_patch_50_validated`
showed via `RandomState::hash_one` + `fallible_with_capacity`.

Swap both sets for linear-scan `Vec<&[u8]>`, the same trade-off already
validated for the sibling `collect_unique_index_macs` (#856), where HashSet
measured 6-120% worse at the patch sizes seen in practice. Set and Remove
stay deduped independently, so the existing semantics
(`process_patch_rejects_duplicate_set_index` and
`process_patch_allows_same_index_across_set_and_remove`) are unchanged.

Zero new dependencies (no fast-hash crate), matching the recent dependency
trimming. CI/CodSpeed measure the delta on bench_process_patch_50_validated.

https://claude.ai/code/session_01XHsbPwjaCRDHDL69HbEgR8
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.

2 participants