]> https://gitweb.dealii.org/ - dealii.git/commitdiff
Avoid initializing memory twice for non-trivial classes in table constructor.
authorMartin Kronbichler <kronbichler@lnm.mw.tum.de>
Sat, 9 Jan 2016 19:57:39 +0000 (20:57 +0100)
committerMartin Kronbichler <kronbichler@lnm.mw.tum.de>
Sun, 10 Jan 2016 08:20:38 +0000 (09:20 +0100)
include/deal.II/base/table.h

index 54c8f804d6ad5acc485f99d061dd407612207780..40646674db4b5a3579cceb3d665750515cd66c08 100644 (file)
@@ -1902,11 +1902,24 @@ TableBase<N,T>::reinit (const TableIndices<N> &new_sizes,
       return;
     }
 
-  // if new size is nonzero: if necessary allocate additional memory, and if
-  // not fast resize, zero out all values/default initialize them)
-  values.resize_fast (new_size);
+  // adjust values field. If it was empty before, we can simply call resize(),
+  // which can set all the data fields. Otherwise, select the fast resize and
+  // manually fill in all the elements as required by the design of this
+  // class. (Selecting another code for the empty case ensures that we touch
+  // the memory only once for non-trivial classes that need to initialize the
+  // memory also in resize_fast.)
   if (!omit_default_initialization)
-    values.fill(T());
+    {
+      if (values.empty())
+        values.resize(new_size, T());
+      else
+        {
+          values.resize_fast(new_size);
+          values.fill(T());
+        }
+    }
+  else
+    values.resize_fast (new_size);
 }
 
 

In the beginning the Universe was created. This has made a lot of people very angry and has been widely regarded as a bad move.

Douglas Adams


Typeset in Trocchi and Trocchi Bold Sans Serif.