3 ms·
> Then you are making programs at least 2x slower, probably much more. Then how come programs written in languages which don't have undefined behaviour, like J
by twic 11y ago
> Then you are making programs at least 2x slower, probably much more.
Then how come programs written in languages which don't have undefined behaviour, like Java, are not 2x slower than those written in C?
- jerf 11y agoBecause they aren't C. I think it's not just that "undefined behavior can be optimized" in some abstract sense but that in particular C has the problem that its undefined behavior is where a lot of optimizations live. This is due to a combination of the language being very old now, and the hardware world having moved on a lot since it was (half-)specified. I really don't consider this a criticism of C per se or its designers. It is unreasonable to expect that a language specified in the 1970s would be tuned for either modern processors or have the benefit of an additional 40 years of collective experience. But it is, nevertheless, true. (It's part of why my position is very much that this entire approach just won't work. C is pretty much what it is; it can not be changed. It is literally easier to use another language than to try to specify a "friendly C" in 2016. And I don't say that because I don't know how large a task it is to start writing code in another language; I say that because people are really underestimating how much work Friendly C will take. I suggest that everyone observe that all who have now seriously tried to sketch what this would look like have immediately given up and totally surrendered. This means something.)
- pjmlp 11y agoThere is another reason for C's undefined behavior. Back when the language was being standardize, there was way more hardware diversity than there is now and C implementations outside UNIX, even K&R ones, had lots of semantic differences. As no compiler vendor involved in the first ANSI C process wanted to give up on their semantics, all of them were swept into "undefined behavior".
- jerf 11y agoKinda part of what I meant by "the hardware world having moved on a lot". I mean, I can hardly think of a way in which it is the same world, except perhaps a vague von-Neumann-esque architecture still being used, but even that's a lot different than it used to, thanks to the way that nowadays even assembler code is really just a specification to the CPU, rather than an execution plan, since the CPU recompiles it to internal microcode and just does whatever it likes instead on the fly. Almost nothing is the same as it was back then.
- StefanKarpinski 11y agoJava programs are quite often 2x or more slower than they could be if written in C. See for example some simple benchmarks at http://julialang.org/ http://julialang.org/ – Java ranges from 1x to 3.92x as slow as C. The billions of dollars that have been poured into JVM optimizations have closed the gap a lot, but by no means all the way.
- pjmlp 11y agoMy problem with these benchmarks is always which JVM are they measuring, given that I can choose any of these ones: https://en.wikipedia.org/wiki/List_of_Java_virtual_machines https://en.wikipedia.org/wiki/List_of_Java_virtual_machines It is like I would measure C performance by picking a specific compiler, even though I can choose any of these ones: https://en.wikipedia.org/wiki/List_of_compilers#C_compilers https://en.wikipedia.org/wiki/List_of_compilers#C_compilers
- nickpsecurity 11y agoDamn, you got me: I was cheating by using Azul's Zing on Vega 3 processors. http://www.azulsystems.com/products/vega/processor http://www.azulsystems.com/products/vega/processor Well, would be if I was a Java salesman. ;)
- nickpsecurity 11y agoStill doesn't call out parent's point because Java is a bad example for both of you. It's a behemoth compared to safer, systems languages with a history of performance problems. Original was something like 10-15x slower than C. A proper comparison would be to something like Component Pascal, Modula-2/3, Ada, Fortran for numeric algorithms, and so on. Their results were high performance with higher correctness and predictable behavior. So, parent's point stands and so does mine that only C's bad design and culture lead to relying on undefined behavior for performance. They could just as easily optimize from well-defined semantics into assembly like the other languages mostly did.
- pcwalton 11y agoThis 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.