3 ms·
This is a great question, and I was hoping someone would ask it. :) The answer varies based on which specific optimization you're talking about, but for the op
by pcwalton 11y ago
This is a great question, and I was hoping someone would ask it. :)
The answer varies based on which specific optimization you're talking about, but for the optimization described in the article the answer is simply that Java is type safe. Notice that the load-load forwarding optimization relies on int pointers and float pointers being guaranteed not to alias. Strict aliasing is one way to guarantee this. But another way is simply being type safe! Type safety means that int pointers and float pointers (in Java, int[] and float[]) can't alias by construction--otherwise the types would be wrong. So a Java compiler can just do this optimization without worrying about it. And it's free of undefined behavior.
I like this example because it shows how the "close to the metal" nature of C and C++ can actually hinder optimization opportunities, as counterintuitive as that may initially sound.
- pjmlp 11y agoAs I added as edit on my comment, I think that the place for such optimizations is pure hand written Assembly, if they cannot be safely generated. You avoid relying on a compiler specific behavior, that will break the moment you switch compiler, compiler version or CPU target. Instead you get a very clear set of Assembly instructions, that will never change for the specific CPU family. The argument of writing Assembly being non-portable, doesn't play a role, because such optimizations are not even guaranteed to stay stable for the same compiler vendor. In any case, only after a profiler has proven they are really a case for spending development effort on. My experience with the C and C++ developer culture back to the mid-90's, is that many suffer by anticipation with needless premature micro-optimizations.
- pcwalton 11y ago> As I added as edit on my comment, I think that the place for such optimizations is pure hand written Assembly, if they cannot be safely generated. Not feasible. Quoting DannyBee yet again: "Most applications have flat profiles. They have flat profiles because people have spent a lot of time optimizing them." When your application has a flat profile, you're spending most of your time in most of your code. So by saying that only code written in assembler should be optimized, you're asking most large C/C++ users to rewrite their codebases in assembler or suffer an unacceptable performance hit. Not going to happen.
- pjmlp 11y ago> you're asking most large C/C++ users to rewrite their codebases in assembler or suffer an unacceptable performance hit. Not going to happen. I am aware of it. They rather rely on something that isn't guaranteed to even survive a minor upgrade of the same compiler, and always act surprised when they discover what compiler does and the standard says don't go always hand in hand. So no reason to complain when those long weekends come in to track down optimizer induced bugs.