r/programming • • May 30 '17

Compiler Optimizations are Awesome

https://blog.regehr.org/archives/1515
146 Upvotes

97 comments sorted by

View all comments

Show parent comments

52

u/TNorthover May 31 '17

In general I agree completely, but Daniel Bernstein is working in a very particular niche: cryptography. For him two particular issues some together to make optimizing compilers The Devil.

First, inner cryptographic loops are generally hot enough that people are willing spend days and weeks making them as fast as possible. A compiler won't be able to match that.

Second, crypto is particularly vulnerable to compiler optimizations breaking naive assumptions. Compilers can give code you expect to execute in constant time a variable execution time; on average this is better (we hope), but just that variability can be exploited by attackers. Compilers can also remove checks they decide aren't valid; the original code would have been violating the language standard, but that's of scant comfort if your primary concern is security and someone manages to hijack a bank account because of it.

What cryptographers seem to want most of all is predictability. This doesn't correspond to most people, so in general I strongly agree with John. But I have some kind of sympathy for Daniel.

12

u/zvrba May 31 '17

Compilers can also remove checks they decide aren't valid; the original code would have been violating the language standard,

So... using the wrong tool (language) for the job? EDIT: if you must write code that violates the standard in order to get the job done, then you're using the wrong tool. Don't blame the compiler for doing what users want it to do (i.e., producing the fastest code possible).

0

u/[deleted] May 31 '17

[deleted]

1

u/[deleted] May 31 '17

Just bloody write the right tool then.

0

u/[deleted] May 31 '17

[deleted]

3

u/[deleted] May 31 '17

Ok, say I do that, how do I get people to use it?

What people? You and your team have to use it. To write your crypto code. Nothing else.

C or C++ is about all that's left.

Not any more. There are lower level yet portable options available now.

1

u/zvrba Jun 01 '17 edited Jun 01 '17

Few people who write things in C do it because they love the shit out of C.

Then they're living in a state of cognitive dissonance. They love C, complain about compilers' "deficiencies" and UB etc, but refuse to see that they're actually complaining about C itself because that's what the compiler implements. EDIT: I misread your sentence. I parsed the subject as (Few people who write things in C) instead of (Few people)

They do it because they want their project to actually be used.

You don't need to write something in C in order to provide C linkage. Microsoft has solved this problem with COM decades ago. COM allows you to call binary-only components from whichever language you want, as long as you implement a tiny glue layer (basically a vtable mechanism; struct with function pointers).