Skip to content

Commit b2d0447

Browse files
stickies-vismaelsadeeq
authored andcommitted
bugfix: correct DisconnectedBlockTransactions memory usage
With `queuedTx` owning the `CTransactionRef` shared ptrs, they (and the managed objects) are entirely allocated on the heap. In `DisconnectedBlockTransactions::DynamicMemoryUsage`, we account for the 2 pointers that make up the shared_ptr, but not for the managed object (CTransaction) or the control block. Prior to this commit, by calculating the `RecursiveDynamicUsage` on a `CTransaction` whenever modifying `cachedInnerUsage`, we account for the dynamic usage of the `CTransaction`, i.e. the `vins` and `vouts` vectors, but we do not account for the `CTransaction` object itself, nor for the `CTransactionRef` control block. This means prior to this commit, `DynamicMemoryUsage` underestimates dynamic memory usage by not including the `CTransaction` objects and the shared ptr control blocks. Fix this by calculating `RecursiveDynamicUsage` on the `CTransactionRef` instead of the `CTransaction` whenever modifying `cachedInnerUsage`.
1 parent f4254e2 commit b2d0447

File tree

2 files changed

+4
-5
lines changed

2 files changed

+4
-5
lines changed

src/kernel/disconnected_transactions.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ std::vector<CTransactionRef> DisconnectedBlockTransactions::LimitMemoryUsage()
3434

3535
while (!queuedTx.empty() && DynamicMemoryUsage() > m_max_mem_usage) {
3636
evicted.emplace_back(queuedTx.front());
37-
cachedInnerUsage -= RecursiveDynamicUsage(*queuedTx.front());
37+
cachedInnerUsage -= RecursiveDynamicUsage(queuedTx.front());
3838
iters_by_txid.erase(queuedTx.front()->GetHash());
3939
queuedTx.pop_front();
4040
}
@@ -53,7 +53,7 @@ size_t DisconnectedBlockTransactions::DynamicMemoryUsage() const
5353
auto it = queuedTx.insert(queuedTx.end(), *block_it);
5454
auto [_, inserted] = iters_by_txid.emplace((*block_it)->GetHash(), it);
5555
assert(inserted); // callers may never pass multiple transactions with the same txid
56-
cachedInnerUsage += RecursiveDynamicUsage(**block_it);
56+
cachedInnerUsage += RecursiveDynamicUsage(*block_it);
5757
}
5858
return LimitMemoryUsage();
5959
}
@@ -69,7 +69,7 @@ void DisconnectedBlockTransactions::removeForBlock(const std::vector<CTransactio
6969
if (iter != iters_by_txid.end()) {
7070
auto list_iter = iter->second;
7171
iters_by_txid.erase(iter);
72-
cachedInnerUsage -= RecursiveDynamicUsage(**list_iter);
72+
cachedInnerUsage -= RecursiveDynamicUsage(*list_iter);
7373
queuedTx.erase(list_iter);
7474
}
7575
}

src/kernel/disconnected_transactions.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,7 @@ static const unsigned int MAX_DISCONNECTED_TX_POOL_BYTES{20'000'000};
3636
*/
3737
class DisconnectedBlockTransactions {
3838
private:
39-
/** Cached dynamic memory usage for the CTransactions (memory for the shared pointers is
40-
* included in the container calculations). */
39+
/** Cached dynamic memory usage for the `CTransactionRef`s */
4140
uint64_t cachedInnerUsage = 0;
4241
const size_t m_max_mem_usage;
4342
std::list<CTransactionRef> queuedTx;

0 commit comments

Comments
 (0)