Skip to content

Missed niche optimization #119055

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

Open
Jules-Bertholet opened this issue Dec 17, 2023 · 1 comment
Open

Missed niche optimization #119055

Jules-Bertholet opened this issue Dec 17, 2023 · 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

@Jules-Bertholet
Copy link
Contributor

I tried this code:

#[repr(u8)]
enum Foo {
    A = 0,
}

#[repr(u8)]
enum Bar {
    A = 1,
}

enum Choice {
    F(Foo),
    B(Bar),
}

fn main() {
    dbg!(core::mem::size_of::<Choice>());
}

I expected to see this happen: Choice has size 1

Instead, this happened: Choice has size 2

Meta

Rust version; 1.74.1

@rustbot label A-layout T-compiler

@Jules-Bertholet Jules-Bertholet added the C-bug Category: This is a bug. label Dec 17, 2023
@rustbot rustbot added needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. A-layout Area: Memory layout of types T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Dec 17, 2023
@saethlin saethlin added 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 Dec 18, 2023
@programmerjake
Copy link
Member

this optimization should apply when variants have data too:

// could be size 2 since A and B's discriminants are in the same spot with non-overlapping contiguous values, but currently isn't
enum Top {
    A(A),
    B(B),
}

#[repr(u8)]
enum A {
    A0(u8) = 0,
    A1(u8) = 1,
}

#[repr(u8)]
enum B {
    B2(u8) = 2,
    B3(u8) = 3,
}

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