diff --git a/src/checkpoint/container/vector_serialize.h b/src/checkpoint/container/vector_serialize.h index 09dd2b1b..3e96de04 100644 --- a/src/checkpoint/container/vector_serialize.h +++ b/src/checkpoint/container/vector_serialize.h @@ -97,14 +97,22 @@ void constructVectorDataWithResize( ); } +template +struct Allocated { + Allocated() : buf{dispatch::Standard::template allocate()} { } + ~Allocated() { std::allocator{}.deallocate(reinterpret_cast(buf), 1); } + + SerialByteType* buf; +}; + template void constructVectorDataReconstruct( SerialSizeType const vec_size, std::vector& vec, typename dispatch::Reconstructor::template isReconstructibleType* = nullptr ) { - auto buf = dispatch::Standard::template allocate(); + Allocated const allocated; for (SerialSizeType i = 0; i < vec_size; ++i) { - auto& t = T::reconstruct(buf); + auto& t = T::reconstruct(allocated.buf); vec.emplace_back(std::move(t)); } } @@ -114,10 +122,10 @@ void constructVectorDataReconstruct( SerialSizeType const vec_size, std::vector& vec, typename dispatch::Reconstructor::template isNonIntReconstructibleType* = nullptr ) { - auto buf = dispatch::Standard::allocate(); + Allocated const allocated; for (SerialSizeType i = 0; i < vec_size; ++i) { T* t = nullptr; - reconstruct(t, buf); + reconstruct(t, allocated.buf); vec.emplace_back(std::move(*t)); } } @@ -135,9 +143,7 @@ void constructVectorData( SerialSizeType const vec_size, std::vector& vec, typename dispatch::Reconstructor::template isTaggedConstructibleType* = nullptr ) { - for (SerialSizeType i = 0; i < vec_size; ++i) { - vec.emplace_back(SERIALIZE_CONSTRUCT_TAG{}); - } + vec.resize(vec_size, T{SERIALIZE_CONSTRUCT_TAG{}}); } template @@ -151,7 +157,11 @@ void constructVectorData( template void serialize(Serializer& s, std::vector& vec) { auto const vec_size = serializeVectorMeta(s, vec); - constructVectorData(vec_size, vec); + + if (s.isUnpacking()) { + constructVectorData(vec_size, vec); + } + dispatch::serializeArray(s, vec.data(), vec.size()); // make sure to account for reserved space when footprinting @@ -166,7 +176,10 @@ void serialize(Serializer& s, std::vector& vec) { } auto const vec_size = serializeVectorMeta(s, vec); - constructVectorData(vec_size, vec); + + if (s.isUnpacking()) { + constructVectorData(vec_size, vec); + } if (!s.isUnpacking()) { for (bool elt : vec) {