From 8ed1a01aedcaffdc19b746a37f0e559c9bf26aad Mon Sep 17 00:00:00 2001 From: evoskuil Date: Wed, 21 Aug 2024 14:19:54 -0400 Subject: [PATCH] Enable block deserialization disable via config. --- include/bitcoin/node/block_memory.hpp | 8 +++----- src/block_memory.cpp | 27 ++++++++------------------- src/parser.cpp | 2 +- 3 files changed, 12 insertions(+), 25 deletions(-) diff --git a/include/bitcoin/node/block_memory.hpp b/include/bitcoin/node/block_memory.hpp index 98178b58..310d6e43 100644 --- a/include/bitcoin/node/block_memory.hpp +++ b/include/bitcoin/node/block_memory.hpp @@ -35,20 +35,18 @@ class BCN_API block_memory final DELETE_COPY_MOVE_DESTRUCT(block_memory); /// Per thread multiple of wire size for each linear allocation chunk. + /// Returns default_arena if multiple is zero or threads exceeded. block_memory(size_t multiple, size_t threads) NOEXCEPT; /// Each thread obtains an arena. arena* get_arena() NOEXCEPT override; -protected: - block_arena* get_block_arena() const THROWS; - private: // This is thread safe. - mutable std::atomic_size_t count_; + std::atomic_size_t count_{}; // This is protected by constructor init and thread_local indexation. - mutable std::vector arenas_; + std::vector arenas_{}; }; } // namespace node diff --git a/src/block_memory.cpp b/src/block_memory.cpp index b1e7b203..d63f878a 100644 --- a/src/block_memory.cpp +++ b/src/block_memory.cpp @@ -28,30 +28,19 @@ namespace node { BC_PUSH_WARNING(NO_THROW_IN_NOEXCEPT) block_memory::block_memory(size_t multiple, size_t threads) NOEXCEPT - : count_{}, arenas_{} { - arenas_.reserve(threads); - for (auto index = zero; index < threads; ++index) - arenas_.emplace_back(multiple); + if (is_nonzero(multiple)) + { + arenas_.reserve(threads); + for (auto index = zero; index < threads; ++index) + arenas_.emplace_back(multiple); + } } arena* block_memory::get_arena() NOEXCEPT { - return get_block_arena(); -} - -// protected -block_arena* block_memory::get_block_arena() const THROWS -{ - static thread_local auto index = count_.fetch_add(one, - std::memory_order_relaxed); - - // More threads are requesting an arena than specified at construct. - ////BC_ASSERT(index < arenas_.size()); - if (index >= arenas_.size()) - throw allocation_exception{}; - - return &arenas_.at(index); + thread_local auto thread = count_.fetch_add(one, std::memory_order_relaxed); + return thread < arenas_.size() ? &arenas_.at(thread) : default_arena::get(); } BC_POP_WARNING() diff --git a/src/parser.cpp b/src/parser.cpp index ee0874fb..c4a6397b 100644 --- a/src/parser.cpp +++ b/src/parser.cpp @@ -900,7 +900,7 @@ options_metadata parser::load_settings() THROWS ( "node.allocation_multiple", value(&configured.node.allocation_multiple), - "Per thread block deserialization buffer multiple of wire size, defaults to 20." + "Block deserialization buffer multiple of wire size, defaults to 20 (0 disables)." ) ( "node.maximum_height",