-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
use cases for alignment, packing and byte order #488
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
Certain (few) Win32 structs have non-standard alignment of its fields. Modelling binary formats is another use case. Bitfields are minefields ( http://www.catb.org/esr/structure-packing/ ). I remember rant by Linus Torvalds, how people really, really need think hard about the invisible padding in structures and try to optimize it away. I personally would prefer explicit padding, checked by the compiler to be correct, instead of the mess and guesswork.
|
I think explicit support for padding is very important. Currently, if you just use special fields for this, you're forced to name them when creating an instance of the struct. You also have to manually maintain some kind of number scheme when naming padding. Special syntax would solve a lot of real-world issues
Should I create a dedicated issue for this? For my usecase this is the only outstanding annoyance with Zigs current implementation |
Yes please, I have some thoughts which I will wait and comment on that issue when you create it. |
This issue should be addressed by #3133 |
@andrewrk is probably tired of this by now, but I think it might be worth at least recording some use cases that make other proposals for struct handling less complete.
One of the problems is that there are cases where you really want to control the exact representation of data in memory and others where you just want to use a higher-level concept.
Here is a short list (not at all complete) of some reasons why you would want to control some aspect of the in-memory representation:
Alignment above is actually two things. There is the alignment of the entity itself (where the struct or value starts) and there is the alignment of fields within the entity (in the case of a struct). Field alignment can be native (aligned to the natural alignment of the field itself), some minimum, and even a maximum. Some fields are aligned to a size that is not some nice multiple or fraction of the field. For instance the weird x87 long-ish double is 80 bits.
Padding can be somewhat included in alignment. You may want to pad a struct out to a certain alignment at all times. I do this kind of garbage in C:
`
struct rc {
int count;
lock_t lock;
void (*cleanup_func)(void *data);
};
`
I could not find another way to make sure that it would pad out this struct to be properly aligned. Probably not all of the dummy fields are necessary, but I found enough questionable things in my searches that I decided that it was better to do this hack with a belt and suspenders.
Suppose you have a struct with a lot of bool fields. You may put them in the struct in a lexical order that matches how you might use them. In many cases it would be nice to tell the compiler to pack them all into a few bytes at the beginning of the struct for size. Given the extreme difference in speed between a cache hit and miss, packing data tightly so that more fits in the cache can have a huge effect on some algorithms.
It would be nice to be able to control all these "knobs" on a data element. On all data, alignment of the targets of pointers would be useful to control. With structs, it would be good to be able to control:
I thought I would try to tie all these thoughts into one place.
The text was updated successfully, but these errors were encountered: