Do not use std::vector = {} to release memory

This commit is contained in:
Pieter Wuille
2023-09-11 13:54:32 -04:00
parent fd69ffbbfb
commit 3fcd7fc7ff
4 changed files with 57 additions and 14 deletions

View File

@@ -49,4 +49,22 @@ inline V Cat(V v1, const V& v2)
return v1;
}
/** Clear a vector (or std::deque) and release its allocated memory. */
template<typename V>
inline void ClearShrink(V& v) noexcept
{
// There are various ways to clear a vector and release its memory:
//
// 1. V{}.swap(v)
// 2. v = V{}
// 3. v = {}; v.shrink_to_fit();
// 4. v.clear(); v.shrink_to_fit();
//
// (2) does not appear to release memory in glibc debug mode, even if v.shrink_to_fit()
// follows. (3) and (4) rely on std::vector::shrink_to_fit, which is only a non-binding
// request. Therefore, we use method (1).
V{}.swap(v);
}
#endif // BITCOIN_UTIL_VECTOR_H