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

Suboptimal niche usage within the fields of a variant. #62691

Closed
eddyb opened this issue Jul 15, 2019 · 1 comment · Fixed by #70411
Closed

Suboptimal niche usage within the fields of a variant. #62691

eddyb opened this issue Jul 15, 2019 · 1 comment · Fixed by #70411
Labels
C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@eddyb
Copy link
Member

eddyb commented Jul 15, 2019

On x86_64, this prints the values in comments:

pub enum Option2<A, B> {
    Some(A, B),
    None
}

fn main() {
    use std::mem::size_of;
    
    dbg!(size_of::<Option<Option<(bool, &())>>>()); // = 16
    dbg!(size_of::<Option<Option<(&(), bool)>>>()); // = 16
    dbg!(size_of::<Option<Option2<bool, &()>>>()); // = 16
    dbg!(size_of::<Option<Option2<&(), bool>>>()); // = 24
}

What's happening here is that when we pick the niche to represent None of Option<T>, we pick the largest (or "roomiest") of the niches in T, so that further enums around Option<T> have more optimization opportunities themselves.

(Why just one niche? That has to do with the fact that in an enum, the only part of the representation guaranteed to be always initialized and valid is the tag / niche, but no other variant fields)

For Option2<A, B>, however, we just pick the best niche in A and only look at B if A has no niches at all, so if B has a better niche, we miss it.

This is a small oversight in the code itself, and arguably it will be slightly nicer once this is fixed, but I wanted to open this issue first, before I have a chance to fix it myself (if someone else doesn't get there first).

cc @Gankra @nox

@nox
Copy link
Contributor

nox commented Jul 15, 2019

I was pretty sure that my patch to find the roomiest niche covered that case, but apparently it didn't. :(

@jonas-schievink jonas-schievink added C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jul 15, 2019
Centril added a commit to Centril/rust that referenced this issue Mar 26, 2020
Fix for rust-lang#62691: use the largest niche across all fields

fixes rust-lang#62691

(The second commit is a small optimization but it makes the code less pretty and i don't know if it is worth it.)
bors added a commit to rust-lang-ci/rust that referenced this issue Mar 26, 2020
Rollup of 5 pull requests

Successful merges:

 - rust-lang#68004 (permit negative impls for non-auto traits)
 - rust-lang#70385 (Miri nits: comment and var name improvement)
 - rust-lang#70411 (Fix for rust-lang#62691: use the largest niche across all fields)
 - rust-lang#70417 (parser: recover on `...` as a pattern, suggesting `..`)
 - rust-lang#70424 (simplify match stmt)

Failed merges:

r? @ghost
@bors bors closed this as completed in 4d77d01 Mar 26, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants