This is the mail archive of the mailing list for the GCC project.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Easy 80x speed increase with template code

On 12/15/06, Mike Stump <> wrote:
The below patch makes:

$ cat
template <class C, int I>
struct _Integer {
  typedef typename _Integer<_Integer<C,I>,I-1>::TYPE TYPE;

template <class C>
struct _Integer<C,0> {
  typedef int TYPE;

template <int I, class C>
struct Integer_ {
  typedef typename Integer_<I-1,Integer_<I,C> >::TYPE TYPE;

template <class C>
struct Integer_<0,C> {
  typedef int TYPE;
int main() {
  typedef _Integer<int,1000>::TYPE TYPE;
  // typedef Integer_<1000,int>::TYPE TYPE;

compile around 86x faster for the n=1000 case, and arouund 22x faster
for the n=500 case, and marginally speeds up compilation when

500 case:
real    0m16.744s
real    0m0.812s

real    0m0.726s
real    0m0.738s

or around 22x faster...

1000 case:
real    0m3.157s
real    4m9.025s

real    0m3.094s
real    0m2.889s

or around 86x faster...

This patch I don't think is needed with Doug's work (right Doug?), but
I thought I would post it here, just in case Doug's work doesn't yet
get all the speed it should and see what others might think about it.
The testcase comes from Jochen Haerdtlein.

The basic idea is that one should not be forced to change the order of
their template parameters in order to get compilation speed, that's
just silly.  This helps avoid such silliness.

Should we consider this for mainline? 4.2?

I agree with the idea (at least the patch needs a comment before the two loops for the reason we have them), though I'm of course worried if the patch has a measurable slowdown for cases not involving any INTEGER_CST template parameters. I guess in practice it doesn't matter. I would also include scalar types in the fast loop, or maybe only leave out aggregate types - splitting the comparison into a cheap and a costly part.


2006-12-15 Mike Stump <>

* pt.c (comp_template_args): Do INTEGER_CST comparisons first.

Doing diffs in cp:
--- cp/pt.c.~1~ 2006-12-14 15:08:09.000000000 -0800
+++ cp/pt.c     2006-12-15 01:09:26.000000000 -0800
@@ -4255,7 +4255,18 @@ comp_template_args (tree oldargs, tree n
       tree nt = TREE_VEC_ELT (newargs, i);
       tree ot = TREE_VEC_ELT (oldargs, i);

-      if (! template_args_equal (ot, nt))
+      if (TREE_CODE (ot) == INTEGER_CST
+         && ! template_args_equal (ot, nt))
+       return 0;
+    }
+  for (i = 0; i < TREE_VEC_LENGTH (oldargs); ++i)
+    {
+      tree nt = TREE_VEC_ELT (newargs, i);
+      tree ot = TREE_VEC_ELT (oldargs, i);
+      if (TREE_CODE (ot) != INTEGER_CST
+         && ! template_args_equal (ot, nt))
        return 0;
   return 1;

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]