Skip to content

Conversation

@nnethercote
Copy link
Contributor

Some minor improvements to the generated code. In a stress test containing 100 invocations like bitflags! { struct F1: u32 { const A = 0b1; const B = 0b10; } } this reduced compile times by about 10%. The generated machine code is also a bit better in dev builds.

The generated code for various operations has local variables:
```
        pub const fn union(self, other: Self) -> Self {
            let f = self;
            let other = other;
            { Self::from_bits_retain(f.bits() | other.bits()) }
        }
```
These are present due to the way the relevant macros are structured.
This commit restructures the `__impl_bitflags` macro to avoid these
indirections, resulting in this code instead:
```
        pub const fn union(self, other: Self) -> Self {
            Self(self.0 | other.0)
        }
```
Benefits:
- `cargo expand` output is nicer.
- It is a little faster to compile.
- `__impl_bitflags!` becomes simpler, with many fewer parameters. E.g.
  `$self` instead of `$union0`, `$intersect0`, etc.
- The `__impl_bitflags!` call sites are also a little nicer, using
  `&self` instead of `f`.
It's simpler to just use `Self()` and `.0`. Here's an example of how
that changes the output for one method:
```
         pub const fn union(self, other: Self) -> Self {
-            Self::from_bits_retain(self.bits() | other.bits())
+            Self(self.0 | other.0)
         }
```
As well as being simpler, this change makes the code a tiny bit faster
to compile, and results in much better code quality (no function calls)
in dev builds.
Copy link
Member

@KodrAus KodrAus left a comment

Choose a reason for hiding this comment

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

Thanks @nnethercote! These changes all seem reasonable to me 👍

@KodrAus KodrAus merged commit 9e1cf3e into bitflags:main Aug 22, 2025
10 checks passed
@nnethercote nnethercote deleted the streamline-generated-code branch August 24, 2025 20:51
nnethercote added a commit to nnethercote/rust that referenced this pull request Aug 24, 2025
The `bitflags!` macro in the latest release has slightly streamlined
codegen. See bitflags/bitflags#458 for details.
Zalathar added a commit to Zalathar/rust that referenced this pull request Aug 25, 2025
…triplett

Update `bitflags` to 2.9.3.

The `bitflags!` macro in the latest release has slightly streamlined codegen. See bitflags/bitflags#458 for details.

r? `@yaahc`
Zalathar added a commit to Zalathar/rust that referenced this pull request Aug 25, 2025
…triplett

Update `bitflags` to 2.9.3.

The `bitflags!` macro in the latest release has slightly streamlined codegen. See bitflags/bitflags#458 for details.

r? `@yaahc`
rust-timer added a commit to rust-lang/rust that referenced this pull request Aug 25, 2025
Rollup merge of #145828 - nnethercote:bitflags-2.9.3, r=joshtriplett

Update `bitflags` to 2.9.3.

The `bitflags!` macro in the latest release has slightly streamlined codegen. See bitflags/bitflags#458 for details.

r? `@yaahc`
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.

2 participants