mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-04-29 23:09:44 -04:00
Do not use std::vector = {} to release memory
Github-Pull: #28452
Rebased-From: 3fcd7fc7ff
This commit is contained in:
parent
defdc15023
commit
67b6d99aea
3 changed files with 46 additions and 2 deletions
|
@ -7,6 +7,7 @@
|
||||||
#include <pow.h>
|
#include <pow.h>
|
||||||
#include <timedata.h>
|
#include <timedata.h>
|
||||||
#include <util/check.h>
|
#include <util/check.h>
|
||||||
|
#include <util/vector.h>
|
||||||
|
|
||||||
// The two constants below are computed using the simulation script on
|
// The two constants below are computed using the simulation script on
|
||||||
// https://gist.github.com/sipa/016ae445c132cdf65a2791534dfb7ae1
|
// https://gist.github.com/sipa/016ae445c132cdf65a2791534dfb7ae1
|
||||||
|
@ -51,9 +52,9 @@ HeadersSyncState::HeadersSyncState(NodeId id, const Consensus::Params& consensus
|
||||||
void HeadersSyncState::Finalize()
|
void HeadersSyncState::Finalize()
|
||||||
{
|
{
|
||||||
Assume(m_download_state != State::FINAL);
|
Assume(m_download_state != State::FINAL);
|
||||||
m_header_commitments = {};
|
ClearShrink(m_header_commitments);
|
||||||
m_last_header_received.SetNull();
|
m_last_header_received.SetNull();
|
||||||
m_redownloaded_headers = {};
|
ClearShrink(m_redownloaded_headers);
|
||||||
m_redownload_buffer_last_hash.SetNull();
|
m_redownload_buffer_last_hash.SetNull();
|
||||||
m_redownload_buffer_first_prev_hash.SetNull();
|
m_redownload_buffer_first_prev_hash.SetNull();
|
||||||
m_process_all_remaining_headers = false;
|
m_process_all_remaining_headers = false;
|
||||||
|
|
|
@ -2782,4 +2782,29 @@ BOOST_AUTO_TEST_CASE(util_WriteBinaryFile)
|
||||||
BOOST_CHECK(valid);
|
BOOST_CHECK(valid);
|
||||||
BOOST_CHECK_EQUAL(actual_text, expected_text);
|
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()
|
BOOST_AUTO_TEST_SUITE_END()
|
||||||
|
|
|
@ -49,4 +49,22 @@ inline V Cat(V v1, const V& v2)
|
||||||
return v1;
|
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
|
#endif // BITCOIN_UTIL_VECTOR_H
|
||||||
|
|
Loading…
Add table
Reference in a new issue