4 ms·
Undefined behaviours are the accidental outcome when a program fails to be well formed. Nobody who writes compilers goes out there thinking that undefined behav
by ensiferum 6y ago
Undefined behaviours are the accidental outcome when a program fails to be well formed. Nobody who writes compilers goes out there thinking that undefined behavior is the key that let's them optimize code to run faster.
- est31 6y agohttps://en.wikipedia.org/wiki/Undefined_behavior#Benefits https://en.wikipedia.org/wiki/Undefined_behavior#Benefits > Documenting an operation as undefined behavior allows compilers to assume that this operation will never happen in a conforming program. This gives the compiler more information about the code and this information can lead to more optimization opportunities.
- ensiferum 6y agoYes exactly the requirement that program is "well formed" is what leads to optimization opportunities. Not its "ill formedness". Nobody writes optimizations thinking "this program is ill formed I can enable this and that optimization" but exactly the reverse. "This program is well formed so I can enable some optimization."
- est31 6y agoI think we are talking past each other. When I said "undefined behaviour" above, I meant the concept of it. The fact that the language spec has undefined behaviour is what enables the compiler to make assumptions. The language spec could for example say that out of bounds indexing in an array is defined behaviour, and defined to raise a trap. But implementing that would slow down a lot of code. You refer to the ill formed code itself that violates the language specification. That's not what I meant. Yes, the fact that this code becomes garbage is a side effect of the optimizer's assumption that code is always well formed, instead of some arcane idea of punishing people who don't follow an arbitrary specification. I think we both agree.