Bound depth of hinted scheduler block traversal (#4359)

* Bound depth of hinted scheduler block traversal

* Return early when stopped
This commit is contained in:
Piotr Wójcik 2024-01-15 20:05:06 +01:00 committed by GitHub
commit b6bf11793f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 3 deletions

View file

@ -61,10 +61,14 @@ bool nano::scheduler::hinted::predicate () const
void nano::scheduler::hinted::activate (const nano::store::read_transaction & transaction, const nano::block_hash & hash, bool check_dependents) void nano::scheduler::hinted::activate (const nano::store::read_transaction & transaction, const nano::block_hash & hash, bool check_dependents)
{ {
const int max_iterations = 64;
std::set<nano::block_hash> visited;
std::stack<nano::block_hash> stack; std::stack<nano::block_hash> stack;
stack.push (hash); stack.push (hash);
while (!stack.empty ()) int iterations = 0;
while (!stack.empty () && iterations++ < max_iterations)
{ {
transaction.refresh_if_needed (); transaction.refresh_if_needed ();
@ -91,7 +95,7 @@ void nano::scheduler::hinted::activate (const nano::store::read_transaction & tr
auto dependents = node.ledger.dependent_blocks (transaction, *block); auto dependents = node.ledger.dependent_blocks (transaction, *block);
for (const auto & dependent_hash : dependents) for (const auto & dependent_hash : dependents)
{ {
if (!dependent_hash.is_zero ()) if (!dependent_hash.is_zero () && visited.insert (dependent_hash).second) // Avoid visiting the same block twice
{ {
stack.push (dependent_hash); // Add dependent block to the stack stack.push (dependent_hash); // Add dependent block to the stack
} }
@ -125,6 +129,11 @@ void nano::scheduler::hinted::run_iterative ()
for (auto const & entry : tops) for (auto const & entry : tops)
{ {
if (stopped)
{
return;
}
if (!predicate ()) if (!predicate ())
{ {
return; return;

View file

@ -9,6 +9,7 @@
#include <boost/multi_index/ordered_index.hpp> #include <boost/multi_index/ordered_index.hpp>
#include <boost/multi_index_container.hpp> #include <boost/multi_index_container.hpp>
#include <atomic>
#include <chrono> #include <chrono>
#include <condition_variable> #include <condition_variable>
#include <thread> #include <thread>
@ -79,7 +80,7 @@ private: // Dependencies
private: private:
hinted_config const & config; hinted_config const & config;
bool stopped{ false }; std::atomic<bool> stopped{ false };
nano::condition_variable condition; nano::condition_variable condition;
mutable nano::mutex mutex; mutable nano::mutex mutex;
std::thread thread; std::thread thread;