This reduces peak memory usage by 20% for a specific testcase.

Bootstrapped and tested on x86_64-unknown-linux-gnu.

It's very ugly so I'd appreciate suggestions on how to handle such
situations better?

gcc/cp/
        * pt.cc (coerce_template_parms): Release expanded argument
        vector when not needed.
---
 gcc/cp/pt.cc | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc
index 04f0a1d5fff..2c8b0d8609d 100644
--- a/gcc/cp/pt.cc
+++ b/gcc/cp/pt.cc
@@ -9442,6 +9442,9 @@ coerce_template_parms (tree parms,
     SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (new_inner_args,
                                         TREE_VEC_LENGTH (new_inner_args));
 
+  if ((return_full_args ? new_args != inner_args : new_inner_args != 
inner_args)
+      && inner_args != orig_inner_args)
+    ggc_free (inner_args);
   return return_full_args ? new_args : new_inner_args;
 }
 
-- 
2.43.0

Reply via email to