]> https://gitweb.dealii.org/ - dealii.git/commitdiff
Avoid a second copy in Utilities::pack for vector arguments 13801/head
authorMartin Kronbichler <martin.kronbichler@uni-a.de>
Tue, 24 May 2022 11:14:59 +0000 (13:14 +0200)
committerMartin Kronbichler <martin.kronbichler@uni-a.de>
Tue, 24 May 2022 11:14:59 +0000 (13:14 +0200)
include/deal.II/base/utilities.h

index 9ff8ff10ebd729932d97acfbcab619f84d6ee298..1c29fcbdad50548087f49025ee371184c39cdd38 100644 (file)
@@ -1265,20 +1265,24 @@ namespace Utilities
       const std::vector<T> &object,
       std::vector<char> &   dest_buffer)
     {
-      const auto current_position                          = dest_buffer.size();
       const typename std::vector<T>::size_type vector_size = object.size();
 
-      // Resize the buffer so that it can store the size of 'object'
-      // as well as all of its elements.
-      dest_buffer.resize(dest_buffer.size() + sizeof(vector_size) +
-                         object.size() * sizeof(T));
+      // Reserve for the buffer so that it can store the size of 'object' as
+      // well as all of its elements.
+      dest_buffer.reserve(dest_buffer.size() + sizeof(vector_size) +
+                          object.size() * sizeof(T));
 
-      // Then copy first the size and then the elements:
-      memcpy(&dest_buffer[current_position], &vector_size, sizeof(vector_size));
+      // Copy the size into the vector
+      dest_buffer.insert(dest_buffer.end(),
+                         reinterpret_cast<const char *>(&vector_size),
+                         reinterpret_cast<const char *>(&vector_size + 1));
+
+      // Insert the elements at the end of the vector:
       if (object.size() > 0)
-        memcpy(&dest_buffer[current_position] + sizeof(vector_size),
-               object.data(),
-               object.size() * sizeof(T));
+        dest_buffer.insert(dest_buffer.end(),
+                           reinterpret_cast<const char *>(object.data()),
+                           reinterpret_cast<const char *>(object.data() +
+                                                          object.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.