Skip to content

Commit bd83111

Browse files
committed
Optimization: Coin&& to ApplyTxInUndo
This avoids a prevector copy in ApplyTxInUndo.
1 parent cb2c7fd commit bd83111

File tree

2 files changed

+8
-8
lines changed

2 files changed

+8
-8
lines changed

src/test/coins_tests.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
#include <boost/test/unit_test.hpp>
1919

20-
int ApplyTxInUndo(const Coin& undo, CCoinsViewCache& view, const COutPoint& out);
20+
int ApplyTxInUndo(Coin&& undo, CCoinsViewCache& view, const COutPoint& out);
2121
void UpdateCoins(const CTransaction& tx, CCoinsViewCache& inputs, CTxUndo &txundo, int nHeight);
2222

2323
namespace
@@ -371,8 +371,8 @@ BOOST_AUTO_TEST_CASE(updatecoins_simulation_test)
371371
// restore inputs
372372
if (!tx.IsCoinBase()) {
373373
const COutPoint &out = tx.vin[0].prevout;
374-
const Coin &undoin = undo.vprevout[0];
375-
ApplyTxInUndo(undoin, *(stack.back()), out);
374+
Coin coin = undo.vprevout[0];
375+
ApplyTxInUndo(std::move(coin), *(stack.back()), out);
376376
}
377377
// Store as a candidate for reconnection
378378
disconnectedids.insert(undohash);

src/validation.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1256,7 +1256,7 @@ enum DisconnectResult
12561256
* @param out The out point that corresponds to the tx input.
12571257
* @return A DisconnectResult as an int
12581258
*/
1259-
int ApplyTxInUndo(const Coin& undo, CCoinsViewCache& view, const COutPoint& out)
1259+
int ApplyTxInUndo(Coin&& undo, CCoinsViewCache& view, const COutPoint& out)
12601260
{
12611261
bool fClean = true;
12621262

@@ -1277,7 +1277,7 @@ int ApplyTxInUndo(const Coin& undo, CCoinsViewCache& view, const COutPoint& out)
12771277
if (coins->IsAvailable(out.n)) fClean = false; // overwriting existing output
12781278
if (coins->vout.size() < out.n+1)
12791279
coins->vout.resize(out.n+1);
1280-
coins->vout[out.n] = undo.out;
1280+
coins->vout[out.n] = std::move(undo.out);
12811281

12821282
return fClean ? DISCONNECT_OK : DISCONNECT_UNCLEAN;
12831283
}
@@ -1326,18 +1326,18 @@ static DisconnectResult DisconnectBlock(const CBlock& block, const CBlockIndex*
13261326

13271327
// restore inputs
13281328
if (i > 0) { // not coinbases
1329-
const CTxUndo &txundo = blockUndo.vtxundo[i-1];
1329+
CTxUndo &txundo = blockUndo.vtxundo[i-1];
13301330
if (txundo.vprevout.size() != tx.vin.size()) {
13311331
error("DisconnectBlock(): transaction and undo data inconsistent");
13321332
return DISCONNECT_FAILED;
13331333
}
13341334
for (unsigned int j = tx.vin.size(); j-- > 0;) {
13351335
const COutPoint &out = tx.vin[j].prevout;
1336-
const Coin &undo = txundo.vprevout[j];
1337-
int res = ApplyTxInUndo(undo, view, out);
1336+
int res = ApplyTxInUndo(std::move(txundo.vprevout[j]), view, out);
13381337
if (res == DISCONNECT_FAILED) return DISCONNECT_FAILED;
13391338
fClean = fClean && res != DISCONNECT_UNCLEAN;
13401339
}
1340+
// At this point, all of txundo.vprevout should have been moved out.
13411341
}
13421342
}
13431343

0 commit comments

Comments
 (0)