-
Notifications
You must be signed in to change notification settings - Fork 1.2k
perf: avoid heavy object copy during ApplyDiff for CDeterministicMNList #6680
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -423,31 +423,28 @@ CDeterministicMNListDiff CDeterministicMNList::BuildDiff(const CDeterministicMNL | |
| return diffRet; | ||
| } | ||
|
|
||
| CDeterministicMNList CDeterministicMNList::ApplyDiff(gsl::not_null<const CBlockIndex*> pindex, const CDeterministicMNListDiff& diff) const | ||
| void CDeterministicMNList::ApplyDiff(gsl::not_null<const CBlockIndex*> pindex, const CDeterministicMNListDiff& diff) | ||
| { | ||
| CDeterministicMNList result = *this; | ||
| result.blockHash = pindex->GetBlockHash(); | ||
| result.nHeight = pindex->nHeight; | ||
| blockHash = pindex->GetBlockHash(); | ||
| nHeight = pindex->nHeight; | ||
|
|
||
| for (const auto& id : diff.removedMns) { | ||
| auto dmn = result.GetMNByInternalId(id); | ||
| auto dmn = GetMNByInternalId(id); | ||
| if (!dmn) { | ||
| throw std::runtime_error(strprintf("%s: can't find a removed masternode, id=%d", __func__, id)); | ||
| } | ||
| result.RemoveMN(dmn->proTxHash); | ||
| RemoveMN(dmn->proTxHash); | ||
| } | ||
| for (const auto& dmn : diff.addedMNs) { | ||
| result.AddMN(dmn); | ||
| AddMN(dmn); | ||
| } | ||
| for (const auto& p : diff.updatedMNs) { | ||
| auto dmn = result.GetMNByInternalId(p.first); | ||
| auto dmn = GetMNByInternalId(p.first); | ||
| if (!dmn) { | ||
| throw std::runtime_error(strprintf("%s: can't find an updated masternode, id=%d", __func__, p.first)); | ||
| } | ||
| result.UpdateMN(*dmn, p.second); | ||
| UpdateMN(*dmn, p.second); | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
| void CDeterministicMNList::AddMN(const CDeterministicMNCPtr& dmn, bool fBumpTotalCount) | ||
|
|
@@ -708,7 +705,8 @@ bool CDeterministicMNManager::UndoBlock(gsl::not_null<const CBlockIndex*> pindex | |
| mnListDiffsCache.erase(blockHash); | ||
| } | ||
| if (diff.HasChanges()) { | ||
| CDeterministicMNList curList{prevList.ApplyDiff(pindex, diff)}; | ||
| CDeterministicMNList curList{prevList}; | ||
| curList.ApplyDiff(pindex, diff); | ||
|
|
||
| auto inversedDiff{curList.BuildDiff(prevList)}; | ||
| updatesRet = {curList, prevList, inversedDiff}; | ||
|
|
@@ -1096,12 +1094,7 @@ CDeterministicMNList CDeterministicMNManager::GetListForBlockInternal(gsl::not_n | |
|
|
||
| for (const auto& diffIndex : listDiffIndexes) { | ||
| const auto& diff = mnListDiffsCache.at(diffIndex->GetBlockHash()); | ||
| if (diff.HasChanges()) { | ||
| snapshot = snapshot.ApplyDiff(diffIndex, diff); | ||
| } else { | ||
| snapshot.SetBlockHash(diffIndex->GetBlockHash()); | ||
| snapshot.SetHeight(diffIndex->nHeight); | ||
| } | ||
| snapshot.ApplyDiff(diffIndex, diff); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Am I correct that this is the only place we have a performance improvement right? All other instances, just now have an explicit copy instead of implicit copy. This change does seem worth it, but only this specific one in GetListForBlockInternal has any perf improvement
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not counting unit tests, there are only 2 usages of |
||
| } | ||
|
|
||
| if (tipIndex) { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Add consistency guards inside
ApplyDiffThe in-place variant looks solid, but a few inexpensive assertions would harden it even more:
assert(diff.nHeight == pindex->nHeight);– Helps detect accidental mismatches when we pass the wrong diff/pindex pair.
Early exit when the diff is empty to skip hash/height updates in the no-op case (micro-optimisation but costs nothing).
void CDeterministicMNList::ApplyDiff(gsl::not_null<const CBlockIndex*> pindex, const CDeterministicMNListDiff& diff) { + // Sanity: the diff we’re applying must belong to the same block. + assert(diff.nHeight == -1 || diff.nHeight == pindex->nHeight); + if (!diff.HasChanges()) { + blockHash = pindex->GetBlockHash(); + nHeight = pindex->nHeight; + return; + }Those checks catch logical errors early without any runtime penalty in production builds (asserts are stripped in
-DNDEBUG).📝 Committable suggestion
🤖 Prompt for AI Agents