Or actually, could use std::list::remove_if. Quickly conveys the intent of the code imo, and looks like it speeds up the bench a tiny bit, going from
| ns/op | op/s | err% | total | benchmark
|--------------------:|--------------------:|--------:|----------:|:----------
| 7,204,642.33 | 138.80 | 0.2% | 0.87 | AddAndRemoveDisconnectedBlockTransactions10
| 5,293,310.64 | 188.92 | 0.4% | 0.64 | AddAndRemoveDisconnectedBlockTransactions100
| 6,931,825.73 | 144.26 | 0.9% | 0.84 | AddAndRemoveDisconnectedBlockTransactions90
to
| ns/op | op/s | err% | total | benchmark
|--------------------:|--------------------:|--------:|----------:|:----------
| 6,971,191.70 | 143.45 | 0.8% | 0.84 | AddAndRemoveDisconnectedBlockTransactions10
| 4,991,280.27 | 200.35 | 0.7% | 0.60 | AddAndRemoveDisconnectedBlockTransactions100
| 6,632,837.50 | 150.77 | 0.6% | 0.80 | AddAndRemoveDisconnectedBlockTransactions90
<details>
<summary>git diff</summary>
diff --git a/src/validation.h b/src/validation.h
index 3e1a024d09..c81331f950 100644
--- a/src/validation.h
+++ b/src/validation.h
@@ -283,15 +283,13 @@ struct DisconnectedBlockTransactions {
std::unordered_set<uint256, SaltedTxidHasher> txids;
std::transform(vtx.cbegin(), vtx.cend(), std::inserter(txids, txids.end()), [](const auto& tx) { return tx->GetHash(); });
// Iterate through entire list once, removing any transactions in the block.
- auto it = queuedTx.begin();
- while (it != queuedTx.end()) {
- auto it_next = std::next(it);
- if (txids.count((*it)->GetHash()) > 0) {
- cachedInnerUsage -= RecursiveDynamicUsage(*it);
- queuedTx.erase(it);
+ queuedTx.remove_if([&](const CTransactionRef& tx) {
+ if (txids.count(tx->GetHash()) > 0) {
+ cachedInnerUsage -= RecursiveDynamicUsage(tx);
+ return true;
}
- it = it_next;
- }
+ return false;
+ });
}
// Remove the earliest-inserted transaction.
</details>