Padding is hard
dave.cheney.net
dave.cheney.net
I can see that that would prevent one from reliably dumping memory to disk and then re-reading it, but It's hardly reliable to do that anyway. One should be using an encoding anyway…
> It seems fine to me for the spec not to guarantee anything about struct field order in memory. The spec doesn't operate at that level.
And while he adds
> That said, no Go compiler should probably ever reorder struct fields.
the final result is struct memory layout is unspecified and implementation-dependent (essentially identical to repr(rust), as I understand it)
You generally don't want to be constraining how data structures work in a language so that they can be handed off to C code directly. (Unless perhaps you're writing a language that is nearly a C dialect or something.)
The same thing is frequently done in C. Wire format data gets padded and __packed as necessary, and then that's usually the program representation as well.
How much is that? As far as modern (2000 and onwards) languages Go, Golang and the compiler are pretty barebones. The compiler especially is as basic as it gets...
Which is fantastic. Nobody likes the bazillion options GCC has.
IMO, that is the right attitude, given that moving fields around may interfere with how parts of large structures map to cache lines, and that may have huge performance impacts, if some parts of a structure are rarely accessed.
Yes, the compiler could try to optimize that, too, but saying that is a hard problem is an understatement, and it needs statistics that the compiler doesn't have available. So, it definitely is unsuitable for go's goal of fast compilation.
No, struct layout is currently undefined[0] so you can't do that yourself. Any manual packing you defined today may or may not hold across platforms or future go revisions.
I think you'd get most of the benefit by writing a static analysis tool (a la "go vet") that suggests when reordering a struct's fields would reduce memory consumption.
_ struct{} // to prevent unkeyed literals
means, some explanation turns up here: https://groups.google.com/forum/#!topic/golang-nuts/NSjVW82i...
For modern x64/x86 the answer seems be to "no": http://lemire.me/blog/2012/05/31/data-alignment-for-speed-my...
Would it make sense for the Go spec to drop the alignment requirement? I'd think that a smaller memory footprint would be the better choice, and I'd think that interoperability would be better served by allowing arbitrary alignments.
It's not for everyone.