Skip to content

Conversation

@jswrenn
Copy link
Collaborator

@jswrenn jswrenn commented Feb 4, 2026

This commit parameterizes TryFromBytes::is_bit_valid and
Ptr::read over alignment, ensuring that well-aligned loads are
used at the leaves of validation whenever possible. This carries
a performance advantage over unaligned loads on many platforms.
This manifests in our API as an optimization for try_transmute!
and TryFromBytes::try_read_from_* for destination types with
alignments greater than 1. For trivially-aligned destination types,
this commit introduces an optimization FIXME noting that validation
could (and should) be performed in-place.

Makes progress towards #2981.


Latest Update: v5 — Compare vs v4

📚 Full Patch History

Links show the diff between the row version and the column version.

Version v4 v3 v2 v1 Base
v5 vs v4 vs v3 vs v2 vs v1 vs Base
v4 vs v3 vs v2 vs v1 vs Base
v3 vs v2 vs v1 vs Base
v2 vs v1 vs Base
v1 vs Base

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @jswrenn, 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 a significant performance enhancement to the zerocopy library by making its validation mechanisms alignment-aware. By parameterizing key validation functions with alignment information, the library can now utilize optimized aligned memory loads, leading to faster data validation. This change also includes updates to the API and documentation to reflect these new capabilities and sets the stage for further optimizations for trivially-aligned types.

Highlights

  • Performance Optimization for Validation: The core validation functions, TryFromBytes::is_bit_valid and Ptr::read, have been parameterized over alignment. This change ensures that well-aligned memory loads are used during validation whenever possible, which can significantly improve performance on platforms where unaligned loads are slower.
  • API Changes for Alignment Awareness: The is_bit_valid trait method now accepts a generic Alignment parameter, allowing for more precise control over memory access patterns during validation. The Ptr::read_unaligned method has been renamed to Ptr::read and its implementation now leverages the new alignment-aware Alignment::read method.
  • Improved Documentation and Future Work: New documentation has been added to try_read_from_bytes, try_read_from_prefix, and try_read_from_suffix methods, explaining the performance benefits of using aligned stack allocations for validation. A FIXME note has also been added, indicating a future optimization to perform in-place validation for trivially-aligned types (alignment of 1).
  • Dependency Update: The zerocopy and zerocopy-derive crates have been updated to version 0.8.37 in Cargo.lock.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Changelog
  • Cargo.lock
    • Updated zerocopy and zerocopy-derive dependencies to version 0.8.37.
  • src/impls.rs
    • Replaced read_unaligned() with the new read() method in TryFromBytes implementations for char and nonzero types.
    • Modified is_bit_valid for UnsafeCell<T> to accept a generic Alignment parameter.
  • src/lib.rs
    • Updated the TryFromBytes::is_bit_valid trait method signature to include a generic Alignment parameter.
    • Added detailed performance considerations to the documentation of try_read_from_bytes, try_read_from_prefix, and try_read_from_suffix, explaining the use of aligned stack allocations and noting a FIXME for in-place validation of trivially-aligned types.
  • src/macros.rs
    • Adjusted is_bit_valid macro implementations to correctly handle the new generic Alignment parameter.
  • src/pointer/invariant.rs
    • Introduced a read method to the Alignment trait.
    • Implemented the read method for Unaligned and Aligned enums, providing specific logic for each alignment type.
  • src/pointer/mod.rs
    • Refined the Maybe type alias by simplifying its generic parameters, specifically removing the explicit Aliasing parameter and defaulting Alignment to invariant::Unaligned.
  • src/pointer/ptr.rs
    • Renamed the read_unaligned method to read and updated its internal implementation to leverage the new Alignment::read method for alignment-aware data access.
  • src/util/macros.rs
    • Modified is_bit_valid macro implementations to support the new generic Alignment parameter.
  • zerocopy-derive/src/derive/try_from_bytes.rs
    • Updated derive_is_bit_valid functions for enums, structs, and unions to accept a generic _ZcAlignment parameter.
    • Replaced read_unaligned() with read() in the derived is_bit_valid implementation for enums.
  • zerocopy-derive/src/output_tests/expected/*.rs
    • Updated various expected output files for TryFromBytes derivations to reflect the changes in the is_bit_valid signature, including the addition of the _ZcAlignment generic parameter and #[inline(always)] attribute.
Activity
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.

@jswrenn jswrenn force-pushed the G74407a530f46f997b4faaeac52452ebc9892b2ae branch from 8657184 to b8a434a Compare February 4, 2026 20:03
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 a well-thought-out refactoring to make validation aware of memory alignment, which can lead to significant performance improvements. The core change is parameterizing TryFromBytes::is_bit_valid and Ptr::read over a new Alignment trait, allowing the system to use more efficient aligned loads when possible. The implementation is clean, consistent, and applied thoroughly across the library, including the derive macros. The documentation updates and FIXME comments regarding future in-place validation optimizations are also valuable additions. I found no issues with the changes.

@jswrenn jswrenn requested a review from joshlf February 4, 2026 20:04
@jswrenn jswrenn force-pushed the G74407a530f46f997b4faaeac52452ebc9892b2ae branch from b8a434a to 4446f48 Compare February 4, 2026 20:05
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.

🔥

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 address existing comments, but otherwise approved.

@jswrenn jswrenn force-pushed the G74407a530f46f997b4faaeac52452ebc9892b2ae branch from 4446f48 to 4ba09a3 Compare February 4, 2026 20:30
This commit parameterizes `TryFromBytes::is_bit_valid` and
`Ptr::read` over alignment, ensuring that well-aligned loads are
used at the leaves of validation whenever possible. This carries
a performance advantage over unaligned loads on many platforms.
This manifests in our API as an optimization for `try_transmute!`
and `TryFromBytes::try_read_from_*` for destination types with
alignments greater than 1. For trivially-aligned destination types,
this commit introduces an optimization FIXME noting that validation
could (and should) be performed in-place.

Makes progress towards #2981.

gherrit-pr-id: G74407a530f46f997b4faaeac52452ebc9892b2ae
@jswrenn jswrenn force-pushed the G74407a530f46f997b4faaeac52452ebc9892b2ae branch from 4ba09a3 to 8923b44 Compare February 4, 2026 20:40
@jswrenn jswrenn enabled auto-merge February 4, 2026 20:40
@codecov-commenter
Copy link

Codecov Report

❌ Patch coverage is 67.50000% with 13 lines in your changes missing coverage. Please review.
✅ Project coverage is 91.87%. Comparing base (6b81374) to head (8923b44).

Files with missing lines Patch % Lines
src/pointer/invariant.rs 50.00% 7 Missing ⚠️
src/macros.rs 0.00% 6 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2985      +/-   ##
==========================================
- Coverage   92.02%   91.87%   -0.15%     
==========================================
  Files          19       20       +1     
  Lines        6029     6057      +28     
==========================================
+ Hits         5548     5565      +17     
- Misses        481      492      +11     

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

@jswrenn jswrenn added this pull request to the merge queue Feb 4, 2026
Merged via the queue into main with commit d296c26 Feb 4, 2026
113 of 124 checks passed
@jswrenn jswrenn deleted the G74407a530f46f997b4faaeac52452ebc9892b2ae branch February 4, 2026 21:18
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.

3 participants