by mytailorisrich · 4y ago · 0 comments
Language specs rarely support it well, so much of the blame is on language designers. But it's a somewhat chicken and egg situation. Language users don't demand it either because they never had it in other languages.
There is more to it than lazy language designers.
Neither is an issue in Go, since everything’s statically compiled and it doesn’t make any ABI guarantee.
Making this opt-out (or making precise layout opt-in) actually improves the situation there, because then you have clear, explicit guarantees.
I submit it's mostly solvable in language design. They're not all lazy of course, but claiming to be very performance-oriented is a half truth as long as you don't have a strong story here.
Smaller size is good for cache, but other factors matter
From the above
Single-Threaded Environment
When a single thread on a single core is accessing the data in a struct, we can improve caching performance by using as little cache lines as possible.
By optimizing for memory footprint, as discussed in the previous section, the struct uses less memory and hence occupies less space in the cache.
By placing heavily used members close together, we hope (based on the locality of reference principle) that they will end up closer together in the cache, preferably even on the same cache line, and hence use less cache space.
By separating hot fields form cold ones, we reduce the amount of cache lines filled with unused data.
So for HPC code, you very much do want the ability to re-order them manually.
C, C++, Pascal all define the order explicitly, the platform ABI generally defines the padding rules.
If you include toy languages, you might get there.
Even languages like Go and Rust recognize that at API boundaries you need a stable and defined ABI.