Skip to content

Commit

Permalink
GH-3 Use unordered_map
Browse files Browse the repository at this point in the history
  • Loading branch information
heifner committed Apr 18, 2024
1 parent 4a759b3 commit 51503c4
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
13 changes: 7 additions & 6 deletions libraries/chain/include/eosio/chain/vote_processor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,16 @@
#include <boost/multi_index/mem_fun.hpp>
#include <boost/multi_index/ordered_index.hpp>

#include <unordered_map>

namespace eosio::chain {

/**
* Process votes in a dedicated thread pool.
*/
class vote_processor_t {
// Even 3000 vote structs are less than 1MB per connection.
// 2500 is should never be reached unless a specific connection is sending garbage.
// 2500 should never be reached unless a specific connection is sending garbage.
static constexpr size_t max_votes_per_connection = 2500;
// If we have not processed a vote in this amount of time, give up on it.
static constexpr fc::microseconds too_old = fc::seconds(5);
Expand Down Expand Up @@ -51,8 +53,8 @@ class vote_processor_t {
std::mutex mtx;
vote_index_type index;
block_state_ptr last_bsp;
// connection, count of messages
std::map<uint32_t, uint16_t> num_messages;
// connection, count of messages
std::unordered_map<uint32_t, uint16_t> num_messages;

std::atomic<block_num_type> lib{0};
std::atomic<block_num_type> largest_known_block_num{0};
Expand Down Expand Up @@ -99,19 +101,18 @@ class vote_processor_t {
index.insert(vote{.connection_id = connection_id, .received = now, .msg = msg});
}

// called with locked mtx
// called with locked mtx, returns with a locked mutex
void process_any_queued_for_later(std::unique_lock<std::mutex>& g) {
if (index.empty())
return;
remove_too_old();
remove_before_lib();
auto& idx = index.get<by_last_received>();
std::vector<vote> unprocessed;
unprocessed.reserve(std::min<size_t>(21u, idx.size())); // maybe increase if we increase # of finalizers from 21
for (auto i = idx.begin(); i != idx.end();) {
if (stopped)
return;
vote v = *i;
vote v = std::move(*i);
idx.erase(i);
auto bsp = get_block(v.msg->block_id, g);
// g is unlocked
Expand Down
2 changes: 1 addition & 1 deletion plugins/net_plugin/net_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,7 @@ namespace eosio {

void on_accepted_block_header( const signed_block_ptr& block, const block_id_type& id );
void on_accepted_block();
void on_voted_block ( uint32_t connection_id, vote_status stauts, const vote_message_ptr& vote );
void on_voted_block( uint32_t connection_id, vote_status stauts, const vote_message_ptr& vote );

void transaction_ack(const std::pair<fc::exception_ptr, packed_transaction_ptr>&);
void on_irreversible_block( const block_id_type& id, uint32_t block_num );
Expand Down

0 comments on commit 51503c4

Please sign in to comment.