Skip to content

Commit ec81881

Browse files
committed
Performance Regression Fix: Pre-Allocate txChanged vector
1 parent ec139a5 commit ec81881

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

src/main.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2793,7 +2793,7 @@ static int64_t nTimePostConnect = 0;
27932793
* Connect a new block to chainActive. pblock is either NULL or a pointer to a CBlock
27942794
* corresponding to pindexNew, to bypass loading it again from disk.
27952795
*/
2796-
bool static ConnectTip(CValidationState& state, const CChainParams& chainparams, CBlockIndex* pindexNew, const CBlock* pblock, std::list<CTransaction> &txConflicted, std::vector<std::tuple<CTransaction,CBlockIndex*,int> > &txChanged)
2796+
bool static ConnectTip(CValidationState& state, const CChainParams& chainparams, CBlockIndex* pindexNew, const CBlock* pblock, std::list<CTransaction> &txConflicted, std::vector<std::tuple<CTransaction,CBlockIndex*,int>> &txChanged)
27972797
{
27982798
assert(pindexNew->pprev == chainActive.Tip());
27992799
// Read block from disk.
@@ -2835,7 +2835,7 @@ bool static ConnectTip(CValidationState& state, const CChainParams& chainparams,
28352835
UpdateTip(pindexNew, chainparams);
28362836

28372837
for(unsigned int i=0; i < pblock->vtx.size(); i++)
2838-
txChanged.push_back(std::make_tuple(pblock->vtx[i], pindexNew, i));
2838+
txChanged.emplace_back(pblock->vtx[i], pindexNew, i);
28392839

28402840
int64_t nTime6 = GetTimeMicros(); nTimePostConnect += nTime6 - nTime5; nTimeTotal += nTime6 - nTime1;
28412841
LogPrint("bench", " - Connect postprocess: %.2fms [%.2fs]\n", (nTime6 - nTime5) * 0.001, nTimePostConnect * 0.000001);
@@ -2917,7 +2917,7 @@ static void PruneBlockIndexCandidates() {
29172917
* Try to make some progress towards making pindexMostWork the active block.
29182918
* pblock is either NULL or a pointer to a CBlock corresponding to pindexMostWork.
29192919
*/
2920-
static bool ActivateBestChainStep(CValidationState& state, const CChainParams& chainparams, CBlockIndex* pindexMostWork, const CBlock* pblock, bool& fInvalidFound, std::list<CTransaction>& txConflicted, std::vector<std::tuple<CTransaction,CBlockIndex*,int> >& txChanged)
2920+
static bool ActivateBestChainStep(CValidationState& state, const CChainParams& chainparams, CBlockIndex* pindexMostWork, const CBlock* pblock, bool& fInvalidFound, std::list<CTransaction>& txConflicted, std::vector<std::tuple<CTransaction,CBlockIndex*,int>>& txChanged)
29212921
{
29222922
AssertLockHeld(cs_main);
29232923
const CBlockIndex *pindexOldTip = chainActive.Tip();
@@ -3019,14 +3019,17 @@ static void NotifyHeaderTip() {
30193019
bool ActivateBestChain(CValidationState &state, const CChainParams& chainparams, const CBlock *pblock) {
30203020
CBlockIndex *pindexMostWork = NULL;
30213021
CBlockIndex *pindexNewTip = NULL;
3022+
std::vector<std::tuple<CTransaction,CBlockIndex*,int>> txChanged;
3023+
if (pblock)
3024+
txChanged.reserve(pblock->vtx.size());
30223025
do {
3026+
txChanged.clear();
30233027
boost::this_thread::interruption_point();
30243028
if (ShutdownRequested())
30253029
break;
30263030

30273031
const CBlockIndex *pindexFork;
30283032
std::list<CTransaction> txConflicted;
3029-
std::vector<std::tuple<CTransaction,CBlockIndex*,int> > txChanged;
30303033
bool fInitialDownload;
30313034
int nNewHeight;
30323035
{

0 commit comments

Comments
 (0)