|Home | About | Partners | Contact Us|
XLR: Extensible Language and Runtime
An interesting effect of semantic noise is that it often precludes useful optimizations, because the compiler has no precise idea of what is happening.
The compiler has in general very little information to determine what use is being made of a specific pointer. For safety, it needs to be very pessimistic and assume the worst. Consider the following simple piece of code:
int width, height; GetWindowExtent (window, &width, &height);
By reading the code, we all know that this reads the window size into width and height. But the compiler has to assume that GetWindowExtent does something nasty, like saving a copy of the pointers away in a global variable, just so that another evildoer function can modify them. Now, let's say that you follow the above code with a loop, like:
for (x = 0; x < width; x++) for (y = 0; y < height; y++) color[x][y] = ComputeColor(x, y, width, height);
You look at the assembly code, and even at highest optmiization levels, you see that the compiler insists on loading width and height from memory, rather than storing them in a register. Why? Simply because the compiler now assumes that the ComputeColor function is the evildoer we mentioned above. The address of width and height has been exposed by the call to GetWindowExtent, even if that was just a side effect and not the intent of that call.
Accesses to memory are very slow relative to register accesses in modern computers. So this is a significant mis-optimization. But it happens all the time in C.
The root cause of this is the semantic noise around pointers. In that particular case, the actual concept that should be used for getting width and height is the notion of output parameter. Many other forms of semantic noise can contribute to similar lost optimizations. Here are a few examples, where understanding why they impact performance negatively is left as an exercise to the reader (following the links should help, e-mail me if you need more help):
By eliminating as much semantic noise as possible, XL as a language allows the compiler to have a very precise understanding of what is happening. Language extensions can even be used to implement domain-specific optimizations.