edit: sipa's responses didn't automatically load so the below is slightly outdated.
shrink_to_fit exists only on vectors
std::deque also has it, as well as e.g. our prevector
Actually, with this change I've found that this PR is using ClearShrink on 2 other types: bitdeque<> m_header_commitments and std::deque<CompressedHeader> m_redownloaded_headers.
Also, why is there a need to enforce it?
I'm not sure if we need to, I'm just raising the question. The main reason I'm considering is that we're currently only unit testing this on a std::vector and it seems a pretty specific function. Keeping usage in line with documentation and testing seems a sane thing to do, but again, it's just a consideration.
Finally, your suggestion would require the caller to specify the type V for every call, which seems suboptimal.
Hmm, can you elaborate? This diff compiles fine for me:
<details>
<summary>git diff</summary>
diff --git a/src/headerssync.cpp b/src/headerssync.cpp
index f891063cd2..09327a33fd 100644
--- a/src/headerssync.cpp
+++ b/src/headerssync.cpp
@@ -52,9 +52,9 @@ HeadersSyncState::HeadersSyncState(NodeId id, const Consensus::Params& consensus
void HeadersSyncState::Finalize()
{
Assume(m_download_state != State::FINAL);
- ClearShrink(m_header_commitments);
+ m_header_commitments = {};
m_last_header_received.SetNull();
- ClearShrink(m_redownloaded_headers);
+ m_redownloaded_headers = {};
m_redownload_buffer_last_hash.SetNull();
m_redownload_buffer_first_prev_hash.SetNull();
m_process_all_remaining_headers = false;
diff --git a/src/util/vector.h b/src/util/vector.h
index 2e83eb181b..09a9c409d4 100644
--- a/src/util/vector.h
+++ b/src/util/vector.h
@@ -51,7 +51,7 @@ inline V Cat(V v1, const V& v2)
/** Clear a vector and release its allocated memory. */
template<typename V>
-inline void ClearShrink(V& v) noexcept
+inline void ClearShrink(std::vector<V>& v) noexcept
{
// There are various ways to clear a vector and releasing its memory:
//
@@ -64,7 +64,7 @@ inline void ClearShrink(V& v) noexcept
// 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);
+ std::vector<V>{}.swap(v);
}
#endif // BITCOIN_UTIL_VECTOR_H
</details>