[PATCH][C++] Save memory/time when folding CONSTRUCTORs

Richard Biener rguenther@suse.de
Wed Feb 1 15:19:00 GMT 2017


On Wed, 1 Feb 2017, Nathan Sidwell wrote:

> On 02/01/2017 09:03 AM, Richard Biener wrote:
> > 
> > Currently we copy CONSTRUCTORs we cp_fold even if no elements fold
> > (we throw the copy away then).  That's wasteful and we can easily
> > do the copying on-demand.  For simplicity the following resorts
> > to memcpy-ing the whole original vector on the first change
> > and only overwrites changed elements in the folding loop
> > (I suspect that's usually faster given initializer elements do
> > usually not fold(?)).
> > 
> > That removes another 35MB GC memory use from the PR12245 testcase.
> > (the biggest offender there is, non-surprisingly the C++ preprocessor
> > tokens with 130MB followed by 35MB for the CONSTRUCTOR and 40MB for
> > INTEGER_CSTs).
> > 
> > Bootstrap / regtest running on x86_64-unknown-linux-gnu.
> > 
> > Ok for trunk if that succeeds?
> > 
> > Thanks,
> > Richard.
> > 
> > 2017-02-01  Richard Biener  <rguenther@suse.de>
> > 
> > 	PR cp/14179
> > 	cp/
> > 	* cp-gimplify.c (cp_fold): When folding a CONSTRUCTOR copy
> > 	it lazily on the first changed element only and copy it
> > 	fully upfront, only storing changed elements.
> > 
> > Index: gcc/cp/cp-gimplify.c
> > ===================================================================
> > --- gcc/cp/cp-gimplify.c	(revision 245094)
> > +++ gcc/cp/cp-gimplify.c	(working copy)
> > @@ -2361,12 +2361,9 @@ cp_fold (tree x)
> >  	bool changed = false;
> >  	vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (x);
> >  	vec<constructor_elt, va_gc> *nelts = NULL;
> 
> > +		if (! changed)
> > +		  {
> > +		    nelts = elts->copy ();
> > +		    changed = true;
> > +		  }
> 
> doesn't this make 'changed' a synonym for 'nelts != NULL'?  (so you could
> ditch the former)

True.  Updated patch below.

Richard.

2017-02-01  Richard Biener  <rguenther@suse.de>

	PR cp/14179
	cp/
	* cp-gimplify.c (cp_fold): When folding a CONSTRUCTOR copy
	it lazily on the first changed element only and copy it
	fully upfront, only storing changed elements.

Index: gcc/cp/cp-gimplify.c
===================================================================
--- gcc/cp/cp-gimplify.c	(revision 245096)
+++ gcc/cp/cp-gimplify.c	(working copy)
@@ -2358,15 +2358,11 @@ cp_fold (tree x)
       {
 	unsigned i;
 	constructor_elt *p;
-	bool changed = false;
 	vec<constructor_elt, va_gc> *elts = CONSTRUCTOR_ELTS (x);
 	vec<constructor_elt, va_gc> *nelts = NULL;
-	vec_safe_reserve (nelts, vec_safe_length (elts));
 	FOR_EACH_VEC_SAFE_ELT (elts, i, p)
 	  {
 	    tree op = cp_fold (p->value);
-	    constructor_elt e = { p->index, op };
-	    nelts->quick_push (e);
 	    if (op != p->value)
 	      {
 		if (op == error_mark_node)
@@ -2375,10 +2371,12 @@ cp_fold (tree x)
 		    changed = false;
 		    break;
 		  }
-		changed = true;
+		if (nelts == NULL)
+		  nelts = elts->copy ();
+		(*nelts)[i].value = op;
 	      }
 	  }
-	if (changed)
+	if (nelts)
 	  x = build_constructor (TREE_TYPE (x), nelts);
 	else
 	  vec_free (nelts);



More information about the Gcc-patches mailing list