Skip to content

Commit

Permalink
Revert "Fix performance issue in block_uniquer cleanup. (nanocurrency…
Browse files Browse the repository at this point in the history
…#4326)"

This reverts commit 6244918.
  • Loading branch information
clemahieu committed Nov 8, 2023
1 parent 358bf03 commit aca4ce4
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 18 deletions.
15 changes: 8 additions & 7 deletions nano/core_test/block.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@

#include <boost/property_tree/json_parser.hpp>

#include <thread>

#include <crypto/ed25519-donna/ed25519.h>

TEST (ed25519, signing)
Expand Down Expand Up @@ -591,14 +589,17 @@ TEST (block_uniquer, cleanup)
.build_shared ();

nano::block_uniquer uniquer;
auto block3 = uniquer.unique (block1);
auto block4 = uniquer.unique (block2);
auto block3 (uniquer.unique (block1));
auto block4 (uniquer.unique (block2));
block2.reset ();
block4.reset ();
ASSERT_EQ (2, uniquer.size ());
std::this_thread::sleep_for (nano::block_uniquer::cleanup_cutoff);
auto block5 = uniquer.unique (block1);
ASSERT_EQ (1, uniquer.size ());
auto iterations (0);
while (uniquer.size () == 2)
{
auto block5 (uniquer.unique (block1));
ASSERT_LT (iterations++, 200);
}
}

TEST (block_builder, from)
Expand Down
19 changes: 12 additions & 7 deletions nano/lib/blocks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1881,19 +1881,24 @@ std::shared_ptr<nano::block> nano::block_uniquer::unique (std::shared_ptr<nano::
{
existing = block_a;
}
auto now = std::chrono::steady_clock::now ();
if (cleanup_cutoff < now - cleanup_last)
release_assert (std::numeric_limits<CryptoPP::word32>::max () > blocks.size ());
for (auto i (0); i < cleanup_count && !blocks.empty (); ++i)
{
cleanup_last = now;
for (auto i = blocks.begin (), n = blocks.end (); i != n;)
auto random_offset (nano::random_pool::generate_word32 (0, static_cast<CryptoPP::word32> (blocks.size () - 1)));
auto existing (std::next (blocks.begin (), random_offset));
if (existing == blocks.end ())
{
if (auto block_l = i->second.lock ())
existing = blocks.begin ();
}
if (existing != blocks.end ())
{
if (auto block_l = existing->second.lock ())
{
++i;
// Still live
}
else
{
i = blocks.erase (i);
blocks.erase (existing);
}
}
}
Expand Down
5 changes: 1 addition & 4 deletions nano/lib/blocks.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -413,10 +413,7 @@ class block_uniquer
private:
nano::mutex mutex{ mutex_identifier (mutexes::block_uniquer) };
std::unordered_map<std::remove_const_t<value_type::first_type>, value_type::second_type> blocks;
std::chrono::steady_clock::time_point cleanup_last{ std::chrono::steady_clock::now () };

public:
static std::chrono::milliseconds constexpr cleanup_cutoff{ 500 };
static unsigned constexpr cleanup_count = 2;
};

std::unique_ptr<container_info_component> collect_container_info (block_uniquer & block_uniquer, std::string const & name);
Expand Down

0 comments on commit aca4ce4

Please sign in to comment.