Skip to content

bench: borrow inputs in the benches that only read them - #849

Merged
jlucaso1 merged 1 commit into
mainfrom
bench/get-children-borrow-input
Jun 11, 2026
Merged

bench: borrow inputs in the benches that only read them#849
jlucaso1 merged 1 commit into
mainfrom
bench/get-children-borrow-input

Conversation

@jlucaso1

Copy link
Copy Markdown
Collaborator

Follow-up to #837, found while flamegraphing bench_get_children_by_tag (57.1 us in Simulation): 66% of that time is drop_in_place of the input Node tree, because the bench consumed its pre-built input with bench_values and the drop ran inside the measured window. The actual traversal costs about 2 us.

Eight benches had the pattern: get_children_by_tag, jid_to_owned_access, the six reporting-token benches (content extraction, full generation, message encoding). All of them only read the input, so they now use bench_refs, and values produced inside the closure are returned so divan drops them outside the timing. process_history_sync keeps bench_values: that operation genuinely takes ownership of its blob, so the consume is part of the measured contract (and its result was already returned).

The CodSpeed report on this PR will show large "improvements" on these eight benchmarks; that is measurement correction, not a performance change. No library code is touched.

Follow-up to #837: eight benches still took their pre-built input by value, so dropping it (a full Node tree, wa::Message structs) was timed as part of the operation. The get_children_by_tag flamegraph made it visible: 66% of the reported time was drop_in_place of the input tree, and the traversal itself is ~2 us, not 57 us. Inputs that the operation only reads are now passed by reference, and produced values are returned so the harness drops them outside the window; process_history_sync keeps bench_values because the operation genuinely consumes its blob.
@coderabbitai

coderabbitai Bot commented Jun 11, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Summary by CodeRabbit

Release Notes

  • Tests
    • Updated internal benchmark methodology for improved performance measurement accuracy.
    • Refined benchmarking approach across multiple test suites.

Note: These are internal testing infrastructure improvements with no impact on end-user functionality.

Walkthrough

These changes convert benchmark harnesses across two files from divan's .bench_values(...) to .bench_refs(...) pattern, shifting measurement focus onto references instead of owned values. Reporting token benchmarks now measure content extraction, token generation, and message encoding directly; binary node benchmarks similarly shift for children iteration and JID attribute access.

Changes

Benchmark Harness Migration

Layer / File(s) Summary
Reporting token benchmark harness updates
wacore/benches/reporting_token_benchmark.rs
Content extraction, full token generation, and message encoding benchmarks convert from .bench_values(...) to .bench_refs(...), wrapping results in divan::black_box to shield computed values from optimization.
Binary node benchmark harness updates
wacore/binary/benches/binary_benchmark.rs
Children-by-tag iteration and JID attribute access benchmarks switch to .bench_refs(...) input handling. The JID access benchmark preserves unmarshal/to_owned flow. The attribute parser benchmark now evaluates the final owned node before returning.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Possibly related PRs

  • oxidezap/whatsapp-rust#247: Both PRs touch bench_get_children_by_tag, with this one refining the divan harness pattern built on the iterator refactor from #247.
  • oxidezap/whatsapp-rust#837: Related divan benchmark input conversion effort, adjusting how benchmark closure inputs are handled across the codebase.
  • oxidezap/whatsapp-rust#828: Part of the broader divan framework migration in wacore benchmarks; this PR refines the harness measurement patterns introduced in that conversion.

Suggested labels

performance

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely describes the main change: refactoring benchmarks to borrow inputs instead of consuming them, which is the core objective of this PR.
Description check ✅ Passed The description is directly related to the changeset, explaining the motivation (measurement noise from drop operations), which benchmarks were affected, and why the changes were made.
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 bench/get-children-borrow-input

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.

@codspeed-hq

codspeed-hq Bot commented Jun 11, 2026

Copy link
Copy Markdown
Contributor

Merging this PR will improve performance by 54.01%

⚡ 9 improved benchmarks
✅ 131 untouched benchmarks

Performance Changes

Mode Benchmark BASE HEAD Efficiency
Simulation bench_get_children_by_tag 57.1 µs 19.3 µs ×3
Simulation bench_message_encoding_simple 17.4 µs 9.1 µs +91.25%
Simulation bench_content_extraction_simple 19.1 µs 10.9 µs +74.9%
Simulation bench_message_encoding_extended 26.3 µs 15.9 µs +65.3%
Simulation bench_content_extraction_extended 29.9 µs 19.7 µs +52%
Simulation bench_full_token_generation_simple 42.3 µs 34.1 µs +24.19%
Simulation bench_full_token_generation_extended 53.2 µs 43.3 µs +23.07%
Simulation bench_unpack_uncompressed 212.5 ns 183.3 ns +15.91%
Simulation bench_unpad_message_ref 205.8 ns 186.4 ns +10.43%

Tip

Curious why this is faster? Comment @codspeedbot explain why this is faster on this PR, or directly use the CodSpeed MCP with your agent.


Comparing bench/get-children-borrow-input (e6d8ff6) with main (be3d255)

Open in CodSpeed

@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 2 files

Re-trigger cubic

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@wacore/benches/reporting_token_benchmark.rs`:
- Line 54: The closure passed to .bench_refs currently calls
black_box(generate_reporting_token_content(msg)) but doesn't return the
generated value, so destruction happens inside the timed section; change these
closures (the ones invoking generate_reporting_token_content via .bench_refs at
the occurrences noted and the similar cases at the other two locations) to
return the produced token (e.g., return the Vec or token value) instead of only
black_box-ing it, so the benchmark measures creation cost while drop happens
outside the timed window—mirror the approach used in bench_jid_to_owned_access
where the produced value is returned.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: e87596b7-f23a-4c6a-8c6a-90e2bdf0055e

📥 Commits

Reviewing files that changed from the base of the PR and between be3d255 and e6d8ff6.

📒 Files selected for processing (2)
  • wacore/benches/reporting_token_benchmark.rs
  • wacore/binary/benches/binary_benchmark.rs

Comment thread wacore/benches/reporting_token_benchmark.rs
@jlucaso1
jlucaso1 merged commit 0bcc3a0 into main Jun 11, 2026
19 checks passed
@jlucaso1
jlucaso1 deleted the bench/get-children-borrow-input branch June 11, 2026 13:44
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