diff --git a/CMakeLists.txt b/CMakeLists.txt index b79d2fc4f7..a51d8e919c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -35,6 +35,7 @@ set(CMAKE_EXPORT_COMPILE_COMMANDS ON) # cmake-format: off option(TESTING "Build and run test suite" ON ) +option(BENCHMARK "Build benchmarks" OFF) option(CLANG_FORMAT "Enable clang-format target" ON ) option(CLANG_TIDY "Enable clang-tidy checks during compilation" OFF) option(COVERAGE "Enable generation of coverage info" OFF) @@ -194,4 +195,8 @@ if(TESTING) add_subdirectory(test) endif() +if(BENCHMARK) + add_subdirectory(benchmarks) +endif() + add_subdirectory(node) diff --git a/benchmarks/CMakeLists.txt b/benchmarks/CMakeLists.txt new file mode 100644 index 0000000000..d5ce32bd85 --- /dev/null +++ b/benchmarks/CMakeLists.txt @@ -0,0 +1,3 @@ + +add_executable(trie_pruner_benchmark trie_pruner_benchmark.cpp) +target_link_libraries(trie_pruner_benchmark benchmark::benchmark storage) \ No newline at end of file diff --git a/benchmarks/trie_pruner_benchmark.cpp b/benchmarks/trie_pruner_benchmark.cpp new file mode 100644 index 0000000000..b1d82e330b --- /dev/null +++ b/benchmarks/trie_pruner_benchmark.cpp @@ -0,0 +1,23 @@ +/** +* Copyright Soramitsu Co., Ltd. All Rights Reserved. +* SPDX-License-Identifier: Apache-2.0 +*/ + +#include + +static void BM_StringCreation(benchmark::State& state) { + for (auto _ : state) + std::string empty_string; +} +// Register the function as a benchmark +BENCHMARK(BM_StringCreation); + +// Define another benchmark +static void BM_StringCopy(benchmark::State& state) { + std::string x = "hello"; + for (auto _ : state) + std::string copy(x); +} +BENCHMARK(BM_StringCopy); + +BENCHMARK_MAIN(); \ No newline at end of file diff --git a/core/consensus/babe/impl/babe_config_repository_impl.cpp b/core/consensus/babe/impl/babe_config_repository_impl.cpp index c617fa7af3..ecade0a23e 100644 --- a/core/consensus/babe/impl/babe_config_repository_impl.cpp +++ b/core/consensus/babe/impl/babe_config_repository_impl.cpp @@ -93,19 +93,14 @@ namespace kagome::consensus::babe { } bool BabeConfigRepositoryImpl::prepare() { + std::unique_lock lock{indexer_mutex_}; auto finalized = block_tree_->getLastFinalized(); auto finalized_header = block_tree_->getBlockHeader(finalized.hash).value(); - primitives::BlockNumber indexer_last_finalized; - { - std::unique_lock lock{indexer_mutex_}; - indexer_last_finalized = indexer_.last_finalized_indexed_.number; - } - - if (finalized.number - indexer_last_finalized + if (finalized.number - indexer_.last_finalized_indexed_.number > kMaxUnindexedBlocksNum and trie_storage_->getEphemeralBatchAt(finalized_header.state_root)) { - warp(finalized); + warp(lock, finalized); } auto genesis_res = config({block_tree_->getGenesisBlockHash(), 0}, false); @@ -243,9 +238,14 @@ namespace kagome::consensus::babe { return 0; } + void BabeConfigRepositoryImpl::warp(std::unique_lock &lock, + const primitives::BlockInfo &block) { + indexer_.put(block, {}, true); + } + void BabeConfigRepositoryImpl::warp(const primitives::BlockInfo &block) { std::unique_lock lock{indexer_mutex_}; - indexer_.put(block, {}, true); + warp(lock, block); } outcome::result> diff --git a/core/consensus/babe/impl/babe_config_repository_impl.hpp b/core/consensus/babe/impl/babe_config_repository_impl.hpp index 72ea518548..345a8122a3 100644 --- a/core/consensus/babe/impl/babe_config_repository_impl.hpp +++ b/core/consensus/babe/impl/babe_config_repository_impl.hpp @@ -52,7 +52,7 @@ namespace kagome::consensus::babe { std::optional> state; /** * Next epoch lazily computed from `config` and digests. - */ + */ std::optional> next_state; }; @@ -125,6 +125,9 @@ namespace kagome::consensus::babe { outcome::result> loadPrev(const std::optional &prev) const; + void warp(std::unique_lock &lock, + const primitives::BlockInfo &block); + std::shared_ptr persistent_storage_; bool config_warp_sync_; std::shared_ptr block_tree_;