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

后端 未结 8 1167
感动是毒
感动是毒 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.

提交回复
热议问题