3 ms·
> if the compiler can automatically transform objects anyway, why do we even need value types/structs? It cannot always without affecting the semantics. An arr
by entha_saava 6y ago
> if the compiler can automatically transform objects anyway, why do we even need value types/structs?
It cannot always without affecting the semantics. An array of structs will still have to be an array of references in most cases, for example. The compiler isn't sufficiently smart to figure out such things beyond trivial examples.
In General it applies to any optimization: the compiler can figure out a subset of optimizations the programmer can. That's why not giving the control of memory layout to the programmer is objectively bad idea.
Also, when you need performance, not relying on compiler to figure out the optimizations is easier than ensuring the optimizations happen. Compilers are quite unpredictable.