From bfb9291a8661fe5b26c14ed755cfa89d27c37110 Mon Sep 17 00:00:00 2001 From: Martin Leitner-Ankerl Date: Sun, 26 Mar 2023 14:40:32 +0200 Subject: [PATCH] util: implement prevector's move ctor & move assignment Using swap() was rather wasteful because it had to copy the whole direct memory data twice. Also, due to the swap() in move assignment the moved-from object might hold on to unused memory for longer than necessary. --- src/prevector.h | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/prevector.h b/src/prevector.h index d3e4b8fd0d2..bcab1ff00cd 100644 --- a/src/prevector.h +++ b/src/prevector.h @@ -264,8 +264,10 @@ public: fill(item_ptr(0), other.begin(), other.end()); } - prevector(prevector&& other) noexcept { - swap(other); + prevector(prevector&& other) noexcept + : _union(std::move(other._union)), _size(other._size) + { + other._size = 0; } prevector& operator=(const prevector& other) { @@ -277,7 +279,12 @@ public: } prevector& operator=(prevector&& other) noexcept { - swap(other); + if (!is_direct()) { + free(_union.indirect_contents.indirect); + } + _union = std::move(other._union); + _size = other._size; + other._size = 0; return *this; }