Skip to content

Commit ada6f2b

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 ada6f2b

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

src/bls/bls.h

Lines changed: 10 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,7 @@ class CBLSLazyWrapper
462463
{
463464
std::unique_lock<std::mutex> l(mutex);
464465
s.read(AsWritableBytes(Span{vecBytes.data(), BLSObject::SerSize}));
465-
bufValid = true;
466+
bufValid = std::any_of(vecBytes.begin(), vecBytes.end(), [](uint8_t c) { return c != 0; });
466467
bufLegacyScheme = specificLegacyScheme;
467468
objInitialized = false;
468469
hash.SetNull();
@@ -507,6 +508,14 @@ class CBLSLazyWrapper
507508

508509
bool operator==(const CBLSLazyWrapper& r) const
509510
{
511+
// If neither bufValid or objInitialized are set, then the object is the default object.
512+
const bool is_default{bufValid || objInitialized};
513+
const bool r_is_default{r.bufValid || r.objInitialized};
514+
// If both are default; they are equal.
515+
if (is_default && r_is_default) return true;
516+
// If one is default and the other isn't, we are not equal
517+
if (is_default != r_is_default) return false;
518+
510519
if (bufValid && r.bufValid && bufLegacyScheme == r.bufLegacyScheme) {
511520
return vecBytes == r.vecBytes;
512521
}

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)