Skip to content

Commit

Permalink
Convert rep_weights mutex to a shared_mutex since the majority of tim…
Browse files Browse the repository at this point in the history
…e it's being read (#4628)
  • Loading branch information
clemahieu authored May 19, 2024
1 parent a7c8b48 commit ad0573e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
18 changes: 9 additions & 9 deletions nano/secure/rep_weights.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ void nano::rep_weights::representation_add (store::write_transaction const & txn
auto previous_weight{ rep_weight_store.get (txn_a, rep_a) };
auto new_weight = previous_weight + amount_a;
put_store (txn_a, rep_a, previous_weight, new_weight);
nano::lock_guard<nano::mutex> guard (mutex);
std::unique_lock guard{ mutex };
put_cache (rep_a, new_weight);
}

Expand All @@ -28,7 +28,7 @@ void nano::rep_weights::representation_add_dual (store::write_transaction const
auto new_weight_2 = previous_weight_2 + amount_2;
put_store (txn_a, rep_1, previous_weight_1, new_weight_1);
put_store (txn_a, rep_2, previous_weight_2, new_weight_2);
nano::lock_guard<nano::mutex> guard (mutex);
std::unique_lock guard{ mutex };
put_cache (rep_1, new_weight_1);
put_cache (rep_2, new_weight_2);
}
Expand All @@ -40,27 +40,27 @@ void nano::rep_weights::representation_add_dual (store::write_transaction const

void nano::rep_weights::representation_put (nano::account const & account_a, nano::uint128_t const & representation_a)
{
nano::lock_guard<nano::mutex> guard (mutex);
std::unique_lock guard{ mutex };
put_cache (account_a, representation_a);
}

nano::uint128_t nano::rep_weights::representation_get (nano::account const & account_a) const
{
nano::lock_guard<nano::mutex> lk (mutex);
std::shared_lock lk{ mutex };
return get (account_a);
}

/** Makes a copy */
std::unordered_map<nano::account, nano::uint128_t> nano::rep_weights::get_rep_amounts () const
{
nano::lock_guard<nano::mutex> guard (mutex);
std::shared_lock guard{ mutex };
return rep_amounts;
}

void nano::rep_weights::copy_from (nano::rep_weights & other_a)
{
nano::lock_guard<nano::mutex> guard_this (mutex);
nano::lock_guard<nano::mutex> guard_other (other_a.mutex);
std::unique_lock guard_this{ mutex };
std::shared_lock guard_other{ other_a.mutex };
for (auto const & entry : other_a.rep_amounts)
{
auto prev_amount (get (entry.first));
Expand Down Expand Up @@ -122,7 +122,7 @@ nano::uint128_t nano::rep_weights::get (nano::account const & account_a) const

std::size_t nano::rep_weights::size () const
{
nano::lock_guard<nano::mutex> guard (mutex);
std::shared_lock guard{ mutex };
return rep_amounts.size ();
}

Expand All @@ -131,7 +131,7 @@ std::unique_ptr<nano::container_info_component> nano::rep_weights::collect_conta
size_t rep_amounts_count;

{
nano::lock_guard<nano::mutex> guard (mutex);
std::shared_lock guard{ mutex };
rep_amounts_count = rep_amounts.size ();
}
auto sizeof_element = sizeof (decltype (rep_amounts)::value_type);
Expand Down
4 changes: 2 additions & 2 deletions nano/secure/rep_weights.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include <nano/lib/utility.hpp>

#include <memory>
#include <mutex>
#include <shared_mutex>
#include <unordered_map>

namespace nano
Expand Down Expand Up @@ -32,7 +32,7 @@ class rep_weights
std::unique_ptr<container_info_component> collect_container_info (std::string const &) const;

private:
mutable nano::mutex mutex;
mutable std::shared_mutex mutex;
std::unordered_map<nano::account, nano::uint128_t> rep_amounts;
nano::store::rep_weight & rep_weight_store;
nano::uint128_t min_weight;
Expand Down

0 comments on commit ad0573e

Please sign in to comment.