how to minimize the size of struct?

218 views
Skip to first unread message

xie cui

unread,
Jul 13, 2022, 3:00:42 AM7/13/22
to golang-nuts
https://github.com/orijtech/structslop/blob/13637e228c1a50d8444da1b71a83aff3b6536851/structslop.go#L246-L267
why sort by <align of field,  size of field> can minimize the sizeof struct?
is there some formal prove? 

Brian Candler

unread,
Jul 13, 2022, 3:26:18 AM7/13/22
to golang-nuts
In every system that I'm familiar with, the alignments are all powers of 2. If you think of this as a kind of bin packing problem with different sized bins, then every large bin can always be split into 2 bins of half the size.

Therefore, if something fits into a small bin, you can't possibly get better packing by putting it in a larger bin. The alignment constraint means that it could never straddle the boundary between those two adjacent bins.

Steven Hartland

unread,
Jul 14, 2022, 9:37:53 AM7/14/22
to xie cui, golang-nuts

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/11d1b26a-b090-4462-a6c0-c04054ca1789n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages