Hacker News new | ask | show | jobs
by happythought 2139 days ago
Nice post, thanks. Did you happen to raise an issue with the Go team? Maybe it would be low hanging fruit for a compiler optimization.
1 comments

I might be biased because I'm currently working on this same optimization but for GCC. I think that it is more complicated than it seems. Defining the layout of a structure is normally done during parse time (at least in GCC). And one cannot know if it is safe to reorder fields all the way up until link time.

This means that a lot of optimizations and assumptions that were made might not longer be valid. For example, in GCC the results of the sizeof operator are evaluated at parse time. One might need to change these values link time. Otherwise statements like:

memset(&astruct, 0, sizeof(astruct));

will overwrite memory that is no longer part of the struct.

Furthermore, constant propagation might have propagated the value of a sizeof operator and it might have been folded into different arithmetic operations. This can be solved in different ways, but if the compiler was not built this way, it might take some time to implement. Also, all pointer arithmetic will need to be changed...