Is Loop Hoisting still a valid manual optimization for C code?

后端 未结 8 1099
感动是毒
感动是毒 2021-01-01 15:08

Using the latest gcc compiler, do I still have to think about these types of manual loop optimizations, or will the compiler take care of them for me well enough?

相关标签:
8条回答
  • 2021-01-01 15:42

    If your profiler tells you there is a problem with a loop, and only then, a thing to watch out for is a memory reference in the loop which you know is invariant across the loop but the compiler does not. Here's a contrived example, bubbling an element out to the end of an array:

    for ( ; i < a->length - 1; i++)
        swap_elements(a, i, i+1);
    

    You may know that the call to swap_elements does not change the value of a->length, but if the definition of swap_elements is in another source file, it is quite likely that the compiler does not. Hence it can be worthwhile hoisting the computation of a->length out of the loop:

    int n = a->length;
    for ( ; i < n - 1; i++)
        swap_elements(a, i, i+1);
    

    On performance-critical inner loops, my students get measurable speedups with transformations like this one.

    Note that there's no need to hoist the computation of n-1; any optimizing compiler is perfectly capable of discovering loop-invariant computations among local variables. It's memory references and function calls that may be more difficult. And the code with n-1 is more manifestly correct.

    As others have noted, you have no business doing any of this until you've profiled and have discovered that the loop is a performance bottleneck that actually matters.

    0 讨论(0)
  • 2021-01-01 15:42

    A good rule of thumb is usually that the compiler performs the optimizations it is able to. Does the optimization require any knowledge about your code that isn't immediately obvious to the compiler? Then it is hard for the compiler to apply the optimization automatically, and you may want to do it yourself

    In most cases, lop hoisting is a fully automatic process requiring no high-level knowledge of the code -- just a lot of lifetime and dependency analysis, which is what the compiler excels at in the first place.

    It is possible to write code where the compiler is unable to determine whether something can be hoisted out safely though -- and in those cases, you may want to do it yourself, as it is a very efficient optimization.

    As an example, take the snippet posted by Steve Jessop:

    for (int i = 0; i < 10; ++i) {
        std::cout << "The number of primes less than 1 billion is: ";
        std::cout << countPrimesLessThan(1000*1000*1000);
        std::cout << std::endl;
    }
    

    Is it safe to hoist out the call to countPrimesLessThan? That depends on how and where the function is defined. What if it has side effects? It may make an important difference whether it is called once or ten times, as well as when it is called. If we don't know how the function is defined, we can't move it outside the loop. And the same is true if the compiler is to perform the optimization.

    Is the function definition visible to the compiler? And is the function short enough that we can trust the compiler to inline it, or at least analyze the function for side effects? If so, then yes, it will hoist it outside the loop.

    If the definition is not visible, or if the function is very big and complicated, then the compiler will probably assume that the function call can not be moved safely, and then it won't automatically hoist it out.

    0 讨论(0)
提交回复
热议问题