From c0280afab3fbf83ce3e16e3b20d1938be7e1faf5 Mon Sep 17 00:00:00 2001 From: Martin Kronbichler Date: Tue, 24 May 2022 13:14:59 +0200 Subject: [PATCH] Avoid a second copy in Utilities::pack for vector arguments --- include/deal.II/base/utilities.h | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/include/deal.II/base/utilities.h b/include/deal.II/base/utilities.h index 9ff8ff10eb..1c29fcbdad 100644 --- a/include/deal.II/base/utilities.h +++ b/include/deal.II/base/utilities.h @@ -1265,20 +1265,24 @@ namespace Utilities const std::vector &object, std::vector & dest_buffer) { - const auto current_position = dest_buffer.size(); const typename std::vector::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(&vector_size), + reinterpret_cast(&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(object.data()), + reinterpret_cast(object.data() + + object.size())); } -- 2.39.5