]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gccrs: prealloc the initilizer vector
authorPhilip Herron <herron.philip@googlemail.com>
Thu, 17 Apr 2025 15:19:35 +0000 (16:19 +0100)
committerArthur Cohen <arthur.cohen@embecosm.com>
Mon, 28 Apr 2025 14:18:55 +0000 (16:18 +0200)
There are two cases when initilizing an array, this is the
const context which means we need to build the array ctor,
which means using lots of memory, its super inefficient
because we are using a big wrapper over the GCC internals here
but preallocating the vectors here causes a:

  terminate called after throwing an instance of 'std::bad_alloc'

So this is a handy error condition to rely on for this senario.

Fixes Rust-GCC#3713
Fixes Rust-GCC#3727

gcc/rust/ChangeLog:

* backend/rust-compile-expr.cc (CompileExpr::array_copied_expr): prealloc the vector

Signed-off-by: Philip Herron <herron.philip@googlemail.com>
gcc/rust/backend/rust-compile-expr.cc

index 141086fc0847d875b156ed6ccc083c7d33282876..dd3420f9f2fa055b789a94c2ae276ac5317d8e2e 100644 (file)
@@ -1972,8 +1972,12 @@ CompileExpr::array_copied_expr (location_t expr_locus,
   if (ctx->const_context_p ())
     {
       size_t idx = 0;
+
       std::vector<unsigned long> indexes;
       std::vector<tree> constructor;
+
+      indexes.reserve (len);
+      constructor.reserve (len);
       for (unsigned HOST_WIDE_INT i = 0; i < len; i++)
        {
          constructor.push_back (translated_expr);