Skip to content

Commit 08dbc6e

Browse files
committed
cuckoocache: Return approximate memory size
Returning the approximate total size eliminates the need for InitS*Cache() to do nElems*sizeof(uint256). The cuckoocache has a better idea of this information.
1 parent 0dbce4b commit 08dbc6e

File tree

8 files changed

+33
-20
lines changed

8 files changed

+33
-20
lines changed

src/bitcoin-chainstate.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ int main(int argc, char* argv[])
6262
// Necessary for CheckInputScripts (eventually called by ProcessNewBlock),
6363
// which will try the script cache first and fall back to actually
6464
// performing the check with the signature cache.
65-
InitSignatureCache();
66-
InitScriptExecutionCache();
65+
Assert(InitSignatureCache());
66+
Assert(InitScriptExecutionCache());
6767

6868

6969
// SETUP: Scheduling and Background Signals

src/cuckoocache.h

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -336,8 +336,8 @@ class cache
336336
uint32_t setup(uint32_t new_size)
337337
{
338338
// depth_limit must be at least one otherwise errors can occur.
339-
depth_limit = static_cast<uint8_t>(std::log2(static_cast<float>(std::max((uint32_t)2, new_size))));
340339
size = std::max<uint32_t>(2, new_size);
340+
depth_limit = static_cast<uint8_t>(std::log2(static_cast<float>(size)));
341341
table.resize(size);
342342
collection_flags.setup(size);
343343
epoch_flags.resize(size);
@@ -357,12 +357,16 @@ class cache
357357
*
358358
* @param bytes the approximate number of bytes to use for this data
359359
* structure
360-
* @returns the maximum number of elements storable (see setup()
361-
* documentation for more detail)
360+
* @returns A pair of the maximum number of elements storable (see setup()
361+
* documentation for more detail) and the approxmiate total size of these
362+
* elements in bytes.
362363
*/
363-
uint32_t setup_bytes(size_t bytes)
364+
std::pair<uint32_t, size_t> setup_bytes(size_t bytes)
364365
{
365-
return setup(bytes/sizeof(Element));
366+
auto num_elems = setup(bytes/sizeof(Element));
367+
368+
size_t approx_size_bytes = num_elems * sizeof(Element);
369+
return std::make_pair(num_elems, approx_size_bytes);
366370
}
367371

368372
/** insert loops at most depth_limit times trying to insert a hash

src/init.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1115,8 +1115,9 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
11151115
args.GetArg("-datadir", ""), fs::PathToString(fs::current_path()));
11161116
}
11171117

1118-
InitSignatureCache();
1119-
InitScriptExecutionCache();
1118+
if (!InitSignatureCache() || !InitScriptExecutionCache()) {
1119+
return InitError(strprintf(_("Unable to allocate memory for -maxsigcachesize: '%s' MiB"), args.GetIntArg("-maxsigcachesize", DEFAULT_MAX_SIG_CACHE_SIZE)));
1120+
}
11201121

11211122
int script_threads = args.GetIntArg("-par", DEFAULT_SCRIPTCHECK_THREADS);
11221123
if (script_threads <= 0) {

src/script/sigcache.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ class CSignatureCache
7575
std::unique_lock<std::shared_mutex> lock(cs_sigcache);
7676
setValid.insert(entry);
7777
}
78-
uint32_t setup_bytes(size_t n)
78+
std::pair<uint32_t, size_t> setup_bytes(size_t n)
7979
{
8080
return setValid.setup_bytes(n);
8181
}
@@ -92,14 +92,18 @@ static CSignatureCache signatureCache;
9292

9393
// To be called once in AppInitMain/BasicTestingSetup to initialize the
9494
// signatureCache.
95-
void InitSignatureCache()
95+
bool InitSignatureCache()
9696
{
9797
// nMaxCacheSize is unsigned. If -maxsigcachesize is set to zero,
9898
// setup_bytes creates the minimum possible cache (2 elements).
9999
size_t nMaxCacheSize = std::min(std::max((int64_t)0, gArgs.GetIntArg("-maxsigcachesize", DEFAULT_MAX_SIG_CACHE_SIZE) / 2), MAX_MAX_SIG_CACHE_SIZE) * ((size_t) 1 << 20);
100-
size_t nElems = signatureCache.setup_bytes(nMaxCacheSize);
100+
101+
auto setup_results = signatureCache.setup_bytes(nMaxCacheSize);
102+
103+
const auto [num_elems, approx_size_bytes] = setup_results;
101104
LogPrintf("Using %zu MiB out of %zu/2 requested for signature cache, able to store %zu elements\n",
102-
(nElems*sizeof(uint256)) >>20, (nMaxCacheSize*2)>>20, nElems);
105+
approx_size_bytes >> 20, (nMaxCacheSize * 2) >> 20, num_elems);
106+
return true;
103107
}
104108

105109
bool CachingTransactionSignatureChecker::VerifyECDSASignature(const std::vector<unsigned char>& vchSig, const CPubKey& pubkey, const uint256& sighash) const

src/script/sigcache.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,6 @@ class CachingTransactionSignatureChecker : public TransactionSignatureChecker
3333
bool VerifySchnorrSignature(Span<const unsigned char> sig, const XOnlyPubKey& pubkey, const uint256& sighash) const override;
3434
};
3535

36-
void InitSignatureCache();
36+
[[nodiscard]] bool InitSignatureCache();
3737

3838
#endif // BITCOIN_SCRIPT_SIGCACHE_H

src/test/util/setup_common.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,8 @@ BasicTestingSetup::BasicTestingSetup(const std::string& chainName, const std::ve
133133
m_node.kernel = std::make_unique<kernel::Context>();
134134
SetupEnvironment();
135135
SetupNetworking();
136-
InitSignatureCache();
137-
InitScriptExecutionCache();
136+
Assert(InitSignatureCache());
137+
Assert(InitScriptExecutionCache());
138138
m_node.chain = interfaces::MakeChain(m_node);
139139
fCheckBlockIndex = true;
140140
static bool noui_connected = false;

src/validation.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1656,7 +1656,7 @@ bool CScriptCheck::operator()() {
16561656
static CuckooCache::cache<uint256, SignatureCacheHasher> g_scriptExecutionCache;
16571657
static CSHA256 g_scriptExecutionCacheHasher;
16581658

1659-
void InitScriptExecutionCache() {
1659+
bool InitScriptExecutionCache() {
16601660
// Setup the salted hasher
16611661
uint256 nonce = GetRandHash();
16621662
// We want the nonce to be 64 bytes long to force the hasher to process
@@ -1667,9 +1667,13 @@ void InitScriptExecutionCache() {
16671667
// nMaxCacheSize is unsigned. If -maxsigcachesize is set to zero,
16681668
// setup_bytes creates the minimum possible cache (2 elements).
16691669
size_t nMaxCacheSize = std::min(std::max((int64_t)0, gArgs.GetIntArg("-maxsigcachesize", DEFAULT_MAX_SIG_CACHE_SIZE) / 2), MAX_MAX_SIG_CACHE_SIZE) * ((size_t) 1 << 20);
1670-
size_t nElems = g_scriptExecutionCache.setup_bytes(nMaxCacheSize);
1670+
1671+
auto setup_results = g_scriptExecutionCache.setup_bytes(nMaxCacheSize);
1672+
1673+
const auto [num_elems, approx_size_bytes] = setup_results;
16711674
LogPrintf("Using %zu MiB out of %zu/2 requested for script execution cache, able to store %zu elements\n",
1672-
(nElems*sizeof(uint256)) >>20, (nMaxCacheSize*2)>>20, nElems);
1675+
approx_size_bytes >> 20, (nMaxCacheSize * 2) >> 20, num_elems);
1676+
return true;
16731677
}
16741678

16751679
/**

src/validation.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ class CScriptCheck
323323
};
324324

325325
/** Initializes the script-execution cache */
326-
void InitScriptExecutionCache();
326+
[[nodiscard]] bool InitScriptExecutionCache();
327327

328328
/** Functions for validating blocks and updating the block tree */
329329

0 commit comments

Comments
 (0)