Enums don't utilize niches across multiple data-carrying variants #121333
Labels
A-layout
Area: Memory layout of types
C-bug
Category: This is a bug.
C-optimization
Category: An issue highlighting optimization opportunities or PRs implementing such
S-has-mcve
Status: A Minimal Complete and Verifiable Example has been found for this issue
T-compiler
Relevant to the compiler team, which will review and decide on the PR/issue.
Tested on latest stable and nightly.
I wanted to build an enum X such that
is 24 bytes large.
I tried various things to exploit Vec's niches (0/null on ptr, > isize::MAX on capacity), including using explicit-zero padding.
Unfortunately for this very specific case it seems Vec lacks alignment niches on its pointer field to make this work, and/or I can't make a niche that's big enough for the compiler to prove the two variants are fully "disjoint" in their valid bit patterns.
However, even testing with much simpler types with much simpler niches I could not get the compiler to produce an optimal enum layout:
X is 16 bytes here, even though
AlwaysZero
andNonZeroU64
's niches are exact opposites of each other, so the entire enum should just be 8 bytes. It would be nice if rustc could support at least simple two-variant cases like this. I can see it coming in handy for things likeResult
and possiblyCow
.The text was updated successfully, but these errors were encountered: