Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Address performance regression in vote_cache::top #4627

Merged
merged 3 commits into from
May 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions nano/node/vote_cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -231,11 +231,11 @@ void nano::vote_cache::clear ()
cache.clear ();
}

std::vector<nano::vote_cache::top_entry> nano::vote_cache::top (const nano::uint128_t & min_tally)
std::deque<nano::vote_cache::top_entry> nano::vote_cache::top (const nano::uint128_t & min_tally)
{
stats.inc (nano::stat::type::vote_cache, nano::stat::detail::top);

std::vector<top_entry> results;
std::deque<top_entry> results;
{
nano::lock_guard<nano::mutex> lock{ mutex };

Expand All @@ -244,12 +244,14 @@ std::vector<nano::vote_cache::top_entry> nano::vote_cache::top (const nano::uint
cleanup ();
}

for (auto & entry : cache)
for (auto & entry : cache.get<tag_tally> ())
{
if (entry.tally () >= min_tally)
auto tally = entry.tally ();
if (tally < min_tally)
{
results.push_back ({ entry.hash (), entry.tally (), entry.final_tally () });
break;
}
results.push_back ({ entry.hash (), tally, entry.final_tally () });
}
}

Expand Down
7 changes: 5 additions & 2 deletions nano/node/vote_cache.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ class vote_cache final
* The blocks are sorted in descending order by final tally, then by tally
* @param min_tally minimum tally threshold, entries below with their voting weight below this will be ignored
*/
std::vector<top_entry> top (nano::uint128_t const & min_tally);
std::deque<top_entry> top (nano::uint128_t const & min_tally);

public: // Container info
std::unique_ptr<nano::container_info_component> collect_container_info (std::string const & name) const;
Expand All @@ -166,14 +166,17 @@ class vote_cache final
// clang-format off
class tag_sequenced {};
class tag_hash {};
class tag_tally {};
// clang-format on

// clang-format off
using ordered_cache = boost::multi_index_container<entry,
mi::indexed_by<
mi::hashed_unique<mi::tag<tag_hash>,
mi::const_mem_fun<entry, nano::block_hash, &entry::hash>>,
mi::sequenced<mi::tag<tag_sequenced>>
mi::sequenced<mi::tag<tag_sequenced>>,
mi::ordered_non_unique<mi::tag<tag_tally>,
mi::const_mem_fun<entry, nano::uint128_t, &entry::tally>, std::greater<>> // DESC
>>;
// clang-format on
ordered_cache cache;
Expand Down
Loading