Skip to content

Commit

Permalink
Optimize loading block index guts if low on memory (Linux only) (#1433)
Browse files Browse the repository at this point in the history
  • Loading branch information
psolstice authored Apr 27, 2024
1 parent 95622c6 commit 8819b89
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/txdb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@

#include <boost/thread.hpp>

#ifdef __linux__
#include <sys/sysinfo.h>
#endif

static const char DB_COIN = 'C';
static const char DB_COINS = 'c';
static const char DB_BLOCK_FILES = 'f';
Expand Down Expand Up @@ -365,6 +369,14 @@ bool CBlockTreeDB::LoadBlockIndexGuts(boost::function<CBlockIndex*(const uint256
int firstInLastNBlocksHeight = 0;

bool fCheckPoWForAllBlocks = GetBoolArg("-fullblockindexcheck", DEFAULT_FULL_BLOCKINDEX_CHECK);
int64_t nBlocksToCheck = GetArg("-numberofblockstocheckonstartup", DEFAULT_BLOCKINDEX_NUMBER_OF_BLOCKS_TO_CHECK);

#ifdef __linux__
struct sysinfo sysInfo;

if (sysinfo(&sysInfo) == 0 && sysInfo.freeram < 2ul*1024ul*1024ul*1024ul)
nBlocksToCheck = DEFAULT_BLOCKINDEX_LOWMEM_NUMBER_OF_BLOCKS_TO_CHECK;
#endif

while (pcursor->Valid()) {
boost::this_thread::interruption_point();
Expand Down Expand Up @@ -423,7 +435,7 @@ bool CBlockTreeDB::LoadBlockIndexGuts(boost::function<CBlockIndex*(const uint256
else {
if (pindexNew->nHeight >= firstInLastNBlocksHeight) {
lastNBlocks.insert(std::pair<int, CBlockIndex*>(pindexNew->nHeight, pindexNew));
if (lastNBlocks.size() > DEFAULT_BLOCKINDEX_NUMBER_OF_BLOCKS_TO_CHECK) {
if (lastNBlocks.size() > nBlocksToCheck) {
// pop the first element from the map
auto firstElement = lastNBlocks.begin();
auto elementToPop = firstElement++;
Expand Down
2 changes: 2 additions & 0 deletions src/txdb.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ static const int64_t nMaxCoinsDBCache = 8;
static const bool DEFAULT_FULL_BLOCKINDEX_CHECK = false;
//! If not doing full check of block index, check only N of the latest blocks
static const int DEFAULT_BLOCKINDEX_NUMBER_OF_BLOCKS_TO_CHECK = 10000;
//! Check fewer blocks if low on memory
static const int DEFAULT_BLOCKINDEX_LOWMEM_NUMBER_OF_BLOCKS_TO_CHECK = 50;

struct CDiskTxPos : public CDiskBlockPos
{
Expand Down

0 comments on commit 8819b89

Please sign in to comment.