Skip to content

Commit

Permalink
perf: speedup of CBLSLazyPublicKey::operator== when comparing to the …
Browse files Browse the repository at this point in the history
…default / null object; speedup CDeterministicMNList::AddMN by avoiding check to IsValid when a nullcheck is sufficient
  • Loading branch information
PastaPastaPasta committed Feb 17, 2025
1 parent d9704d3 commit ada6f2b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
11 changes: 10 additions & 1 deletion src/bls/bls.h
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,7 @@ class CBLSLazyWrapper
mutable std::mutex mutex;

mutable std::array<uint8_t, BLSObject::SerSize> vecBytes;
// Indicates if the value contained in vecBytes is valid
mutable bool bufValid{false};
mutable bool bufLegacyScheme{true};

Expand Down Expand Up @@ -462,7 +463,7 @@ class CBLSLazyWrapper
{
std::unique_lock<std::mutex> l(mutex);
s.read(AsWritableBytes(Span{vecBytes.data(), BLSObject::SerSize}));
bufValid = true;
bufValid = std::any_of(vecBytes.begin(), vecBytes.end(), [](uint8_t c) { return c != 0; });
bufLegacyScheme = specificLegacyScheme;
objInitialized = false;
hash.SetNull();
Expand Down Expand Up @@ -507,6 +508,14 @@ class CBLSLazyWrapper

bool operator==(const CBLSLazyWrapper& r) const
{
// If neither bufValid or objInitialized are set, then the object is the default object.
const bool is_default{bufValid || objInitialized};
const bool r_is_default{r.bufValid || r.objInitialized};
// If both are default; they are equal.
if (is_default && r_is_default) return true;
// If one is default and the other isn't, we are not equal
if (is_default != r_is_default) return false;

if (bufValid && r.bufValid && bufLegacyScheme == r.bufLegacyScheme) {
return vecBytes == r.vecBytes;
}
Expand Down
2 changes: 1 addition & 1 deletion src/evo/deterministicmns.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,7 @@ void CDeterministicMNList::AddMN(const CDeterministicMNCPtr& dmn, bool fBumpTota
throw(std::runtime_error(strprintf("%s: Can't add a masternode %s with a duplicate keyIDOwner=%s", __func__,
dmn->proTxHash.ToString(), EncodeDestination(PKHash(dmn->pdmnState->keyIDOwner)))));
}
if (dmn->pdmnState->pubKeyOperator.Get().IsValid() && !AddUniqueProperty(*dmn, dmn->pdmnState->pubKeyOperator)) {
if (dmn->pdmnState->pubKeyOperator != CBLSLazyPublicKey() && !AddUniqueProperty(*dmn, dmn->pdmnState->pubKeyOperator)) {
mnUniquePropertyMap = mnUniquePropertyMapSaved;
throw(std::runtime_error(strprintf("%s: Can't add a masternode %s with a duplicate pubKeyOperator=%s", __func__,
dmn->proTxHash.ToString(), dmn->pdmnState->pubKeyOperator.ToString())));
Expand Down

0 comments on commit ada6f2b

Please sign in to comment.