This is the mail archive of the gcc-patches@gcc.gnu.org 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]

Easy 80x speed increase with template code


The below patch makes:

$ cat t.cc
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:
before:
real    0m16.744s
real    0m0.812s

after:
real    0m0.726s
real    0m0.738s

or around 22x faster...

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

after:
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?


2006-12-15  Mike Stump  <mrs@apple.com>

        * 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]