Skip to content

Commit bfb9291

Browse files
committed
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.
1 parent fffc86f commit bfb9291

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

src/prevector.h

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -264,8 +264,10 @@ class prevector {
264264
fill(item_ptr(0), other.begin(), other.end());
265265
}
266266

267-
prevector(prevector<N, T, Size, Diff>&& other) noexcept {
268-
swap(other);
267+
prevector(prevector<N, T, Size, Diff>&& other) noexcept
268+
: _union(std::move(other._union)), _size(other._size)
269+
{
270+
other._size = 0;
269271
}
270272

271273
prevector& operator=(const prevector<N, T, Size, Diff>& other) {
@@ -277,7 +279,12 @@ class prevector {
277279
}
278280

279281
prevector& operator=(prevector<N, T, Size, Diff>&& other) noexcept {
280-
swap(other);
282+
if (!is_direct()) {
283+
free(_union.indirect_contents.indirect);
284+
}
285+
_union = std::move(other._union);
286+
_size = other._size;
287+
other._size = 0;
281288
return *this;
282289
}
283290

0 commit comments

Comments
 (0)