Skip to content

Conversation

@bmberger
Copy link
Contributor

@bmberger bmberger commented Nov 8, 2025

With optimization:
test r#ref::benches::bench_deref_mut_sized ... bench: 0.64 ns/iter (+/- 0.25)
test r#ref::benches::bench_deref_sized ... bench: 0.54 ns/iter (+/- 0.20)
test r#ref::benches::bench_from_bytes_sized ... bench: 5.71 ns/iter (+/- 1.59)
test r#ref::benches::bench_into_mut_sized ... bench: 0.66 ns/iter (+/- 0.05)
test r#ref::benches::bench_into_ref_sized ... bench: 0.88 ns/iter (+/- 0.08)

Without optimization:
test r#ref::benches::bench_deref_mut_sized ... bench: 0.74 ns/iter (+/- 0.09)
test r#ref::benches::bench_deref_sized ... bench: 0.69 ns/iter (+/- 0.10)
test r#ref::benches::bench_from_bytes_sized ... bench: 5.09 ns/iter (+/- 0.42)
test r#ref::benches::bench_into_mut_sized ... bench: 0.68 ns/iter (+/- 0.08)
test r#ref::benches::bench_into_ref_sized ... bench: 0.88 ns/iter (+/- 0.01)

Closes #2752

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @bmberger, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request introduces targeted optimizations for Ref methods within the zerocopy crate when dealing with Sized types. The goal is to improve performance by allowing direct pointer manipulation, avoiding unnecessary intermediate conversions. While initial benchmarks show only minimal performance changes, the update refines the internal handling of Sized types and includes new benchmarks to monitor these specific operations.

Highlights

  • Optimization for Sized Types: Implemented a specialized code path for Ref<_, T> methods (into_ref, into_mut, deref, deref_mut) when the generic type T is Sized.
  • Direct Pointer Manipulation: This optimization bypasses intermediate ByteSlice conversions by directly using Ptr for Sized types, aiming for improved efficiency.
  • New Benchmarks: Added dedicated benchmark tests to measure the performance of these optimized Ref methods specifically for Sized types.
  • Test Feature Enablement: Enabled the test feature in src/lib.rs to support the new benchmarks.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces an optimization for Sized types in several methods of Ref<_, T>. The logic seems sound, and the addition of benchmarks is a great way to validate the performance impact. My main feedback is regarding significant code duplication across four methods, which could be refactored to improve maintainability. I've provided a suggestion to use a macro to deduplicate this logic.

@bmberger bmberger force-pushed the optimize-ref-methods branch 2 times, most recently from f68da63 to 08ba7b2 Compare November 14, 2025 23:20
@bmberger bmberger requested a review from joshlf November 14, 2025 23:23
@bmberger bmberger force-pushed the optimize-ref-methods branch from 08ba7b2 to 2fef20b Compare December 16, 2025 01:17
@bmberger bmberger force-pushed the optimize-ref-methods branch from 2fef20b to d62f86c Compare December 17, 2025 22:21
@bmberger bmberger requested a review from joshlf December 23, 2025 00:07
@joshlf
Copy link
Member

joshlf commented Dec 23, 2025

We're probably about to make a bit of noise for you on this PR, but also hopefully make your life a bit easier, with #2860. Let's hold off working on this for a few days until #2860 lands?

@joshlf joshlf force-pushed the optimize-ref-methods branch 2 times, most recently from 02dd1cd to 3f7849b Compare January 6, 2026 20:18
@joshlf
Copy link
Member

joshlf commented Jan 6, 2026

Okay, I've rebased on main and force-pushed. @bmberger you should be able to download these changes and continue working on them.

Copy link
Member

@joshlf joshlf left a comment

Choose a reason for hiding this comment

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

Please update the commit message with new benchmark numbers, but otherwise LGTM!

@joshlf joshlf mentioned this pull request Jan 6, 2026
@joshlf joshlf force-pushed the optimize-ref-methods branch from 3f7849b to 1b91935 Compare January 6, 2026 21:00
@bmberger bmberger force-pushed the optimize-ref-methods branch from 1b91935 to 061dbe3 Compare January 6, 2026 21:06
@joshlf joshlf enabled auto-merge January 6, 2026 21:09
@joshlf joshlf force-pushed the optimize-ref-methods branch 2 times, most recently from b1fc793 to a2a8a49 Compare January 6, 2026 21:35
@codecov-commenter
Copy link

codecov-commenter commented Jan 6, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 92.02%. Comparing base (8dfa29b) to head (ab598ae).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2758      +/-   ##
==========================================
+ Coverage   91.89%   92.02%   +0.13%     
==========================================
  Files          20       20              
  Lines        5773     5870      +97     
==========================================
+ Hits         5305     5402      +97     
  Misses        468      468              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@joshlf joshlf added this pull request to the merge queue Jan 6, 2026
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Jan 6, 2026
@joshlf joshlf force-pushed the optimize-ref-methods branch from a2a8a49 to b023d10 Compare January 6, 2026 23:51
@joshlf joshlf enabled auto-merge January 6, 2026 23:52
With optimization:
test r#ref::benches::bench_deref_mut_sized  ... bench:           0.64 ns/iter (+/- 0.25)
test r#ref::benches::bench_deref_sized      ... bench:           0.54 ns/iter (+/- 0.20)
test r#ref::benches::bench_from_bytes_sized ... bench:           5.71 ns/iter (+/- 1.59)
test r#ref::benches::bench_into_mut_sized   ... bench:           0.66 ns/iter (+/- 0.05)
test r#ref::benches::bench_into_ref_sized   ... bench:           0.88 ns/iter (+/- 0.08)

Without optimization:
test r#ref::benches::bench_deref_mut_sized  ... bench:           0.74 ns/iter (+/- 0.09)
test r#ref::benches::bench_deref_sized      ... bench:           0.69 ns/iter (+/- 0.10)
test r#ref::benches::bench_from_bytes_sized ... bench:           5.09 ns/iter (+/- 0.42)
test r#ref::benches::bench_into_mut_sized   ... bench:           0.68 ns/iter (+/- 0.08)
test r#ref::benches::bench_into_ref_sized   ... bench:           0.88 ns/iter (+/- 0.01)

Closes google#2752

Co-authored-by: Joshua Liebow-Feeser <joshlf@google.com>
@joshlf joshlf force-pushed the optimize-ref-methods branch from b023d10 to ab598ae Compare January 7, 2026 00:04
@joshlf joshlf added this pull request to the merge queue Jan 7, 2026
Merged via the queue into google:main with commit 37b16a6 Jan 7, 2026
103 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Optimize Ref<_, T> methods when T: Sized

3 participants