diff mbox series

C++ PATCHes for improved template memory use

Message ID CADzB+2kcKPQdG+7gEDp36CTUcZQpmrRk6r8SnGbyHpBWfMmaZQ@mail.gmail.com
State New
Headers show
Series C++ PATCHes for improved template memory use | expand

Commit Message

Jason Merrill June 21, 2018, 6:16 p.m. UTC
I've been looking at -fmem-report for a testcase from someone on the
committee, and found a couple of low-hanging fruits.

The first patch doesn't change any allocation, just makes it so
-fmem-report can see who's calling cxx_make_type.

The second patch avoids creating a vector (which we then never freed)
every time we push_to_top_level for doing template instantiation.
This alone improved peak memory use by 30%.

The third patch avoids redundantly creating level-lowered versions of
TEMPLATE_TYPE_PARM, as we had already been doing for non-type
parameters.  This improved peak memory use by another 20%.

Unfortunately, these don't affect the 80290 testcase significantly.

Tested x86_64-pc-linux-gnu, applying to trunk.
diff mbox series

Patch

commit 93ca12c981968b5d545f1428d7b6d47557f207f7
Author: Jason Merrill <jason@redhat.com>
Date:   Wed Jun 20 12:36:29 2018 -0400

         * pt.c (tsubst) [TEMPLATE_TYPE_PARM]: Use TEMPLATE_PARM_DESCENDANTS.

diff --git a/gcc/cp/pt.c b/gcc/cp/pt.c
index c5433dc46ae..69e9479302e 100644
--- a/gcc/cp/pt.c
+++ b/gcc/cp/pt.c
@@ -14472,6 +14472,15 @@  tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
 		     && !PLACEHOLDER_TYPE_CONSTRAINTS (r))
 	      /* Break infinite recursion when substituting the constraints
 		 of a constrained placeholder.  */;
+	    else if (TREE_CODE (t) == TEMPLATE_TYPE_PARM
+		     && !PLACEHOLDER_TYPE_CONSTRAINTS (t)
+		     && !CLASS_PLACEHOLDER_TEMPLATE (t)
+		     && (arg = TEMPLATE_TYPE_PARM_INDEX (t),
+			 r = TEMPLATE_PARM_DESCENDANTS (arg))
+		     && (TEMPLATE_PARM_LEVEL (r)
+			 == TEMPLATE_PARM_LEVEL (arg) - levels))
+		/* Cache the simple case of lowering a type parameter.  */
+	      r = TREE_TYPE (r);
 	    else
 	      {
 		r = copy_type (t);