4 ms·
The most compact representation is not always the most efficient. If two fields in the same struct variable are accessed by different goroutines, placing them
by adonovan 11y ago
The most compact representation is not always the most efficient. If two fields in the same struct variable are accessed by different goroutines, placing them far apart so that they reside in different cache lines may avoid a great deal of contention.
- masklinn 11y agoExcept technically struct memory layout is currently undefined in Go so you're relying on an implementation detail.
- adonovan 11y agoThis kind of optimized placement could in principle be done by a sufficiently smart compiler, but no such compiler exists. That's why existing compilers allow their users to control layout by, as you say, relying on an implementation detail.
- masklinn 11y agoNo, the language can specify source—order struct layout, either by default or opt-in.