Page MenuHomePhabricator

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

Authored by PiRK on Aug 5 2025, 15:27.

Details

Reviewers
Fabien
Group Reviewers
Restricted Project
Commits
rABC287640f183f2: 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

Diff Detail

Repository
rABC Bitcoin ABC
Lint
Lint Not Applicable
Unit
Tests Not Applicable