Skip to content

Commit 40909d7

Browse files
perf: speedup of CBLSLazyPublicKey::operator== when comparing to the default / null object; speedup CDeterministicMNList::AddMN by avoiding check to IsValid when a nullcheck is sufficient
1 parent d9704d3 commit 40909d7

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

src/bls/bls.h

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,7 @@ class CBLSLazyWrapper
391391
mutable std::mutex mutex;
392392

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

@@ -462,7 +463,11 @@ class CBLSLazyWrapper
462463
{
463464
std::unique_lock<std::mutex> l(mutex);
464465
s.read(AsWritableBytes(Span{vecBytes.data(), BLSObject::SerSize}));
465-
bufValid = true;
466+
if (std::any_of(vecBytes.begin(), vecBytes.end(), [](uint8_t c) { return c != 0; })) {
467+
bufValid = true;
468+
} else {
469+
bufValid = false;
470+
}
466471
bufLegacyScheme = specificLegacyScheme;
467472
objInitialized = false;
468473
hash.SetNull();
@@ -507,6 +512,19 @@ class CBLSLazyWrapper
507512

508513
bool operator==(const CBLSLazyWrapper& r) const
509514
{
515+
// If neither bufValid or objInitialized are set, then the object is the default object.
516+
// If both are default; they are equal.
517+
if ((!r.bufValid && !r.objInitialized) && (!bufValid && !objInitialized)) {
518+
return true;
519+
}
520+
// If one is default and the other isn't, we are not equal
521+
if ((bufValid || objInitialized) && (!r.bufValid && !r.objInitialized)) {
522+
return false;
523+
}
524+
if ((!bufValid && !objInitialized) && (r.bufValid || r.objInitialized)) {
525+
return false;
526+
}
527+
510528
if (bufValid && r.bufValid && bufLegacyScheme == r.bufLegacyScheme) {
511529
return vecBytes == r.vecBytes;
512530
}

src/evo/deterministicmns.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,7 @@ void CDeterministicMNList::AddMN(const CDeterministicMNCPtr& dmn, bool fBumpTota
477477
throw(std::runtime_error(strprintf("%s: Can't add a masternode %s with a duplicate keyIDOwner=%s", __func__,
478478
dmn->proTxHash.ToString(), EncodeDestination(PKHash(dmn->pdmnState->keyIDOwner)))));
479479
}
480-
if (dmn->pdmnState->pubKeyOperator.Get().IsValid() && !AddUniqueProperty(*dmn, dmn->pdmnState->pubKeyOperator)) {
480+
if (dmn->pdmnState->pubKeyOperator != CBLSLazyPublicKey() && !AddUniqueProperty(*dmn, dmn->pdmnState->pubKeyOperator)) {
481481
mnUniquePropertyMap = mnUniquePropertyMapSaved;
482482
throw(std::runtime_error(strprintf("%s: Can't add a masternode %s with a duplicate pubKeyOperator=%s", __func__,
483483
dmn->proTxHash.ToString(), dmn->pdmnState->pubKeyOperator.ToString())));

0 commit comments

Comments
 (0)