Skip to content

Conversation

@knst
Copy link
Collaborator

@knst knst commented Jun 24, 2025

Issue being fixed or feature implemented

Each call of CDeterministicMNManager::GetListForBlock for historical block can require up to 575 diff applying from snapshot.
Consequent calls for close blocks repeat this calculation twice.

What was done?

This PR abuses mnListsCache by adding mini-snapshot each 32 blocks between snapshots in database (each 576 blocks).

Downside of this solution: extra RAM usage.
Though, this cache is cleaned every 10 seconds by CDeterministicMNManager::DoMaintenance, so, the RAM usage is temporary.

How Has This Been Tested?

It speeds up RPC protx diff up to 8x.
develop:

$ time ( for j in $(seq 500) ; do src/dash-cli protx diff $((2121000+$j)) $((2121000+$j+1)) ; done ) > /dev/null
real    0m47,743s
user    0m0,472s
sys     0m1,467s

PR:

$ time ( for j in $(seq 500) ; do src/dash-cli protx diff $((2121000+$j)) $((2121000+$j+1)) ; done ) > /dev/null
real    0m6,032s
user    0m0,423s
sys     0m1,300s

It speeds ups blocks's Undo up to 10x; measured by calling invalidateblock blockhash where blockhash is distant block, far from the tip (500+).

Breaking Changes

N/A

Checklist:

  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have added or updated relevant unit/integration/functional/e2e tests
  • I have made corresponding changes to the documentation
  • I have assigned this pull request to a milestone

@knst knst added this to the 23 milestone Jun 24, 2025
@coderabbitai
Copy link

coderabbitai bot commented Jun 24, 2025

Walkthrough

Added #include <node/blockstorage.h> and an in-memory mini-snapshot caching step inside CDeterministicMNManager::GetListForBlockInternal (src/evo/deterministicmns.cpp). After applying each cached diff to the snapshot, when not reindexing (fReindex == false) and the snapshot height is a multiple of 32 (MINI_SNAPSHOT_INTERVAL = 32), the code inserts the current snapshot into mnListsCache keyed by snapshot.GetBlockHash(). No changes to exported/public declarations; existing snapshot/diff loading and other caching logic remain unchanged.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Tip

🔌 Remote MCP (Model Context Protocol) integration is now available!

Pro plan users can now connect to remote MCP servers from the Integrations page. Connect with popular remote MCPs such as Notion and Linear to add more context to your reviews and chats.


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

💡 Knowledge Base configuration:

  • MCP integration is disabled by default for public repositories
  • Jira integration is disabled by default for public repositories
  • Linear integration is disabled by default for public repositories

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between 2896e2b507b61ffde410c263e50ea4e40a1317bd and 366cd2b.

📒 Files selected for processing (1)
  • src/evo/deterministicmns.cpp (2 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/evo/deterministicmns.cpp
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (7)
  • GitHub Check: x86_64-w64-mingw32 / Build depends
  • GitHub Check: Lint / Run linters
  • GitHub Check: x86_64-apple-darwin / Build depends
  • GitHub Check: x86_64-pc-linux-gnu_multiprocess / Build depends
  • GitHub Check: x86_64-pc-linux-gnu_nowallet / Build depends
  • GitHub Check: x86_64-pc-linux-gnu / Build depends
  • GitHub Check: arm-linux-gnueabihf / Build depends
✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

CodeRabbit Commands (Invoked using PR/Issue comments)

Type @coderabbitai help to get the list of available commands.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Status, Documentation and Community

  • Visit our Status Page to check the current availability of CodeRabbit.
  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@knst
Copy link
Collaborator Author

knst commented Jun 24, 2025

@PastaPastaPasta

at first, I haven't planned to get this one merged, because I assumed that it improves only UndoBlock for several blocks; which maybe useful for reconsiderblock; and have very little benefits on mainnet; but actually it improves RPC performance for protx diff, especially for multiple calls for blocks that are close to each other. Also protx list is improved, if it's called multiple times for blocks that are close to each other in chain.

@knst knst force-pushed the perf-rpc-protx-diff branch from 4de63d1 to 30a9b2e Compare June 24, 2025 20:47
@knst knst requested review from PastaPastaPasta and UdjinM6 July 1, 2025 15:20
Comment on lines 1072 to 798
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Consider Making Cache Interval Configurable
    - The hardcoded value 32 could be a named constant or configurable parameter
    static constexpr int MINI_SNAPSHOT_INTERVAL = 32;
    if (snapshot.GetHeight() % MINI_SNAPSHOT_INTERVAL == 0) {

@PastaPastaPasta
Copy link
Member

Consider:

  1. Add Test Coverage
    • The PR lacks unit tests verifying the caching behavior
    • Consider adding tests to ensure:
      • Snapshots are created at correct intervals
      • Cache hits improve performance
      • Cache cleanup doesn't remove actively used snapshots
  2. Documentation
    - The inline comment is good but could explain the "575 diffs" calculation more clearly
    - Consider adding a brief explanation of why 32 was chosen as the interval

@UdjinM6
Copy link

UdjinM6 commented Aug 15, 2025

  1. We should probably disable this extra cache while node is still reindexing e.g
diff --git a/src/evo/deterministicmns.cpp b/src/evo/deterministicmns.cpp
index 921a119642..3f2dab1d3e 100644
--- a/src/evo/deterministicmns.cpp
+++ b/src/evo/deterministicmns.cpp
@@ -16,6 +16,7 @@
 #include <consensus/validation.h>
 #include <deploymentstatus.h>
 #include <messagesigner.h>
+#include <node/blockstorage.h>
 #include <script/standard.h>
 #include <stats/client.h>
 #include <uint256.h>
@@ -26,6 +27,8 @@
 #include <optional>
 #include <memory>
 
+using node::fReindex;
+
 static const std::string DB_LIST_SNAPSHOT = "dmn_S3";
 static const std::string DB_LIST_DIFF = "dmn_D3";
 
@@ -780,7 +783,7 @@ CDeterministicMNList CDeterministicMNManager::GetListForBlockInternal(gsl::not_n
     for (const auto& diffIndex : listDiffIndexes) {
         const auto& diff = mnListDiffsCache.at(diffIndex->GetBlockHash());
         snapshot.ApplyDiff(diffIndex, diff);
-        if (snapshot.GetHeight() % 32 == 0) {
+        if (!fReindex && (snapshot.GetHeight() % 32 == 0)) {
             // Add this temporary mini-snapshot to cache.
             // This extra cached mn-list helps to improve performance of GetListForBlock
             // for close blocks, because in the worst cases each of them requires to retrieve
  1. Consider Making Cache Interval Configurable
    • The hardcoded value 32 could be a named constant or configurable parameter
      static constexpr int MINI_SNAPSHOT_INTERVAL = 32;
      if (snapshot.GetHeight() % MINI_SNAPSHOT_INTERVAL == 0) {

Agree

  1. Add Test Coverage

    • The PR lacks unit tests verifying the caching behavior

    • Consider adding tests to ensure:

      • Snapshots are created at correct intervals
      • Cache hits improve performance
      • Cache cleanup doesn't remove actively used snapshots

Adding unit tests here feels like an overkill tbh

@github-actions
Copy link

github-actions bot commented Aug 18, 2025

✅ No Merge Conflicts Detected

This PR currently has no conflicts with other open PRs.

@knst
Copy link
Collaborator Author

knst commented Aug 18, 2025

Consider Making Cache Interval Configurable

I declared named const MINI_SNAPSHOT_INTERVAL for it

The PR lacks unit tests verifying the caching behavior

It's a bit overkill IMO. considerable, possible to implement bench module for it; but it's not unit test. Also notice, I haven't touch cleanup function -> almost impossible to break any current logic (such as active quorum, tip caching)

Cache cleanup doesn't remove actively used snapshots

Implementation of cache cleanup is intact; every cleanup() call called by scheduler will remove this mini-snapshots.
They are meant to live only seconds; don't abuse RAM and helps only for consequent and often calls of RPC.

They help to improve performance for 50% of calculation protx diff N N+1 (it's an ideal case when cache will exist for sure) or reconsideration of any amount of blocks bigger than 1.

  • Consider adding a brief explanation of why 32 was chosen as the interval

24 is a theoretically calculated number for MINI_SNAPSHOT_INTERVAL to reduce complexity from O(N^2) to O(N * sqrt(N)) where N is the distance between snapshots; for case if you want to pass all blocks between snapshots one time.
Though, I bench it, 32 and 24 had very close runtime, but 32 uses less RAM.
So, 32 has been chosen as it is close to theoretically calculated value; give a good performance boost and more conservative about RAM usage.

The best theoretical performance will have MINI_SNAPSHOT_INTERVAL=1 but it's a bit abusive for RAM, if scheduler won't be called on time.
Theoretical difference between MINI_SNAPSHOT_INTERVAL=1 and MINI_SNAPSHOT_INTERVAL=32 is less than 40% (assumed by results of benchmarking with perf) and it will potentially improve benefits of this PR from 10x for undo and 8x for protx diff to potentially up 15x for undo and up to 12x for protx diff in the best optimistic scenario, but I would prefer to be more conservative for RAM.

@knst
Copy link
Collaborator Author

knst commented Aug 18, 2025

A new circular dependency in the form of "evo/deterministicmns -> node/blockstorage -> evo/deterministicmns" appears to have been introduced.
Good job! The circular dependency "dsnotificationinterface -> llmq/chainlocks -> node/blockstorage -> dsnotificationinterface" is no longer present.
Please remove it from EXPECTED_CIRCULAR_DEPENDENCIES in /__w/dash/dash/test/lint/lint-circular-dependencies.py

any ideas how to avoid it? Due to using fReindex

Nevermind, I already solved it by #6728, which is merged.
Fixed by rebasing on top of develop

knst added 2 commits August 18, 2025 15:44
…r historical blocks

It speeds ups blocks's Undo up to 10x. It speeds up RPC `protx diff` up to 9x
@knst knst force-pushed the perf-rpc-protx-diff branch from 589587c to 2896e2b Compare August 18, 2025 08:44
@knst knst force-pushed the perf-rpc-protx-diff branch from 2896e2b to 366cd2b Compare August 18, 2025 08:54
Copy link

@UdjinM6 UdjinM6 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, utACK 366cd2b

@knst knst requested a review from PastaPastaPasta August 19, 2025 08:03
@PastaPastaPasta PastaPastaPasta merged commit 8fc24bd into dashpay:develop Aug 19, 2025
32 of 33 checks passed
@knst knst deleted the perf-rpc-protx-diff branch August 19, 2025 13:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants