aboutsummaryrefslogtreecommitdiff
path: root/src/prevector.h
diff options
context:
space:
mode:
authorMartin Leitner-Ankerl <martin.ankerl@gmail.com>2023-03-26 14:40:32 +0200
committerMartin Leitner-Ankerl <martin.ankerl@gmail.com>2023-03-26 15:49:52 +0200
commitbfb9291a8661fe5b26c14ed755cfa89d27c37110 (patch)
treeea607b8f5cc1305319dec6b60666420e8209dbdb /src/prevector.h
parentfffc86f49f4eeb811b8438bc1b7f8d9e05882c6f (diff)
downloadbitcoin-bfb9291a8661fe5b26c14ed755cfa89d27c37110.tar.xz
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.
Diffstat (limited to 'src/prevector.h')
-rw-r--r--src/prevector.h13
1 files changed, 10 insertions, 3 deletions
diff --git a/src/prevector.h b/src/prevector.h
index d3e4b8fd0d..bcab1ff00c 100644
--- a/src/prevector.h
+++ b/src/prevector.h
@@ -264,8 +264,10 @@ public:
fill(item_ptr(0), other.begin(), other.end());
}
- prevector(prevector<N, T, Size, Diff>&& other) noexcept {
- swap(other);
+ prevector(prevector<N, T, Size, Diff>&& other) noexcept
+ : _union(std::move(other._union)), _size(other._size)
+ {
+ other._size = 0;
}
prevector& operator=(const prevector<N, T, Size, Diff>& other) {
@@ -277,7 +279,12 @@ public:
}
prevector& operator=(prevector<N, T, Size, Diff>&& 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;
}