From: Martin Kronbichler Date: Tue, 24 May 2022 11:14:59 +0000 (+0200) Subject: Avoid a second copy in Utilities::pack for vector arguments X-Git-Tag: v9.4.0-rc1~163^2 X-Git-Url: https://gitweb.dealii.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=refs%2Fpull%2F13801%2Fhead;p=dealii.git Avoid a second copy in Utilities::pack for vector arguments --- 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())); }