Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Compiler Performance with Compile-Time Array?

Reply
Thread Tools

Compiler Performance with Compile-Time Array?

 
 
Andrew Tomazos
Guest
Posts: n/a
 
      10-27-2012
The following C++11 program (with N=1000000) causes GCC 4.7 to run out of memory and/or crash at compile time. For N=10000 it works fine.

It looks like it is using O(N^2) memory somehow, however there should only be O(NlogN) template instantiations and total template parameters. I am curious as to where the extra memory usage comes from.

The purpose of the program is to initialize a std::array at compile time with a constexpr function of each elements index. ie:

array x = { f(0), f(1), f(2), ..., f(N-1) }

such that the calculated data is stored in the application image static data (.rodata), in the same way as a C array is when given an initializer list.

===== CUT HERE =====

#include <iostream>
#include <array>
using namespace std;

constexpr int N = 1000000;
constexpr int f(int x) { return x*2; }

typedef array<int, N> A;

template<int... i> struct F { static constexpr A f() { return A{{ ::f(i)... }}; } };

template<class A, class B> struct C {};
template<int... i, int... j> struct C<F<i...>, F<j...>> : F<i..., (sizeof...(i)+j)...>
{
using T = F<i..., (sizeof...(i)+j)...>;
};

template<int n> struct S : C<typename S<n/2>::T, typename S<n-n/2>::T> {};
template<> struct S<1> : F<0> { using T = F<0>; };

constexpr auto X = S<N>::f();

int main()
{
cout << X[3] << endl;
}

===== CUT HERE =====

Any thoughts appreciated.

Regards,
Andrew.
--
Andrew Tomazos <http://www.tomazos.com>
 
Reply With Quote
 
 
 
 
Melzzzzz
Guest
Posts: n/a
 
      10-28-2012
On Sat, 27 Oct 2012 14:45:20 -0700 (PDT)
Andrew Tomazos <(E-Mail Removed)> wrote:

> The following C++11 program (with N=1000000) causes GCC 4.7 to run
> out of memory and/or crash at compile time. For N=10000 it works
> fine.


for N=100000 it goes out of memory too (I have 16gigs of ram and 5
swap

>
> It looks like it is using O(N^2) memory somehow, however there should
> only be O(NlogN) template instantiations and total template
> parameters. I am curious as to where the extra memory usage comes
> from.
>
> The purpose of the program is to initialize a std::array at compile
> time with a constexpr function of each elements index. ie:
>
> array x = { f(0), f(1), f(2), ..., f(N-1) }
>
> such that the calculated data is stored in the application image
> static data (.rodata), in the same way as a C array is when given an
> initializer list.
>
> ===== CUT HERE =====
>
> #include <iostream>
> #include <array>
> using namespace std;
>
> constexpr int N = 1000000;
> constexpr int f(int x) { return x*2; }
>
> typedef array<int, N> A;
>
> template<int... i> struct F { static constexpr A f() { return
> A{{ ::f(i)... }}; } };
>
> template<class A, class B> struct C {};
> template<int... i, int... j> struct C<F<i...>, F<j...>> : F<i...,
> (sizeof...(i)+j)...> {
> using T = F<i..., (sizeof...(i)+j)...>;
> };
>
> template<int n> struct S : C<typename S<n/2>::T, typename
> S<n-n/2>::T> {}; template<> struct S<1> : F<0> { using T = F<0>; };
>
> constexpr auto X = S<N>::f();
>
> int main()
> {
> cout << X[3] << endl;
> }
>
> ===== CUT HERE =====
>
> Any thoughts appreciated.


I don't know what this is suppose to do , but it expands beyond
16gb of RAM even if I put n/2 , n/2 and N = 100000
I have filling that this expands even more then n^2.

>
> Regards,
> Andrew.



 
Reply With Quote
 
 
 
 
Melzzzzz
Guest
Posts: n/a
 
      10-28-2012
On Sun, 28 Oct 2012 14:56:08 +0100
Melzzzzz <(E-Mail Removed)> wrote:

> On Sat, 27 Oct 2012 14:45:20 -0700 (PDT)
> Andrew Tomazos <(E-Mail Removed)> wrote:
>
> > The following C++11 program (with N=1000000) causes GCC 4.7 to run
> > out of memory and/or crash at compile time. For N=10000 it works
> > fine.

>
> for N=100000 it goes out of memory too (I have 16gigs of ram and 5
> swap
>
> >
> > It looks like it is using O(N^2) memory somehow, however there
> > should only be O(NlogN) template instantiations and total template
> > parameters. I am curious as to where the extra memory usage comes
> > from.
> >
> > The purpose of the program is to initialize a std::array at compile
> > time with a constexpr function of each elements index. ie:
> >
> > array x = { f(0), f(1), f(2), ..., f(N-1) }
> >
> > such that the calculated data is stored in the application image
> > static data (.rodata), in the same way as a C array is when given an
> > initializer list.
> >
> > ===== CUT HERE =====
> >
> > #include <iostream>
> > #include <array>
> > using namespace std;
> >
> > constexpr int N = 1000000;
> > constexpr int f(int x) { return x*2; }
> >
> > typedef array<int, N> A;
> >
> > template<int... i> struct F { static constexpr A f() { return
> > A{{ ::f(i)... }}; } };
> >
> > template<class A, class B> struct C {};
> > template<int... i, int... j> struct C<F<i...>, F<j...>> : F<i...,
> > (sizeof...(i)+j)...> {
> > using T = F<i..., (sizeof...(i)+j)...>;
> > };
> >
> > template<int n> struct S : C<typename S<n/2>::T, typename
> > S<n-n/2>::T> {}; template<> struct S<1> : F<0> { using T = F<0>; };
> >
> > constexpr auto X = S<N>::f();
> >
> > int main()
> > {
> > cout << X[3] << endl;
> > }
> >
> > ===== CUT HERE =====
> >
> > Any thoughts appreciated.

>
> I don't know what this is suppose to do , but it expands beyond
> 16gb of RAM even if I put n/2 , n/2 and N = 100000
> I have filling that this expands even more then n^2.


Seems that it is g++ thing, clang 3.1 compiles fine even
with N=1000000 (takes around 2 Gig)

>
> >
> > Regards,
> > Andrew.

>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Compiler Error Message: The compiler failed with error code -1073741819 Ram ASP .Net 0 09-13-2005 09:52 AM
Why is a JIT compiler faster than a byte-compiler RickMuller Python 4 03-26-2005 04:30 PM
Compiler compiler with C++ as output Andrey Batyuck C++ 3 05-17-2004 08:17 PM
Can we use <compiler> tag to avoid RunTime Compiler error? Jack Wright ASP .Net 5 01-19-2004 04:36 PM
Compiler Error Message: The compiler failed with error code 128. Yan ASP .Net 0 07-21-2003 10:49 PM



Advertisments