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

Enums not being stored as compactly as possible #34961

Closed
sourtin opened this issue Jul 21, 2016 · 3 comments
Closed

Enums not being stored as compactly as possible #34961

sourtin opened this issue Jul 21, 2016 · 3 comments
Labels
T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@sourtin
Copy link

sourtin commented Jul 21, 2016

I noticed that some enums seem to not be stored as compactly as they could be considering they should equate to unions. The two enums below, Foo and Bar, consist of two variants each. Each variant is <= 8 bytes, so the total size should fit within (8+tag) bytes, and they should take up the same space as each other. It seems however that the data is being stored adjacently rather than overlapping, as Foo takes up 16 bytes and Bar takes up 12 bytes. Is this a bug or is there an alternative way to obtain more compact data structures? (I suppose I could manually tag a struct and use transmute if necessary.)

enum Foo {
    Integer(i64),
    Rational(i32, u32)
}
println!("{:?}", std::mem::size_of::<Foo>());
// => 16

enum Bar {
    Integer(i32),
    Rational(i32, u32)
}
println!("{:?}", std::mem::size_of::<Bar>());
// => 12
@jonas-schievink
Copy link
Contributor

I believe the tag is stored first, followed by padding such that the i64 or i32 is aligned to 4 Bytes, which is why the tag seems to take up 8/4 Bytes.

@sourtin
Copy link
Author

sourtin commented Jul 21, 2016

Ah ok, that makes sense. Thanks!

@sourtin sourtin closed this as completed Jul 21, 2016
@jonas-schievink
Copy link
Contributor

In case you're interested, here's an issue about improving the layout optimizations the compiler does: rust-lang/rfcs#1230

@steveklabnik steveklabnik added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Mar 24, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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

3 participants