7 ms·
No it doesn't: just means we know what it will do. It might still fail. It might still use a risky approach that allows high optimization. One can run old Pasca
by nickpsecurity 11y ago
No it doesn't: just means we know what it will do. It might still fail. It might still use a risky approach that allows high optimization. One can run old Pascal or Modula-2 with unsafe constructs to do anything C can do at high performance.
Disallowing undefined behavior simply means the result is defined behavior. Might be safe, unsafe, any number of things. Just clearly that to developer and tools.
- pcwalton 11y agoThe point of the article is that precisely what you suggest will have extremely bad consequences for optimization. In your proposal, how do you propose to solve the load-load forwarding issue described in the article?
- pjmlp 11y agoBy taking the approach that in proper engineering quality should precede "anything goes" mentality.
- pcwalton 11y agoThat's very philosophical. It also fails to describe a technical solution to the problem. What is the specific solution to the specific load-load forwarding problem described in the article? This is a technical problem that needs a specific technical solution, not philosophy.
- pjmlp 11y agoI replied to it, indirectly. My position is the same as Tony Hoare, optimizations that exploit undefined behavior or contribute to possible malfunctioning programs are not to be done at all.
- pcwalton 11y agoThen you are making programs at least 2x slower, probably much more. That is untenable for many of us who are using C and C++. Customers will simply not accept e.g. browser engines that are 2x slower.
- 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".
- nickpsecurity 11y agoI don't know enough about C language and compiler technology to give a specific recommendation on that. However, your other comment on Java hints that its a hold-over from bad language design. Also, I recall that Fortran compilers could optimize the heck out of code that C compilers held back on specifically because Fortran was more deterministic. The compiler knew what the code was doing and could guess where usage was going. C's properties made the same code a giant unknown due to effects of pointers. My overall point is that you can do an optimization from a set of statements with specific meaning at least as easy as you can from one with unknown meaning. So, a similarly low-level language with well-defined behavior could be optimized at least as much as C. However, since compiler faces uncertainty, the argument doesn't work the other way around. That's why C and its undefined behavior are bad by design. And hence all the discussions on workarounds like the OP.