[LLVMbugs] [Bug 21628] Increase limit of number of parameters in variadic template packs?

bugzilla-daemon at llvm.org bugzilla-daemon at llvm.org
Fri Nov 21 15:20:01 PST 2014


http://llvm.org/bugs/show_bug.cgi?id=21628

Richard Smith <richard-llvm at metafoo.co.uk> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |richard-llvm at metafoo.co.uk
         Resolution|---                         |INVALID

--- Comment #3 from Richard Smith <richard-llvm at metafoo.co.uk> ---
Your code is slow because your algorithm is bad:

template <class TUndInt, class TV, class ...TVs>
constexpr TUndInt CalcMax(TV V, TVs... Vs) noexcept {
    return static_cast<TUndInt>(V) > CalcMax<TUndInt>(Vs...) ?
static_cast<TUndInt>(V) : CalcMax<TUndInt>(Vs...);
}

For a list of N elements, this calls CalcMax on a list of N-1 elements twice.
Thus your function's runtime is O(2^N). When your list has 25 elements, this
hits Clang's limit for complexity of constexpr evaluation.

g++ happens to memoize constexpr evaluations, which hides performance problems
in code like this, but Clang does not.

If you switch to a better algorithm, your problem will go away. For instance:

template <class TUndInt, class TV1, class TV2, class ...TVs>
constexpr TUndInt CalcMax(TV1 V1, TV2 V2, TVs... Vs) noexcept {
    return static_cast<TUndInt>(V1) > static_cast<TUndInt>(V2)
               ? CalcMax<TUndInt>(V1, Vs...)
               : CalcMax<TUndInt>(V2, Vs...);
}

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20141121/8516927d/attachment.html>


More information about the llvm-bugs mailing list