Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.Sign up
ability to set alignment on struct fields #1512
Just like global variables and local variables support alignment, struct fields should support alignment.
The alignment of a struct should be the alignment of its most aligned member. As Zig iterates over the fields of a struct, it knows the current alignment at that position. When a field requires more alignment, padding bytes are inserted until the desired alignment is achieved. Zig currently does this incorrectly - See #1248
Related is reordering fields for better performance and memory usage (see #168)
You should also be able to set the alignment of fields in packed structs. This does 2 things:
A packed struct in an array should give a compile error if its size is not byte-aligned, or if its size is not aligned to its own alignment.
referenced this issue
Sep 13, 2018
Here we can see a use case for specifying the alignment of struct fields: https://github.com/facebook/folly/blob/master/folly/ProducerConsumerQueue.h (search for 'alignas').
They specify the alignment to be the minimum offset between two objects to avoid false sharing.