Li Chen
Li Chen

Reputation: 5270

loop unrolling and metaprogramming(TMP)?

While reading [Compile-time code optimization] from wikipedia' article-Template metaprogramming:

template <int length>
Vector<length>& Vector<length>::operator+=(const Vector<length>& rhs) 
{
    for (int i = 0; i < length; ++i)
        value[i] += rhs.value[i];
    return *this;
}

When the compiler instantiates the function template defined above, the following code may be produced:[citation needed]

template <>
Vector<2>& Vector<2>::operator+=(const Vector<2>& rhs) 
{
    value[0] += rhs.value[0];
    value[1] += rhs.value[1];
    return *this;
}

The compiler's optimizer should be able to unroll the for loop because the template parameter length is a constant at compile time.

However, take caution as this may cause code bloat as separate unrolled code will be generated for each 'N'(vector size) you instantiate with.

However, I learned while writing TMP codes, loop should be avoided because it is run-time and recursion with templates is a substition.

I have search edit on google and find this question which unrool manually. Answers also encourage to use recursion.

So, should I rely on Compile-time code optimization which is able to unroll the for loop using compile-time determinate length(end of the loop) at compile time or use recursion all the time?

I think the article from wikepedia encourage us to rely on un-roll. Maybe I misunderstand it?

Upvotes: 3

Views: 790

Answers (2)

Jean-Baptiste Yun&#232;s
Jean-Baptiste Yun&#232;s

Reputation: 36401

Loop unrolling has nothing to do with template (at least in general). Compilers can unroll loops when size is known (they may also be able to unroll not statically bounded loops but this is much more difficult).

If you use template recursion that only means that you can control the way loop unrolling occurs.

Whatever, don't try make premature optimization... It is unlikely that rolled loops is your runtime cost problem. Let the compiler unroll is at least free of charge, while doing it by yourself is a little bit painful and error-prone while not being sure that the result will worth the effort.

Upvotes: 4

marom
marom

Reputation: 5230

From my experience loop unrolling works up to a certain length. I guess that if code grows too large this has a negative impact on cache and resulting in a less than optimal performance

Upvotes: 0

Related Questions