You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
If a struct field would be placed at an offset from the beginning of the struct not divisible by its align, then padding will be inserted between it and the previous field. Because alignments will be to a number of bytes that is a power of 2, a field with an alignment <= the alignment of the previous field will not require padding, since the larger field's alignment is a multiple of the smaller field's alignment. Therefore, sorting by descending alignment should guarantee that there will be no padding between the struct's fields.
There could still be padding at the end of the struct (for example if the last field in a struct has a size smaller than the overall struct's alignment). Interestingly, this means there might still be padding between struct fields if the fields themselves have padding included at the end. Thus, it could be possible to get further savings (at the cost of readability and probably other things) by flattening a struct. For instance: https://go.dev/play/p/ALZhIBGTKbQ
I'm less certain whether having the secondary sorting key be the size of the field, but at the very least, it makes it clear which fields in a struct take up the most space :)
structslop/structslop.go
Lines 246 to 267 in 13637e2
why sort by <align of field, size of field> can minimize the sizeof struct?
is there some formal prove?
The text was updated successfully, but these errors were encountered: