11 ms·
It’s important to understand where compiler optimization opportunities come from. They’re not magic; they’re determined by the information the compiler has avai
by derefr 7y ago
It’s important to understand where compiler optimization opportunities come from. They’re not magic; they’re determined by the information the compiler has available to it—information about constraints you’ve imposed on it, and information about guarantees it can make.
Using separate variables instead of reusing the same variable will sometimes actually increase the optimizations that compilers can do. Why? Because the version where you overwrite the variable is introducing additional constraints (e.g. keeping X1 and X2 in the very same stack slot called X) that have costs, but whose semantics you aren’t necessarily (or even usually!) relying upon.
For example: you know all that spare register spill space in the x64 calling convention that you’re allocating either way, and which deallocates as a block on function exit? Can’t make efficient use of that if you’re telling the compiler it has to keep both of your pointers successively in the same memory address!
On the other hand, the opposite is also true: sometimes overwriting a variable will free the compiler from a constraint it was under. This mostly comes about as the result of X1 and X2 both holding a shared or overlapping pointer, introducing aliasing (as often happens in the pure-functional data structures OP advocates for.) By overwriting X1 with X2 (or, equivalently, by freeing X1 as soon as you’re done with it!) you can allow the compiler to be more confident that nothing will access the shared pointer through X1.Y any more, only through X2.Y—which may allow e.g. smart pointer classes to elide a synchronization point.
(Note that this assumes a language with a model where a variable directly relates to memory, such that assigning to the same variable implies assigning to the same memory. This is true in most low-level languages, but not in functional languages; in e.g. Elixir, a second assignment to the same variable is actually creating a second variable with a mangled name, and leaving it to the compiler’s lifetime analysis to figure out that the first one is dead after that point.)
- lou1306 7y ago> Using separate variables instead of reusing the same variable will sometimes actually increase the optimizations that compilers can do LLVM optimizations, for instance, often need the IR to be in SSA form (static single Assignment).
- strbean 7y agoIf you're converting it to SSA form, is there really a difference between int a = 5 int b = 2 * a // don't use a again and > int a = 5 > a = 2 * a ?
- lou1306 7y agoI don't think I really understand the question, but please bear with me. Indeed both code snippets are the same computation. The first snippet is SSA, the second is not. Notice that doing, for instance, constant propagation on the first snippet is straightforward: int a = 5 int b = 2 * 5 You can do this because SSA form guarantees that the value of a is 5. In non-SSA code, there is no such guarantee: you need to check the "history" of each variable before replacing it with a value. I'm not totally convinced that SSA "increases the optimizations that compilers can do". However, it definitely makes optimization algorithms easier.
- strbean 7y agoI guess my point was that the optimizations available are the same (between those two snippets of code) if you end up in SSA form. Thinking about it now, I don't know what optimizations would be done before going to SSA and how they would be affected.
- jandrese 7y agoYeah, but in this context you have to hope the compiler realizes that you have self-imposed a static-only data model in a language that otherwise supports dynamic assignment.
- derefr 7y agoThat's what I'm saying—you don't have to "hope." You can look at what information you've provided to the compiler through your code, and deduce from that, what optimizations are possible to make on that code. From there, you just have to know whether the compiler you're using supports those optimizations. If it does, and your code should have them, but it doesn't end up having them—well, that's a bug in your compiler, and you should report it as such! If there were a clearly-more-efficient query plan possible to generate for a query of a particular data model in an RDBMS, and your RDBMS failed to come up with said query plan, you'd consider that a bug in your RDBMS, no? Well, same thing here.