https://gcc.gnu.org/bugzilla/show_bug.cgi?id=70277

--- Comment #3 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
Let's look at
#include <string>
#include <vector>

int
main ()
{
  std::vector<std::string> v {
    "0", "1", "2"
  };
}

which is IMHO below the limit for the proposed optimization, but on the other
side easier to explain.  We already have there
  const struct basic_string D.26903[3];
array, the proposal is if the C++ FE finds long enough range of initializers
with the same type it can add also
  static const char *D.xxxxx[3] = { "0", "1", "2" };
    {
    struct vector v;

    try
      {
        std::allocator<char>::allocator (&D.26895);
        int D.yyyyy;
        D.yyyyy = 0;
        try
          {
            while (D.yyyyy < 3)
              {
                std::allocator<char>::allocator (&D.26895);
                std::basic_string<char>::basic_string (&D.26903[D.yyyyy],
D.xxxxx[D.yyyyy], &D.26895);
              }
            ...
          }
        finally
          {
            // Destruct only what has been constructed already in reverse
order, use D.yyyyy for that.
          }
      }
...

It doesn't have to be just char literals, it can be any other kind of
constants, and it will certainly result in better generated code, unroller can
unroll it if beneficial later, but it will help inlining etc., compared to
having hundreds of thousands of statements just to construct something.

Reply via email to