Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 701
Chen Li Avatar asked Jun 13 '26 14:06

Chen Li


1 Answers

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.

like image 155
Jean-Baptiste Yunès Avatar answered Jun 16 '26 08:06

Jean-Baptiste Yunès