Skip to content

Conversation

@bmberger
Copy link

@bmberger bmberger commented Nov 8, 2025

Benchmarks are surprisingly showing minimal impact...

With optimization:
test r#ref::tests::bench_deref_mut_sized ... bench: 0.23 ns/iter (+/- 0.03)
test r#ref::tests::bench_deref_sized ... bench: 0.44 ns/iter (+/- 0.04)
test r#ref::tests::bench_from_bytes_sized ... bench: 4.64 ns/iter (+/- 0.03)
test r#ref::tests::bench_into_mut_sized ... bench: 0.23 ns/iter (+/- 0.03)
test r#ref::tests::bench_into_ref_sized ... bench: 0.44 ns/iter (+/- 0.00)

Without optimization:
test r#ref::tests::bench_deref_mut_sized ... bench: 0.22 ns/iter (+/- 0.02)
test r#ref::tests::bench_deref_sized ... bench: 0.44 ns/iter (+/- 0.03)
test r#ref::tests::bench_from_bytes_sized ... bench: 4.66 ns/iter (+/- 0.31)
test r#ref::tests::bench_into_mut_sized ... bench: 0.23 ns/iter (+/- 0.03)
test r#ref::tests::bench_into_ref_sized ... bench: 0.44 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
Benchmarks are surprisingly showing minimal impact...

With optimization:
test r#ref::tests::bench_deref_mut_sized  ... bench:           0.72 ns/iter (+/- 0.09)
test r#ref::tests::bench_deref_sized      ... bench:           0.70 ns/iter (+/- 0.09)
test r#ref::tests::bench_from_bytes_sized ... bench:           5.27 ns/iter (+/- 0.69)
test r#ref::tests::bench_into_mut_sized   ... bench:           0.70 ns/iter (+/- 0.07)
test r#ref::tests::bench_into_ref_sized   ... bench:           0.94 ns/iter (+/- 0.08)

Without optimization:
test r#ref::tests::bench_deref_mut_sized  ... bench:           0.72 ns/iter (+/- 0.23)
test r#ref::tests::bench_deref_sized      ... bench:           0.73 ns/iter (+/- 0.09)
test r#ref::tests::bench_from_bytes_sized ... bench:           5.26 ns/iter (+/- 0.69)
test r#ref::tests::bench_into_mut_sized   ... bench:           0.70 ns/iter (+/- 0.08)
test r#ref::tests::bench_into_ref_sized   ... bench:           0.97 ns/iter (+/- 0.08)

Closes google#2752

Co-authored-by: Joshua Liebow-Feeser <joshlf@google.com>
@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?

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

2 participants