6 ms·
I can see both sides of this. But looking at this argument: > There are two ways to evaluate the the C specification's rightness and properness. [...] The se
by haberman 11y ago
I can see both sides of this. But looking at this argument:
> There are two ways to evaluate the the C specification's rightness and properness. [...] The second is to ask what is most useful. And there again the C committee have clearly failed.
Just two days on HN we saw this article: https://news.ycombinator.com/item?id=11468603 https://news.ycombinator.com/item?id=11468603 In which it says:
> no matter the kind of software, performance is almost always worse than our customers would like it to be.
That is why all of this is happening. There is a market demand for performance. Compilers that increasingly exploit UB in C is just a manifestation of that market demand.
- michaelt 11y agoThere is a market demand for performance. You could fulfil that demand without breaking existing code by making the riskier optimisations configurable and turned off by default.
- webkike 11y agoThey are usually. Most people don't change the -03 in their build files after a compiler update though.
- ktRolster 11y ago-O4 exists for 'risky' optimizations. -O3 is for optimizations that are mostly not risky. -O2 is for optimizations that don't involve a speed/size tradeoff -O1 is for optimizations that don't slow your compiler down too much Obviously these are fuzzy categories, but -O4 is where "undefined behavior" optimizations belong. Alternately, each individual optimization has its own flag, and can be turned on individually.
- haberman 11y agoI've never heard of -O4 for "risky" optimizations, and I can't find any reference to this. Do you have one?
- ktRolster 11y agoIf you search for "gcc O4" you'll find some references (it's tricky because if you search for -O4 Google will search for everything without O4). Currently, there are no optimizations that fit in that category, but as recently as 2003 I remember seeing it in the man pages. I believe there was -O5 and -O6 too, but I don't trust my memory enough to be certain.
- haberman 11y agoThe results for that query all seem to say that "gcc -O4" doesn't exist. There is one proposal from Chris Lattner for adding this to Clang, but it's not clear that this ever happened.
- ktRolster 11y agoCurrently gcc uses -Ofast for 'dangerous' optimizations. Maybe I remembered the history wrong. There are definitely cases of people using -O4 http://www.larcenists.org/Twobit/KVW/kvwbenchmarks.html http://www.larcenists.org/Twobit/KVW/kvwbenchmarks.html . There are definitely optimization options which are (or have been) available that are not turned on by -O3. For example: -ffast-math -funroll-all-loops -fstrict-aliasing Are not turned on (for good reasons: unrolling loops often slows a program down). So there's no reason UB optimizations need to be turned on by default, or with -O3. You could throw them in -Ofast or use -Oub, or use optimization specific flags (they should have optimization specific flags in any case) and that was my main point.
- haberman 11y ago-Ofast turns on optimizations that can break standard-conforming programs! That is a very different thing that turning on optimizations that might break non-standard-conforming programs! I think the idea of making it controlled by optimization flags is interesting, but you would need to formalize what the more "safe" variant of the language is if people are actually going to depend on it.
- mwcampbell 11y agoBut why do we always want more, more, more? If gcc -O2 without whole-program optimization was good enough to encode MP3 faster than real-time on a 366 MHz Pentium laptop in 1999, then surely it's good enough now, with all the increases in processor performance since then. If that level of compiler optimization isn't good enough now, then maybe that means we programmers are doing something wrong. At some point, better optimization in a compiler probably isn't worth what you give up to get it, in terms of reliability and comprehensibility. Edit: All the responses are right. Feel free to downvote this ill-considered comment into oblivion, now that I can't take it back.
- chrisseaton 11y ago> If gcc -O2 without whole-program optimization was good enough to encode MP3 faster than real-time on a 366 MHz Pentium laptop in 1999, then surely it's good enough now This is patently a ludicrous argument. How can the speed of encoding an MP3 be used as a proxy for countless other applications of computers? Programs that are slower than they need to be waste real energy and other industrial resources.
- haberman 11y agoPeople in 1999 weren't running huge neural nets and beating the world champion in Go. There are millions of computers in data centers around the world. They are working on problems that someone thought it was worthwhile enough to pay for. If C compilers emit code that is slower, that means buying and maintaining a lot more computers, using more energy, producing more greenhouse gas, etc.
- dragontamer 11y ago> People in 1999 weren't running huge neural nets and beating the world champion in Go. Which was mostly GPU code, so it was either CUDA or OpenCL btw. Not actually C. People do need to realize that the fastest, massively parallel supercomputers are in fact written in CUDA / OpenCL. And the most powerful supercomputers are mostly being used to display 3d-models of a sword-wielder fighting a guy with guns.
- jerf 11y agoYou don't really understand the UB problem if you model this in your head as some sort of simple set of rules that everybody really should have known all along, and it's not a big deal to fix the UB in your code. You have to make sure you're modeling it as A: a set of rules so arcane and byzantine that virtually nobody understands them, and to a first approximation, no non-trivial well-defined C program has ever been written and B: that this was a de facto change in C's nature... in a very real way, the language changed out from underneath people, without them expecting it. You can't just shrug this away with "Well, if you want performance...", because people in fact don't want abstract "performance"... they want the language they were truly writing in to perform well, not for what is de facto a different dialect of the language to suddenly appear and replace the language they were using.
- haberman 11y ago> You don't really understand the UB problem if you model this in your head as some sort of simple set of rules that everybody really should have known all along What does this have to do with my comment, at all? > You can't just shrug this away with Who is shrugging? You don't understand a complex problem until you understand both sides. The article only presents one side. I am presenting the other. It doesn't mean I think the problem is simple or easy.
- ktRolster 11y agobecause people in fact don't want abstract "performance" That's true, if performance were the only consideration, we would all be writing assembly.
- physguy1123 11y agoTo add to this, a lot of what compilers decide is undefined isn't bizarre corner case code that rarely happens, but common and useful techniques that the compiler will happily demolish without warning. The 'defined' workarounds, however, are usually difficult, less safe, harder to understand, and often slower. This is without getting into the nightmare that is LTO finding 'undefined' behavior across program boundaries.
- 11y ago
- ArkyBeagle 11y agoYou have to work pretty hard to get code to a state such that compiler optimizations will save you. I've had this same argument with cache management specialists. If you have to consider how the cache works for your product to work, you're doing it wrong.
- GFK_of_xmaspast 11y ago> You have to work pretty hard to get code to a state such that compiler optimizations will save you At least in c++, this is absolutely 100% not the case, there's a huge difference between -O0 and -O2.
- ArkyBeagle 11y agoI need to check that out for myself.
- dumael 11y agoI've dealt with bugs that existed at -O0, -O2 was fine. But the bug was in the compiler itself.
- ArkyBeagle 11y agoAbsolutely.