-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Discriminants may not want to all be as small as possible #11551
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
Comments
Another idea that @Aatch came up with was to put the discriminant at the end of the struct so we wouldn't have to worry about alignment at all (and we keep the same size calculations that we have today). |
union {
struct { u8 tag; uint value; } Some;
struct { u8 tag; } None;
} And similarly for any other general-case enum. I mention this because the nice thing about having the discriminant at the beginning is that we don't have to do anything fancy in the per-variant LLVM types to have it at the right offset. If we just want to get rid of the |
I've seen LLVM do some nice optimizations in the past of If this is an involved solution, then it's not really worth it to pursue. Out of curiosity, is there a reason for |
If struct {
union {
uint value;
}
u8 tag;
} Wouldn't that get avoid the alignment issue without requiring special handling for each variant? Basically the LLVM type would change from |
@dotdash: You're missing that |
@thestinger: Ah right, completely lost track of arrays somewhere in the middle of my thoughts. |
cc #6736 for tbaa.struct. |
I've been led to believe that this isn't as easy a solution as I thought, and in some cases this may make structs larger. I'm going to close in favor of the correct solution, #6736 |
…t, r=xFrednet fixed fp caused by moving &mut reference inside of a closure changelog: [`needless_pass_by_ref mut`]: fixes false positive caused by not covering mutable references passed to a closure inside of a fuction fixes rust-lang#11545
If I have a struct
Option<uint>
, the way that we represent this today is viastruct { u8 tag; uint value; }
. This is ideal for discriminant size, and even for struct size. The bad part is that we add explicit padding (7 bytes on 64-bit machines) to make up for the middle padding. This leads to horrible things like unaligned moves in LLVM and alloca's of 7 bytes.I believe that it may actually be better for a discriminant to be the smallest size above or equal to the maximum alignment of the struct. So in the above case the discriminant would actually become
uint
, and forOption<u8>
it would remainu8
. This would remove any padding between the discriminant and the beginning of the data.cc @jld, I think this would remove some need for
tbaa.struct
for now, although we'll still want that eventually. I'm also not sure if reading a word is more efficient than reading a byte, but it kinda seems so to me?The text was updated successfully, but these errors were encountered: