C++ PATCH for c++/86320, memory-hog with std::array of pair

Message ID CADzB+2mSbjJaNCM73SYrW1q1n-xw6A72+T+C=EMQT00kw5OcwQ@mail.gmail.com
State New
Headers show
  • C++ PATCH for c++/86320, memory-hog with std::array of pair
Related show

Commit Message

Jason Merrill June 27, 2018, 2:59 a.m.
In this PR, we have a large std::array of pairs.  Since the C array is
wrapped in a class we don't go to build_vec_init, so we end up with
digest_init wanting to build up the element initializer for each
element of the array.

In the more general case, like 80272, we have a data structure
problem: we don't currently have a good way of expressing the same
dynamic initialization of many elements within a CONSTRUCTOR.
RANGE_EXPR probably ought to work, but will need more work at
genericize or gimplify time.

But in this case, the initialization for each element reduces to
constant 0, so we don't even need to add anything to the CONSTRUCTOR.
We just need to realize that if the initializer for one element is 0,
the others will be as well, and we don't need to iterate over the
whole array.

For the trunk, I also use a RANGE_EXPR to handle constant
initialization by a value other than 0.

#include <array>
#include <utility>

void foo ()
  std::array<std::pair<int, int>, 1024 * 1024> arr {};

Tested x86_64-pc-linux-gnu, applying to trunk and 8.


commit 484d1a56ac83f53bf0a6064d9762acceb9d2f158
Author: Jason Merrill <jason@redhat.com>
Date:   Tue Jun 26 14:58:58 2018 -0400

            PR c++/86320 - memory-hog with std::array of pair
            * typeck2.c (process_init_constructor_array): If zero-initialization
            is fine for one element, we're done.

diff --git a/gcc/cp/typeck2.c b/gcc/cp/typeck2.c
index 43e236de41c..5f738f038f0 100644
--- a/gcc/cp/typeck2.c
+++ b/gcc/cp/typeck2.c
@@ -1367,6 +1367,9 @@  process_init_constructor_array (tree type, tree init, int nested,
 	    flags |= picflag_from_initializer (next);
 	    CONSTRUCTOR_APPEND_ELT (v, size_int (i), next);
+	else
+	  /* Don't bother checking all the other elements.  */
+	  break;
   CONSTRUCTOR_ELTS (init) = v;