From 93fb0e7897000072ea790a91816aea876718ad27 Mon Sep 17 00:00:00 2001 From: TheCharlatan Date: Sat, 27 Jul 2024 16:36:02 +0200 Subject: [PATCH] kernel: Only setup kernel context globals once The globals setup by the function calls when creating a new kernel context only need to be setup once. Calling them multiple times may be wasteful and has no apparent benefit. Besides kernel users potentially creating multiple contexts, this change may also be useful for tests creating multiple setups. Co-authored-by: stickies-v --- src/kernel/context.cpp | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/kernel/context.cpp b/src/kernel/context.cpp index bfb17915fd..2420d18d74 100644 --- a/src/kernel/context.cpp +++ b/src/kernel/context.cpp @@ -8,15 +8,18 @@ #include #include +#include #include - namespace kernel { Context::Context() { - std::string sha256_algo = SHA256AutoDetect(); - LogPrintf("Using the '%s' SHA256 implementation\n", sha256_algo); - RandomInit(); + static std::once_flag globals_initialized{}; + std::call_once(globals_initialized, []() { + std::string sha256_algo = SHA256AutoDetect(); + LogInfo("Using the '%s' SHA256 implementation\n", sha256_algo); + RandomInit(); + }); }