HomePhabricator

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

Description

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

Summary:

It appears that invoking v = {}; for an std::vector<...> v is equivalent to v.clear(), which does not release its allocated memory. There are a number of places in the codebase where it appears to be used for that purpose however (mostly written by me). Replace those with std::vector<...>{}.swap(v); (using a helper function ClearShrink in util/vector.h).

For now the net.cpp changes are not applicable, as they depend on V2Transport backports. The change is useful anyway even if we just apply it to the headersync data. IIUC we could save multiple megabytes of memory, that would otherwsise not be freed during the entire lifetime of the connection, per peer involved in header pre-sync . m_redownloaded_headers is a buffer that can stores block headers during the redownload phase of header synchronization.

This is a backport of core#28452

Test Plan: ninja all check-all

Reviewers: #bitcoin_abc, Fabien

Reviewed By: #bitcoin_abc, Fabien

Differential Revision: https://reviews.bitcoinabc.org/D18446

Details

Provenance
Pieter Wuille <pieter@wuille.net>Authored on Aug 11 2025, 12:06
PiRKCommitted on Aug 11 2025, 12:07
PiRKPushed on Aug 11 2025, 12:07
Reviewer
Restricted Project
Differential Revision
D18446: Do not use std::vector = {} to release memory
Parents
rABC0314c7b58d96: [Automated] Update seeds
Branches
Unknown
Tags
Unknown