Optimization: Coin&& to ApplyTxInUndo

This avoids a prevector copy in ApplyTxInUndo.
This commit is contained in:
Pieter Wuille 2017-04-25 11:29:27 -07:00
parent cb2c7fdac2
commit bd83111a0f
2 changed files with 8 additions and 8 deletions

View File

@ -17,7 +17,7 @@
#include <boost/test/unit_test.hpp> #include <boost/test/unit_test.hpp>
int ApplyTxInUndo(const Coin& undo, CCoinsViewCache& view, const COutPoint& out); int ApplyTxInUndo(Coin&& undo, CCoinsViewCache& view, const COutPoint& out);
void UpdateCoins(const CTransaction& tx, CCoinsViewCache& inputs, CTxUndo &txundo, int nHeight); void UpdateCoins(const CTransaction& tx, CCoinsViewCache& inputs, CTxUndo &txundo, int nHeight);
namespace namespace
@ -371,8 +371,8 @@ BOOST_AUTO_TEST_CASE(updatecoins_simulation_test)
// restore inputs // restore inputs
if (!tx.IsCoinBase()) { if (!tx.IsCoinBase()) {
const COutPoint &out = tx.vin[0].prevout; const COutPoint &out = tx.vin[0].prevout;
const Coin &undoin = undo.vprevout[0]; Coin coin = undo.vprevout[0];
ApplyTxInUndo(undoin, *(stack.back()), out); ApplyTxInUndo(std::move(coin), *(stack.back()), out);
} }
// Store as a candidate for reconnection // Store as a candidate for reconnection
disconnectedids.insert(undohash); disconnectedids.insert(undohash);

View File

@ -1256,7 +1256,7 @@ enum DisconnectResult
* @param out The out point that corresponds to the tx input. * @param out The out point that corresponds to the tx input.
* @return A DisconnectResult as an int * @return A DisconnectResult as an int
*/ */
int ApplyTxInUndo(const Coin& undo, CCoinsViewCache& view, const COutPoint& out) int ApplyTxInUndo(Coin&& undo, CCoinsViewCache& view, const COutPoint& out)
{ {
bool fClean = true; bool fClean = true;
@ -1277,7 +1277,7 @@ int ApplyTxInUndo(const Coin& undo, CCoinsViewCache& view, const COutPoint& out)
if (coins->IsAvailable(out.n)) fClean = false; // overwriting existing output if (coins->IsAvailable(out.n)) fClean = false; // overwriting existing output
if (coins->vout.size() < out.n+1) if (coins->vout.size() < out.n+1)
coins->vout.resize(out.n+1); coins->vout.resize(out.n+1);
coins->vout[out.n] = undo.out; coins->vout[out.n] = std::move(undo.out);
return fClean ? DISCONNECT_OK : DISCONNECT_UNCLEAN; return fClean ? DISCONNECT_OK : DISCONNECT_UNCLEAN;
} }
@ -1326,18 +1326,18 @@ static DisconnectResult DisconnectBlock(const CBlock& block, const CBlockIndex*
// restore inputs // restore inputs
if (i > 0) { // not coinbases if (i > 0) { // not coinbases
const CTxUndo &txundo = blockUndo.vtxundo[i-1]; CTxUndo &txundo = blockUndo.vtxundo[i-1];
if (txundo.vprevout.size() != tx.vin.size()) { if (txundo.vprevout.size() != tx.vin.size()) {
error("DisconnectBlock(): transaction and undo data inconsistent"); error("DisconnectBlock(): transaction and undo data inconsistent");
return DISCONNECT_FAILED; return DISCONNECT_FAILED;
} }
for (unsigned int j = tx.vin.size(); j-- > 0;) { for (unsigned int j = tx.vin.size(); j-- > 0;) {
const COutPoint &out = tx.vin[j].prevout; const COutPoint &out = tx.vin[j].prevout;
const Coin &undo = txundo.vprevout[j]; int res = ApplyTxInUndo(std::move(txundo.vprevout[j]), view, out);
int res = ApplyTxInUndo(undo, view, out);
if (res == DISCONNECT_FAILED) return DISCONNECT_FAILED; if (res == DISCONNECT_FAILED) return DISCONNECT_FAILED;
fClean = fClean && res != DISCONNECT_UNCLEAN; fClean = fClean && res != DISCONNECT_UNCLEAN;
} }
// At this point, all of txundo.vprevout should have been moved out.
} }
} }