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

Optimize enum layout #20638

Closed
mahkoh opened this issue Jan 6, 2015 · 11 comments
Closed

Optimize enum layout #20638

mahkoh opened this issue Jan 6, 2015 · 11 comments
Labels
A-codegen Area: Code generation

Comments

@mahkoh
Copy link
Contributor

mahkoh commented Jan 6, 2015

fn main() {
    enum X { A, B(u8, uint) }
    enum Y { A, B(uint, u8) }
    assert_eq!(std::mem::size_of::<X>(), 16);
    assert_eq!(std::mem::size_of::<Y>(), 24);
}
@frewsxcv
Copy link
Member

frewsxcv commented Jan 7, 2015

Interestingly:

fn main() {
    enum X { A, B(u64, u32) }
    enum Y { A, B(u32, u64) }
    assert_eq!(std::mem::size_of::<X>(), 24);
    assert_eq!(std::mem::size_of::<Y>(), 16);
}

I don't know much about llvm, but I would maybe guess some sort of optimization is going on

@Diggsey
Copy link
Contributor

Diggsey commented Jan 7, 2015

@frewsxcv Those sizes are what one would expect without any kind of optimisation:
X => 1 byte discriminator + 7 bytes padding for field alignment + 8 bytes for u64 + 4 bytes for u32 + 4 bytes padding for struct alignment = 24 bytes total
Y => 1 byte discriminator + 3 bytes padding for field alignment + 4 bytes for u32 + 8 bytes for u64 = 16 bytes total

@frewsxcv
Copy link
Member

frewsxcv commented Jan 7, 2015

@Diggsey Awesome, thanks for the clarification 👍 . I'm not experienced in this lower level way of thinking

@frewsxcv
Copy link
Member

frewsxcv commented Jan 7, 2015

@Diggsey also, for X, 1 + 7 + 8 + 4 != 24

@frewsxcv
Copy link
Member

frewsxcv commented Jan 7, 2015

Nevermind, missed that last + 4

@Diggsey
Copy link
Contributor

Diggsey commented Jan 7, 2015

NP, you had me worried for a second there...

@huonw
Copy link
Member

huonw commented Jan 7, 2015

@mahkoh to be clear, are you suggesting that the compiler should reorder fields to try to make enums as small as possible?

@mahkoh
Copy link
Contributor Author

mahkoh commented Jan 7, 2015

Isn't that the point behind leaving the layout undefined?

@huonw
Copy link
Member

huonw commented Jan 7, 2015

I'll take that as a yes. Thanks for clarifying.

@mahkoh
Copy link
Contributor Author

mahkoh commented Jan 7, 2015

This particular enum can be optimized without reordering fields. Reordering is just one method to find an unused byte.

@kmcallister kmcallister added the A-codegen Area: Code generation label Jan 9, 2015
@kmcallister
Copy link
Contributor

Reordering is just one method to find an unused byte.

Some of which we should only do with #[repr(packed)].

@mahkoh mahkoh closed this as completed Apr 11, 2015
@kmcallister kmcallister reopened this Apr 11, 2015
@mahkoh mahkoh closed this as completed Apr 11, 2015
@rust-lang rust-lang locked and limited conversation to collaborators Apr 11, 2015
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
A-codegen Area: Code generation
Projects
None yet
Development

No branches or pull requests

5 participants