Commit 8ef6729
committed
Merge bitcoin/bitcoin#28452: Do not use std::vector = {} to release memory
3fcd7fc Do not use std::vector = {} to release memory (Pieter Wuille)
Pull request description:
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).
To explain what is going on: `v = {...};` is equivalent in general to `v.operator=({...});`. For many types, the `{}` is converted to the type of `v`, and then assigned to `v` - which for `std::vector` would ordinarily have the effect of clearing its memory (constructing a new empty vector, and then move-assigning it to `v`). However, since `std::vector<T>` has an `operator=(std::initializer_list<T>)` defined, it has precedence (since no implicit conversion is needed), and with an empty list, that is equivalent to `clear()`.
I did consider using `v = std::vector<T>{};` as replacement for `v = {};` instances where memory releasing is desired, but it appears that it does not actually work universally either. `V{}.swap(v);` does.
ACKs for top commit:
ajtowns:
utACK 3fcd7fc
stickies-v:
ACK 3fcd7fc
theStack:
Code-review ACK 3fcd7fc
Tree-SHA512: 6148558126ec3c8cfd6daee167ec1c67b360cf1dff2cbc132bd71768337cf9bc4dda3e5a9cf7da4f7457d2123288eeba77dd78f3a17fa2cfd9c6758262950cc54 files changed
+57
-14
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
| 10 | + | |
10 | 11 | | |
11 | 12 | | |
12 | 13 | | |
| |||
51 | 52 | | |
52 | 53 | | |
53 | 54 | | |
54 | | - | |
| 55 | + | |
55 | 56 | | |
56 | | - | |
| 57 | + | |
57 | 58 | | |
58 | 59 | | |
59 | 60 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
35 | 35 | | |
36 | 36 | | |
37 | 37 | | |
| 38 | + | |
38 | 39 | | |
39 | 40 | | |
40 | 41 | | |
| |||
899 | 900 | | |
900 | 901 | | |
901 | 902 | | |
902 | | - | |
903 | | - | |
| 903 | + | |
904 | 904 | | |
905 | 905 | | |
906 | 906 | | |
| |||
1123 | 1123 | | |
1124 | 1124 | | |
1125 | 1125 | | |
1126 | | - | |
1127 | | - | |
| 1126 | + | |
| 1127 | + | |
1128 | 1128 | | |
1129 | 1129 | | |
1130 | 1130 | | |
| |||
1184 | 1184 | | |
1185 | 1185 | | |
1186 | 1186 | | |
1187 | | - | |
1188 | | - | |
| 1187 | + | |
1189 | 1188 | | |
1190 | 1189 | | |
1191 | 1190 | | |
| |||
1275 | 1274 | | |
1276 | 1275 | | |
1277 | 1276 | | |
1278 | | - | |
| 1277 | + | |
1279 | 1278 | | |
1280 | 1279 | | |
1281 | 1280 | | |
| |||
1295 | 1294 | | |
1296 | 1295 | | |
1297 | 1296 | | |
1298 | | - | |
| 1297 | + | |
1299 | 1298 | | |
1300 | | - | |
| 1299 | + | |
1301 | 1300 | | |
1302 | 1301 | | |
1303 | 1302 | | |
| |||
1511 | 1510 | | |
1512 | 1511 | | |
1513 | 1512 | | |
1514 | | - | |
| 1513 | + | |
1515 | 1514 | | |
1516 | 1515 | | |
1517 | 1516 | | |
| |||
1545 | 1544 | | |
1546 | 1545 | | |
1547 | 1546 | | |
1548 | | - | |
| 1547 | + | |
1549 | 1548 | | |
1550 | 1549 | | |
1551 | 1550 | | |
| |||
1577 | 1576 | | |
1578 | 1577 | | |
1579 | 1578 | | |
1580 | | - | |
| 1579 | + | |
1581 | 1580 | | |
1582 | 1581 | | |
1583 | 1582 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1791 | 1791 | | |
1792 | 1792 | | |
1793 | 1793 | | |
| 1794 | + | |
| 1795 | + | |
| 1796 | + | |
| 1797 | + | |
| 1798 | + | |
| 1799 | + | |
| 1800 | + | |
| 1801 | + | |
| 1802 | + | |
| 1803 | + | |
| 1804 | + | |
| 1805 | + | |
| 1806 | + | |
| 1807 | + | |
| 1808 | + | |
| 1809 | + | |
| 1810 | + | |
| 1811 | + | |
| 1812 | + | |
| 1813 | + | |
| 1814 | + | |
| 1815 | + | |
| 1816 | + | |
| 1817 | + | |
| 1818 | + | |
1794 | 1819 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
49 | 49 | | |
50 | 50 | | |
51 | 51 | | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
52 | 70 | | |
0 commit comments