Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Missed layout optimisation with multiple niches #119507

Open
GnomedDev opened this issue Jan 2, 2024 · 1 comment
Open

Missed layout optimisation with multiple niches #119507

GnomedDev opened this issue Jan 2, 2024 · 1 comment
Labels
A-layout Area: Memory layout of types C-optimization Category: An issue highlighting optimization opportunities or PRs implementing such T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@GnomedDev
Copy link
Contributor

I tried this code:

use std::num::NonZeroU8;

struct Test {
    niche_one: NonZeroU8,
    niche_two: NonZeroU8,
}

fn main() {
    assert_eq!(
        std::mem::size_of::<Test>(),
        std::mem::size_of::<Option<Option<Test>>>()
    );
}

I expected this program to exit fine, and fit the tag of the second option in the available niche.
Instead, the program panics, as the second option wastes a byte of memory making it's own tag byte.

Meta

rustc --version --verbose:

rustc 1.77.0-nightly (e51e98dde 2023-12-31)
binary: rustc
commit-hash: e51e98dde6a60637b6a71b8105245b629ac3fe77
commit-date: 2023-12-31
host: x86_64-unknown-linux-gnu
release: 1.77.0-nightly
LLVM version: 17.0.6
@GnomedDev GnomedDev added the C-bug Category: This is a bug. label Jan 2, 2024
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Jan 2, 2024
GnomedDev added a commit to GnomedDev/small-fixed-array that referenced this issue Jan 2, 2024
This was inhibiting niche optimisations due to rust-lang/rust#119507
@saethlin
Copy link
Member

saethlin commented Jan 2, 2024

I'm pretty sure we already have an issue for this, but I can't find one that's written clearly. #101567 was the first one that clearly looks like a duplicate when I was scrolling.

The problem is that the rustc layout representation (in this case of Test) only represents a single contiguous niche with a single contiuous (albeit wrapping) range of niche values. That should be (probably is somewhere) tracked in an issue.

@Noratrieb Noratrieb added T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. A-layout Area: Memory layout of types C-optimization Category: An issue highlighting optimization opportunities or PRs implementing such and removed C-bug Category: This is a bug. needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Jan 2, 2024
GnomedDev added a commit to GnomedDev/small-fixed-array that referenced this issue Jan 14, 2024
This is not currently usable, due to rust-lang/rust#119507, but may become useful in future compiler versions
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-layout Area: Memory layout of types C-optimization Category: An issue highlighting optimization opportunities or PRs implementing such T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants