Repository navigation
Closed
Description
Currently, bitflags::all() outputs 6 + 20n lines of LLVM IR in debug builds, and 8 + 14n on release builds, where n is the number of flags. I first noticed this with wgpu's FeaturesWGPU, which has 58 flags (1166 lines debug, 820 release).
The implementation is here:
Lines 150 to 168 in 9253889
| fn all() { | |
| let mut truncated = <$T as $crate::Bits>::EMPTY; | |
| let mut i = 0; | |
| $( | |
| $crate::__bitflags_expr_safe_attrs!( | |
| $(#[$inner $($args)*])* | |
| {{ | |
| let flag = <$PublicBitFlags as $crate::Flags>::FLAGS[i].value().bits(); | |
| truncated = truncated | flag; | |
| i += 1; | |
| }} | |
| ); | |
| )* | |
| let _ = i; | |
| Self(truncated) | |
| } |
If I wrap the function in const {} then the function emits only 1 line of IR in debug, and seems to be fully optimized away in release.
This bloat slows down builds (sllightly) and probably makes it less likely for some optimizations to occur (don't quote me on that, I'm not a compiler developer).
There are probably some other generated functions that suffer from this. I can investigate and open a PR if that's wanted.
Metadata
Metadata
Assignees
Labels
No labels
Activity
bolshoytoster commentedon Jun 30, 2026
from_nameis also fairly big13 + 7ndebug,12 + 14nrelease (not a typo, it's way bigger in release). I can get it down to13 + 6nand13 + 12nrelease by changing$PublicBitFlags::$Flag.bits()to$value(only affected debug), and changing the list of if statements to a match (only affected release).KodrAus commentedon Jun 30, 2026
Thanks for investigating this @bolshoytoster! Would you be happy to open a PR with your improvements and we can take a look? We should be aiming for as small a footprint as possible.
bolshoytoster commentedon Jul 1, 2026
I've opened a PR (#492)