Skip to content

Commit

Permalink
Do not use std::vector = {} to release memory
Browse files Browse the repository at this point in the history
Github-Pull: #28452
Rebased-From: 3fcd7fc
  • Loading branch information
sipa authored and fanquake committed Sep 26, 2023
1 parent defdc15 commit 67b6d99
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/headerssync.cpp
Expand Up @@ -7,6 +7,7 @@
#include <pow.h>
#include <timedata.h>
#include <util/check.h>
#include <util/vector.h>

// The two constants below are computed using the simulation script on
// https://gist.github.com/sipa/016ae445c132cdf65a2791534dfb7ae1
Expand Down Expand Up @@ -51,9 +52,9 @@ HeadersSyncState::HeadersSyncState(NodeId id, const Consensus::Params& consensus
void HeadersSyncState::Finalize()
{
Assume(m_download_state != State::FINAL);
m_header_commitments = {};
ClearShrink(m_header_commitments);
m_last_header_received.SetNull();
m_redownloaded_headers = {};
ClearShrink(m_redownloaded_headers);
m_redownload_buffer_last_hash.SetNull();
m_redownload_buffer_first_prev_hash.SetNull();
m_process_all_remaining_headers = false;
Expand Down
25 changes: 25 additions & 0 deletions src/test/util_tests.cpp
Expand Up @@ -2782,4 +2782,29 @@ BOOST_AUTO_TEST_CASE(util_WriteBinaryFile)
BOOST_CHECK(valid);
BOOST_CHECK_EQUAL(actual_text, expected_text);
}

BOOST_AUTO_TEST_CASE(clearshrink_test)
{
{
std::vector<uint8_t> v = {1, 2, 3};
ClearShrink(v);
BOOST_CHECK_EQUAL(v.size(), 0);
BOOST_CHECK_EQUAL(v.capacity(), 0);
}

{
std::vector<bool> v = {false, true, false, false, true, true};
ClearShrink(v);
BOOST_CHECK_EQUAL(v.size(), 0);
BOOST_CHECK_EQUAL(v.capacity(), 0);
}

{
std::deque<int> v = {1, 3, 3, 7};
ClearShrink(v);
BOOST_CHECK_EQUAL(v.size(), 0);
// std::deque has no capacity() we can observe.
}
}

BOOST_AUTO_TEST_SUITE_END()
18 changes: 18 additions & 0 deletions src/util/vector.h
Expand Up @@ -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

0 comments on commit 67b6d99

Please sign in to comment.