Skip to content

[codegen] Test and document codegen under different layouts#3085

Open
jswrenn wants to merge 2 commits intomainfrom
G39e297d5b891f5115ef9607b090c5fbe95427d68
Open

[codegen] Test and document codegen under different layouts#3085
jswrenn wants to merge 2 commits intomainfrom
G39e297d5b891f5115ef9607b090c5fbe95427d68

Conversation

@jswrenn
Copy link
Collaborator

@jswrenn jswrenn commented Mar 6, 2026

This improves the robustness of our suite to different code paths, and
illustrates in our documentation the relationship between type
complexity and codegen complexity.


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
⬇️ Download this PR

Branch

git fetch origin refs/heads/G39e297d5b891f5115ef9607b090c5fbe95427d68 && git checkout -b pr-G39e297d5b891f5115ef9607b090c5fbe95427d68 FETCH_HEAD

Checkout

git fetch origin refs/heads/G39e297d5b891f5115ef9607b090c5fbe95427d68 && git checkout FETCH_HEAD

Cherry Pick

git fetch origin refs/heads/G39e297d5b891f5115ef9607b090c5fbe95427d68 && git cherry-pick FETCH_HEAD

Pull

git pull origin refs/heads/G39e297d5b891f5115ef9607b090c5fbe95427d68

Stacked PRs enabled by GHerrit.

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello, 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 significantly improves the zerocopy library's code generation testing and documentation. By categorizing benchmarks based on type memory layouts (static, dynamic, and dynamically padded), it provides clearer insights into the performance characteristics and compiler optimizations for different zerocopy trait implementations. The new documentation structure, featuring interactive tabs, makes it easier to compare code generation outputs and understand the relationship between type complexity and codegen efficiency.

Highlights

  • Refactored Codegen Benchmarks: Existing codegen benchmarks were reorganized and new ones introduced to explicitly test zerocopy traits under various memory layouts: static size, dynamic size, and dynamically padded types. This provides a more granular view of performance characteristics.
  • Enhanced Codegen Documentation: The documentation system was significantly upgraded with new Rustdoc macros (codegen_section!, codegen_example_suite!, etc.). These macros generate interactive tabbed displays for benchmark results, allowing users to easily compare code generation outputs for different type layouts directly within the documentation.
  • Updated Rustdoc Styling: The rustdoc/style.css file was modified to support the new tabbed documentation layout. This includes using a CSS variable for dynamic grid column counts and adding specific styling for the interactive elements, improving the visual presentation and user experience.
  • Streamlined Documentation Generation: The --generate-macro-expansion flag was removed from the tools/doc.sh script, simplifying the rustdoc command and potentially speeding up documentation builds.

🧠 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
  • benches/formats/coco.rs
    • Renamed to benches/formats/coco_dynamic_padding.rs.
  • benches/formats/coco_no_padding.rs
    • Renamed to benches/formats/coco_dynamic_size.rs.
  • benches/formats/coco_static_size.rs
    • Added a new format definition for static-sized types.
  • benches/read_from_bytes.rs
    • Added a new benchmark for read_from_bytes with static size.
  • benches/read_from_bytes.x86-64
    • Added x86-64 assembly output for read_from_bytes_static_size.
  • benches/read_from_bytes.x86-64.mca
    • Added machine code analysis for read_from_bytes_static_size.
  • benches/read_from_prefix.rs
    • Added a new benchmark for read_from_prefix with static size.
  • benches/read_from_prefix.x86-64
    • Added x86-64 assembly output for read_from_prefix_static_size.
  • benches/read_from_prefix.x86-64.mca
    • Added machine code analysis for read_from_prefix_static_size.
  • benches/read_from_suffix.rs
    • Added a new benchmark for read_from_suffix with static size.
  • benches/read_from_suffix.x86-64
    • Added x86-64 assembly output for read_from_suffix_static_size.
  • benches/read_from_suffix.x86-64.mca
    • Added machine code analysis for read_from_suffix_static_size.
  • benches/ref_from_bytes.rs
    • Removed the old ref_from_bytes benchmark.
  • benches/ref_from_bytes.x86-64
    • Renamed to benches/ref_from_bytes_dynamic_padding.x86-64.
  • benches/ref_from_bytes.x86-64.mca
    • Renamed to benches/ref_from_bytes_dynamic_padding.x86-64.mca.
  • benches/ref_from_bytes_dynamic_padding.rs
    • Added a new benchmark for ref_from_bytes with dynamic padding.
  • benches/ref_from_bytes_dynamic_size.rs
    • Added a new benchmark for ref_from_bytes with dynamic size.
  • benches/ref_from_bytes_dynamic_size.x86-64
    • Added x86-64 assembly output for ref_from_bytes_dynamic_size.
  • benches/ref_from_bytes_dynamic_size.x86-64.mca
    • Added machine code analysis for ref_from_bytes_dynamic_size.
  • benches/ref_from_bytes_static_size.rs
    • Added a new benchmark for ref_from_bytes with static size.
  • benches/ref_from_bytes_static_size.x86-64
    • Added x86-64 assembly output for ref_from_bytes_static_size.
  • benches/ref_from_bytes_static_size.x86-64.mca
    • Added machine code analysis for ref_from_bytes_static_size.
  • benches/ref_from_bytes_with_elems.rs
    • Removed the old ref_from_bytes_with_elems benchmark.
  • benches/ref_from_bytes_with_elems.x86-64
    • Renamed to benches/ref_from_bytes_with_elems_dynamic_padding.x86-64.
  • benches/ref_from_bytes_with_elems.x86-64.mca
    • Renamed to benches/ref_from_bytes_with_elems_dynamic_padding.x86-64.mca.
  • benches/ref_from_bytes_with_elems_dynamic_padding.rs
    • Added a new benchmark for ref_from_bytes_with_elems with dynamic padding.
  • benches/ref_from_bytes_with_elems_dynamic_size.rs
    • Added a new benchmark for ref_from_bytes_with_elems with dynamic size.
  • benches/ref_from_bytes_with_elems_dynamic_size.x86-64
    • Added x86-64 assembly output for ref_from_bytes_with_elems_dynamic_size.
  • benches/ref_from_bytes_with_elems_dynamic_size.x86-64.mca
    • Added machine code analysis for ref_from_bytes_with_elems_dynamic_size.
  • benches/ref_from_prefix.rs
    • Renamed to benches/ref_from_prefix_static_size.rs and updated its path.
  • benches/ref_from_prefix.x86-64
    • Renamed to benches/ref_from_prefix_dynamic_padding.x86-64.
  • benches/ref_from_prefix.x86-64.mca
    • Renamed to benches/ref_from_prefix_dynamic_padding.x86-64.mca.
  • benches/ref_from_prefix_dynamic_padding.rs
    • Added a new benchmark for ref_from_prefix with dynamic padding.
  • benches/ref_from_prefix_dynamic_size.rs
    • Added a new benchmark for ref_from_prefix with dynamic size.
  • benches/ref_from_prefix_dynamic_size.x86-64
    • Added x86-64 assembly output for ref_from_prefix_dynamic_size.
  • benches/ref_from_prefix_dynamic_size.x86-64.mca
    • Added machine code analysis for ref_from_prefix_dynamic_size.
  • benches/ref_from_prefix_static_size.x86-64
    • Added x86-64 assembly output for ref_from_prefix_static_size.
  • benches/ref_from_prefix_static_size.x86-64.mca
    • Added machine code analysis for ref_from_prefix_static_size.
  • benches/ref_from_prefix_with_elems.rs
    • Renamed to benches/ref_from_prefix_with_elems_dynamic_size.rs and updated its path.
  • benches/ref_from_prefix_with_elems.x86-64
    • Renamed to benches/ref_from_prefix_with_elems_dynamic_padding.x86-64.
  • benches/ref_from_prefix_with_elems.x86-64.mca
    • Renamed to benches/ref_from_prefix_with_elems_dynamic_padding.x86-64.mca.
  • benches/ref_from_prefix_with_elems_dynamic_padding.rs
    • Added a new benchmark for ref_from_prefix_with_elems with dynamic padding.
  • benches/ref_from_prefix_with_elems_dynamic_size.x86-64
    • Added x86-64 assembly output for ref_from_prefix_with_elems_dynamic_size.
  • benches/ref_from_prefix_with_elems_dynamic_size.x86-64.mca
    • Added machine code analysis for ref_from_prefix_with_elems_dynamic_size.
  • benches/ref_from_suffix.rs
    • Renamed to benches/ref_from_suffix_static_size.rs and updated its path.
  • benches/ref_from_suffix.x86-64
    • Renamed to benches/ref_from_suffix_dynamic_padding.x86-64.
  • benches/ref_from_suffix.x86-64.mca
    • Renamed to benches/ref_from_suffix_dynamic_padding.x86-64.mca.
  • benches/ref_from_suffix_dynamic_padding.rs
    • Added a new benchmark for ref_from_suffix with dynamic padding.
  • benches/ref_from_suffix_dynamic_size.rs
    • Added a new benchmark for ref_from_suffix with dynamic size.
  • benches/ref_from_suffix_dynamic_size.x86-64
    • Added x86-64 assembly output for ref_from_suffix_dynamic_size.
  • benches/ref_from_suffix_dynamic_size.x86-64.mca
    • Added machine code analysis for ref_from_suffix_dynamic_size.
  • benches/ref_from_suffix_static_size.x86-64
    • Added x86-64 assembly output for ref_from_suffix_static_size.
  • benches/ref_from_suffix_static_size.x86-64.mca
    • Added machine code analysis for ref_from_suffix_static_size.
  • benches/ref_from_suffix_with_elems.rs
    • Renamed to benches/ref_from_suffix_with_elems_dynamic_size.rs and updated its path.
  • benches/ref_from_suffix_with_elems.x86-64
    • Renamed to benches/ref_from_suffix_with_elems_dynamic_padding.x86-64.
  • benches/ref_from_suffix_with_elems.x86-64.mca
    • Renamed to benches/ref_from_suffix_with_elems_dynamic_padding.x86-64.mca.
  • benches/ref_from_suffix_with_elems_dynamic_padding.rs
    • Added a new benchmark for ref_from_suffix_with_elems with dynamic padding.
  • benches/ref_from_suffix_with_elems_dynamic_size.x86-64
    • Added x86-64 assembly output for ref_from_suffix_with_elems_dynamic_size.
  • benches/ref_from_suffix_with_elems_dynamic_size.x86-64.mca
    • Added machine code analysis for ref_from_suffix_with_elems_dynamic_size.
  • benches/transmute_ref.rs
    • Renamed to benches/transmute_ref_dynamic_size.rs and updated its path.
  • benches/transmute_ref.x86-64
    • Renamed to benches/transmute_ref_dynamic_size.x86-64.
  • benches/transmute_ref.x86-64.mca
    • Renamed to benches/transmute_ref_dynamic_size.x86-64.mca.
  • benches/transmute_ref_static_size.rs
    • Added a new benchmark for transmute_ref with static size.
  • benches/transmute_ref_static_size.x86-64
    • Added x86-64 assembly output for transmute_ref_static_size.
  • benches/transmute_ref_static_size.x86-64.mca
    • Added machine code analysis for transmute_ref_static_size.
  • benches/try_read_from_bytes.rs
    • Added a new benchmark for try_read_from_bytes with static size.
  • benches/try_read_from_bytes.x86-64
    • Added x86-64 assembly output for try_read_from_bytes_static_size.
  • benches/try_read_from_bytes.x86-64.mca
    • Added machine code analysis for try_read_from_bytes_static_size.
  • benches/try_read_from_prefix.rs
    • Added a new benchmark for try_read_from_prefix with static size.
  • benches/try_read_from_prefix.x86-64
    • Added x86-64 assembly output for try_read_from_prefix_static_size.
  • benches/try_read_from_prefix.x86-64.mca
    • Added machine code analysis for try_read_from_prefix_static_size.
  • benches/try_read_from_suffix.rs
    • Added a new benchmark for try_read_from_suffix with static size.
  • benches/try_read_from_suffix.x86-64
    • Added x86-64 assembly output for try_read_from_suffix_static_size.
  • benches/try_read_from_suffix.x86-64.mca
    • Added machine code analysis for try_read_from_suffix_static_size.
  • benches/try_ref_from_bytes.rs
    • Removed the old try_ref_from_bytes benchmark.
  • benches/try_ref_from_bytes.x86-64
    • Renamed to benches/try_ref_from_bytes_dynamic_padding.x86-64.
  • benches/try_ref_from_bytes.x86-64.mca
    • Renamed to benches/try_ref_from_bytes_dynamic_padding.x86-64.mca.
  • benches/try_ref_from_bytes_dynamic_padding.rs
    • Added a new benchmark for try_ref_from_bytes with dynamic padding.
  • benches/try_ref_from_bytes_dynamic_size.rs
    • Added a new benchmark for try_ref_from_bytes with dynamic size.
  • benches/try_ref_from_bytes_dynamic_size.x86-64
    • Added x86-64 assembly output for try_ref_from_bytes_dynamic_size.
  • benches/try_ref_from_bytes_dynamic_size.x86-64.mca
    • Added machine code analysis for try_ref_from_bytes_dynamic_size.
  • benches/try_ref_from_bytes_static_size.rs
    • Added a new benchmark for try_ref_from_bytes with static size.
  • benches/try_ref_from_bytes_static_size.x86-64
    • Added x86-64 assembly output for try_ref_from_bytes_static_size.
  • benches/try_ref_from_bytes_static_size.x86-64.mca
    • Added machine code analysis for try_ref_from_bytes_static_size.
  • benches/try_ref_from_bytes_with_elems.rs
    • Removed the old try_ref_from_bytes_with_elems benchmark.
  • benches/try_ref_from_bytes_with_elems.x86-64
    • Renamed to benches/try_ref_from_bytes_with_elems_dynamic_padding.x86-64.
  • benches/try_ref_from_bytes_with_elems.x86-64.mca
    • Renamed to benches/try_ref_from_bytes_with_elems_dynamic_padding.x86-64.mca.
  • benches/try_ref_from_bytes_with_elems_dynamic_padding.rs
    • Added a new benchmark for try_ref_from_bytes_with_elems with dynamic padding.
  • benches/try_ref_from_bytes_with_elems_dynamic_size.rs
    • Added a new benchmark for try_ref_from_bytes_with_elems with dynamic size.
  • benches/try_ref_from_bytes_with_elems_dynamic_size.x86-64
    • Added x86-64 assembly output for try_ref_from_bytes_with_elems_dynamic_size.
  • benches/try_ref_from_bytes_with_elems_dynamic_size.x86-64.mca
    • Added machine code analysis for try_ref_from_bytes_with_elems_dynamic_size.
  • benches/try_ref_from_prefix.rs
    • Renamed to benches/try_ref_from_prefix_static_size.rs and updated its path.
  • benches/try_ref_from_prefix.x86-64
    • Renamed to benches/try_ref_from_prefix_dynamic_padding.x86-64.
  • benches/try_ref_from_prefix.x86-64.mca
    • Renamed to benches/try_ref_from_prefix_dynamic_padding.x86-64.mca.
  • benches/try_ref_from_prefix_dynamic_padding.rs
    • Added a new benchmark for try_ref_from_prefix with dynamic padding.
  • benches/try_ref_from_prefix_dynamic_size.rs
    • Added a new benchmark for try_ref_from_prefix with dynamic size.
  • benches/try_ref_from_prefix_dynamic_size.x86-64
    • Added x86-64 assembly output for try_ref_from_prefix_dynamic_size.
  • benches/try_ref_from_prefix_dynamic_size.x86-64.mca
    • Added machine code analysis for try_ref_from_prefix_dynamic_size.
  • benches/try_ref_from_prefix_static_size.x86-64
    • Added x86-64 assembly output for try_ref_from_prefix_static_size.
  • benches/try_ref_from_prefix_static_size.x86-64.mca
    • Added machine code analysis for try_ref_from_prefix_static_size.
  • benches/try_ref_from_prefix_with_elems.rs
    • Renamed to benches/try_ref_from_prefix_with_elems_dynamic_size.rs and updated its path.
  • benches/try_ref_from_prefix_with_elems.x86-64
    • Renamed to benches/try_ref_from_prefix_with_elems_dynamic_padding.x86-64.
  • benches/try_ref_from_prefix_with_elems.x86-64.mca
    • Renamed to benches/try_ref_from_prefix_with_elems_dynamic_padding.x86-64.mca.
  • benches/try_ref_from_prefix_with_elems_dynamic_padding.rs
    • Added a new benchmark for try_ref_from_prefix_with_elems with dynamic padding.
  • benches/try_ref_from_prefix_with_elems_dynamic_size.x86-64
    • Added x86-64 assembly output for try_ref_from_prefix_with_elems_dynamic_size.
  • benches/try_ref_from_prefix_with_elems_dynamic_size.x86-64.mca
    • Added machine code analysis for try_ref_from_prefix_with_elems_dynamic_size.
  • benches/try_ref_from_suffix.rs
    • Renamed to benches/try_ref_from_suffix_static_size.rs and updated its path.
  • benches/try_ref_from_suffix.x86-64
    • Renamed to benches/try_ref_from_suffix_dynamic_padding.x86-64.
  • benches/try_ref_from_suffix.x86-64.mca
    • Renamed to benches/try_ref_from_suffix_dynamic_padding.x86-64.mca.
  • benches/try_ref_from_suffix_dynamic_padding.rs
    • Added a new benchmark for try_ref_from_suffix with dynamic padding.
  • benches/try_ref_from_suffix_dynamic_size.rs
    • Added a new benchmark for try_ref_from_suffix with dynamic size.
  • benches/try_ref_from_suffix_dynamic_size.x86-64
    • Added x86-64 assembly output for try_ref_from_suffix_dynamic_size.
  • benches/try_ref_from_suffix_dynamic_size.x86-64.mca
    • Added machine code analysis for try_ref_from_suffix_dynamic_size.
  • benches/try_ref_from_suffix_static_size.x86-64
    • Added x86-64 assembly output for try_ref_from_suffix_static_size.
  • benches/try_ref_from_suffix_static_size.x86-64.mca
    • Added machine code analysis for try_ref_from_suffix_static_size.
  • benches/try_ref_from_suffix_with_elems.rs
    • Renamed to benches/try_ref_from_suffix_with_elems_dynamic_size.rs and updated its path.
  • benches/try_ref_from_suffix_with_elems.x86-64
    • Renamed to benches/try_ref_from_suffix_with_elems_dynamic_padding.x86-64.
  • benches/try_ref_from_suffix_with_elems.x86-64.mca
    • Renamed to benches/try_ref_from_suffix_with_elems_dynamic_padding.x86-64.mca.
  • benches/try_ref_from_suffix_with_elems_dynamic_padding.rs
    • Added a new benchmark for try_ref_from_suffix_with_elems with dynamic padding.
  • benches/try_ref_from_suffix_with_elems_dynamic_size.x86-64
    • Added x86-64 assembly output for try_ref_from_suffix_with_elems_dynamic_size.
  • benches/try_ref_from_suffix_with_elems_dynamic_size.x86-64.mca
    • Added machine code analysis for try_ref_from_suffix_with_elems_dynamic_size.
  • benches/try_transmute_ref.rs
    • Renamed to benches/try_transmute_ref_dynamic_size.rs and updated its path.
  • benches/try_transmute_ref.x86-64
    • Renamed to benches/try_transmute_ref_dynamic_size.x86-64.
  • benches/try_transmute_ref.x86-64.mca
    • Renamed to benches/try_transmute_ref_dynamic_size.x86-64.mca.
  • benches/try_transmute_ref_static_size.rs
    • Added a new benchmark for try_transmute_ref with static size.
  • benches/try_transmute_ref_static_size.x86-64
    • Added x86-64 assembly output for try_transmute_ref_static_size.
  • benches/try_transmute_ref_static_size.x86-64.mca
    • Added machine code analysis for try_transmute_ref_static_size.
  • rustdoc/style.css
    • Updated CSS for codegen tabs to use a CSS variable for column count and added styling for the 'Click one of the above headers' message.
    • Updated the CSS selector for open details summary to use the direct child combinator.
  • src/lib.rs
    • Updated documentation for TryFromBytes and FromBytes traits to use the new codegen_section! macro, enabling multi-variant codegen displays.
  • src/macros.rs
    • Updated documentation for transmute_ref! and try_transmute_ref! macros to use the new codegen_section! macro.
  • src/util/macros.rs
    • Introduced new macros (docstring!, tabs!, codegen_example!, codegen_example_suite!, codegen_preamble!, codegen_section!) to improve modularity and flexibility of codegen documentation.
  • tests/codegen.rs
    • Added a println! to show which codegen test is running and removed a redundant println!.
  • tools/doc.sh
    • Removed the --generate-macro-expansion flag from the rustdoc command.
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.

@codecov-commenter
Copy link

codecov-commenter commented Mar 6, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 91.87%. Comparing base (5eafa06) to head (87f0c90).

Additional details and impacted files
@@                            Coverage Diff                             @@
##           G04affc58395810ba04b941f14dfdc20563fa850b    #3085   +/-   ##
==========================================================================
  Coverage                                      91.87%   91.87%           
==========================================================================
  Files                                             20       20           
  Lines                                           6057     6057           
==========================================================================
  Hits                                            5565     5565           
  Misses                                           492      492           

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

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 significant refactoring of the benchmark tests and their documentation to showcase codegen under different data layouts. The changes are extensive, renaming and adding many files to create separate benchmarks for static_size, dynamic_size, and dynamic_padding layouts. The documentation macros have been completely overhauled for better modularity and to support these new tabbed examples. While the changes are generally excellent, I've found a minor issue in the CSS and a potential improvement in the new macro definitions.

.codegen-tabs details[open] :is(summary) {
.codegen-tabs details[open] > summary {
background-color: var(--code-block-background-color);
border-bottom-color: var(--target-border-color);
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

The use of :is(summary) here is unnecessary as summary is the only valid child of details that can be a summary. Using the child combinator > is more direct and slightly more performant. It also improves clarity by being more specific about the intended structure.

.codegen-tabs details[open] > summary {

Comment on lines +870 to +893
macro_rules! tabs {
(
name = $name:expr,
arity = $arity:literal,
$([
$($open:ident)?
@index $n:literal
@title $title:literal
$(#[doc = $content:expr])*
]),*
) => {
concat!("
<div class='codegen-tabs' style='--arity: ", $arity ,"'>", $(concat!("
<details name='tab-", $name,"' style='--n: ", $n ,"'", $(stringify!($open),)*">
<summary><h6>", $title, "</h6></summary>
<div>

```ignore
",
include_str!(concat!("../benches/", $name, ".x86-64")),
"```
", $($content, "\n",)* "
\
</div>
</details>
<details name='tab-",
$name,
"' style='--n: 4'>
<summary>
<h6>Machine Code Analysis</h6>
</summary>
<div>

### Replication
</details>"),)*
"</div>")
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

The logic for processing doc comments within the tabs! macro is a duplication of the logic in the docstring! macro. To improve maintainability and reduce code duplication, you could refactor tabs! to use docstring!. This would require a bit of adjustment to how the doc comments are passed and processed within the macro system, but would centralize the doc comment processing logic.

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 is a significant and well-executed refactoring of the code generation documentation. It introduces a more flexible and powerful macro-based system for generating documentation with tabbed examples for different memory layouts. This greatly improves the documentation's utility for understanding the relationship between type complexity and codegen complexity. The addition of new benchmark variants for static size, dynamic size, and dynamic padding is also a valuable improvement to the test suite's robustness. The changes are consistent and well-structured. I have one suggestion to improve the documentation further.

Comment on lines +927 to +929
/// ```plain
#[doc = include_str!(concat!("../benches/", $bench, ".x86-64.mca"))]
/// ```
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

The previous version of the documentation included instructions on how to replicate the machine code analysis using cargo-asm. This was very useful for users who wanted to run the analysis themselves. This information seems to have been lost in the refactoring.

Consider adding it back to the "Machine Code Analysis" tab. The command should be updated to be more accurate, including the --target-cpu and --simplify flags, and the correct function name.

For example:

                @index 4
                @title "Machine Code Analysis"
                /// ### Replication
                ///
                /// You may replicate this analysis on your device with [`cargo-show-asm`] by running:
                ///
                /// [`cargo-show-asm`]: https://github.com/pacak/cargo-show-asm
                ///
                /// ```bash
                #[doc = concat!("cargo asm --bench ", $bench, " --target-cpu x86-64 --simplify --mca bench_", $bench)]
                /// ```
                ///
                /// ### Results
                ///
                /// ```plain
                #[doc = include_str!(concat!("../benches/", $bench, ".x86-64.mca"))]
                /// ```
                /// ### Replication
                ///
                /// You may replicate this analysis on your device with [`cargo-show-asm`] by running:
                ///
                /// [`cargo-show-asm`]: https://github.com/pacak/cargo-show-asm
                ///
                /// ```bash
                #[doc = concat!("cargo asm --bench ", $bench, " --target-cpu x86-64 --simplify --mca bench_", $bench)]
                /// ```
                ///
                /// ### Results
                ///
                /// ```plain
                #[doc = include_str!(concat!("../benches/", $bench, ".x86-64.mca"))]
                /// ```

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 refactors the code generation benchmark documentation within the zerocopy crate. The changes involve replacing the existing codegen_tabs! macro with a new, more flexible suite of macros (tabs!, codegen_example!, codegen_example_suite!, codegen_preamble!, and codegen_section!). This new macro system allows for the documentation of multiple benchmark variants (static size, dynamic size, and dynamic padding) using HTML tabs in rustdoc, providing a more organized and detailed view of code generation. Consequently, numerous benchmark definitions in src/lib.rs and src/macros.rs have been updated to utilize the new codegen_section! macro, and new benchmark files, along with their assembly and machine code analysis outputs, have been added for these variants. The rustdoc/style.css file was also updated to support the new tabbed layout. Review comments indicate issues with the new documentation macros, specifically that the tabs! macro incorrectly generates the open HTML attribute, contains a stray backslash in its output, and has a mismatch in how it expects the open argument from codegen_example_suite!. The reviewer suggests using helper arms in the tabs! macro to correctly handle the optional open attribute.

Note: Security Review did not run due to the size of the PR.

Comment on lines +870 to +963
macro_rules! tabs {
(
name = $name:expr,
arity = $arity:literal,
$([
$($open:ident)?
@index $n:literal
@title $title:literal
$(#[doc = $content:expr])*
]),*
) => {
concat!("
<div class='codegen-tabs' style='--arity: ", $arity ,"'>", $(concat!("
<details name='tab-", $name,"' style='--n: ", $n ,"'", $(stringify!($open),)*">
<summary><h6>", $title, "</h6></summary>
<div>

```ignore
",
include_str!(concat!("../benches/", $name, ".x86-64")),
"```
", $($content, "\n",)* "
\
</div>
</details>
<details name='tab-",
$name,
"' style='--n: 4'>
<summary>
<h6>Machine Code Analysis</h6>
</summary>
<div>

### Replication
</details>"),)*
"</div>")
}
}

You may replicate this analysis on your device with [`cargo-show-asm`] by running:
/// Generates the HTML for a single benchmark example.
#[allow(unused)]
macro_rules! codegen_example {
(format = $format:expr, bench = $bench:expr) => {
tabs!(
name = $bench,
arity = 4,
[
@index 1
@title "Format"
/// ```ignore
#[doc = include_str!(concat!("../benches/formats/", $format, ".rs"))]
/// ```
],
[
@index 2
@title "Benchmark"
/// ```ignore
#[doc = include_str!(concat!("../benches/", $bench, ".rs"))]
/// ```
],
[
open
@index 3
@title "Assembly"
/// ```plain
#[doc = include_str!(concat!("../benches/", $bench, ".x86-64"))]
/// ```
],
[
@index 4
@title "Machine Code Analysis"
/// ```plain
#[doc = include_str!(concat!("../benches/", $bench, ".x86-64.mca"))]
/// ```
]
)
}
}

[`cargo-show-asm`]: https://github.com/pacak/cargo-show-asm
/// Generate the HTML for a suite of benchmark examples.
#[allow(unused)]
macro_rules! codegen_example_suite {
(
bench = $bench:expr,
format = $format:expr,
arity = $arity:literal,
$([
$($open:ident)?
@index $index:literal
@title $title:literal
@variant $variant:literal
]),*
) => {
tabs!(
name = $bench,
arity = $arity,
$([
$($open)*
@index $index
@title $title
#[doc = codegen_example!(
format = concat!($format, "_", $variant),
bench = concat!($bench, "_", $variant)
)]
]),*
)
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

There are a few related issues in the new documentation generation macros:

  1. In tabs! (line 883), the $(stringify!($open),)* part generates an invalid open attribute. It produces something like <details ... "open"> instead of the correct boolean attribute <details ... open>. The repetition * and comma are also problematic.
  2. In tabs! (line 888), there's a stray backslash \ inside a string literal that will be rendered in the HTML. This seems to be a typo and should be removed.
  3. In codegen_example_suite! (line 953), $($open)* is passed to tabs!. This should be $($open)? to match the optional ident pattern in tabs!.

A common way to fix the conditional open attribute is to use helper arms on the tabs! macro.

@jswrenn jswrenn force-pushed the G39e297d5b891f5115ef9607b090c5fbe95427d68 branch from 6f24e9a to efab483 Compare March 6, 2026 20:54
@jswrenn jswrenn force-pushed the G39e297d5b891f5115ef9607b090c5fbe95427d68 branch 2 times, most recently from 89ab1c2 to a9921b5 Compare March 6, 2026 23:53
@jswrenn jswrenn force-pushed the G04affc58395810ba04b941f14dfdc20563fa850b branch from 11a76fa to 57741d2 Compare March 6, 2026 23:53
jswrenn added 2 commits March 7, 2026 00:35
For `TryFromBytes` and `FromBytes` tests:
- Alignment is now 4.
- Minimum size is now 12.
- Trailing padding may be dynamically present.

For `transmute_ref!` and `try_transmute_ref!`:
- Metadata fixup required.

gherrit-pr-id: G04affc58395810ba04b941f14dfdc20563fa850b
This improves the robustness of our suite to different code paths, and
illustrates in our documentation the relationship between type
complexity and codegen complexity.

gherrit-pr-id: G39e297d5b891f5115ef9607b090c5fbe95427d68
@jswrenn jswrenn force-pushed the G39e297d5b891f5115ef9607b090c5fbe95427d68 branch from a9921b5 to 87f0c90 Compare March 7, 2026 00:35
@jswrenn jswrenn force-pushed the G04affc58395810ba04b941f14dfdc20563fa850b branch from 57741d2 to 5eafa06 Compare March 7, 2026 00:35
Base automatically changed from G04affc58395810ba04b941f14dfdc20563fa850b to main March 7, 2026 01:10
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