Skip to content

Commit

Permalink
GH-376 Avoid cache line false sharing
Browse files Browse the repository at this point in the history
  • Loading branch information
heifner committed Jul 23, 2024
1 parent 4360347 commit 789e322
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
8 changes: 4 additions & 4 deletions libraries/chain/finality/qc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,25 +108,25 @@ bool open_qc_sig_t::has_voted(bool strong, size_t index) const {
}

void open_qc_sig_t::votes_t::reflector_init() {
processed = std::vector<std::atomic<bool>>(bitset.size());
processed = std::vector<bit_processed>(bitset.size());
for (size_t i = 0; i < bitset.size(); ++i) {
if (bitset[i]) {
processed[i].store(true, std::memory_order_relaxed);
processed[i].value.store(true, std::memory_order_relaxed);
}
}
}

bool open_qc_sig_t::votes_t::has_voted(size_t index) const {
assert(index < processed.size());
return processed[index].load(std::memory_order_relaxed);
return processed[index].value.load(std::memory_order_relaxed);
}


vote_status open_qc_sig_t::votes_t::add_vote(size_t index, const bls_signature& signature) {
if (bitset[index]) { // check here as could have come in while unlocked
return vote_status::duplicate; // shouldn't be already present
}
processed[index].store(true, std::memory_order_relaxed);
processed[index].value.store(true, std::memory_order_relaxed);
bitset.set(index);
sig.aggregate(signature); // works even if _sig is default initialized (fp2::zero())
return vote_status::success;
Expand Down
7 changes: 6 additions & 1 deletion libraries/chain/include/eosio/chain/finality/qc.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <eosio/chain/finality/finalizer_policy.hpp>
#include <eosio/chain/finality/vote_message.hpp>
#include <eosio/chain/block_timestamp.hpp>
#include <eosio/chain/thread_utils.hpp>
#include <fc/crypto/bls_private_key.hpp>
#include <fc/crypto/bls_public_key.hpp>
#include <fc/crypto/bls_signature.hpp>
Expand Down Expand Up @@ -113,7 +114,11 @@ namespace eosio::chain {

vote_bitset bitset;
bls_aggregate_signature sig;
std::vector<std::atomic<bool>> processed; // avoid locking mutex for _bitset duplicate check
struct bit_processed {
alignas(hardware_destructive_interference_size)
std::atomic<bool> value;
};
std::vector<bit_processed> processed; // avoid locking mutex for _bitset duplicate check

void reflector_init();
public:
Expand Down

0 comments on commit 789e322

Please sign in to comment.