Skip to content
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

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 #6581

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from

Conversation

PastaPastaPasta
Copy link
Member

Profiling Analysis

before
Pasted Graphic 11

after
Pasted Graphic

Methods

Below, is some analysis on the results of running the protx diff RPC 500 times. The diffs had a start block between MIN and MAX as defined below; and an end block no more than MAX_DIFF from the selected start block. We then perform some statistical analysis on the data.

MIN_VALUE = 1500050
MAX_VALUE = 2000050
MAX_DIFF = 50000

Statistical Analysis, outliers included

Before

Five-Number Summary of Execution Times:
Min: 0.024492 sec
Q1: 0.124626 sec
Median: 0.243000 sec
Q3: 0.358459 sec
Max: 15.583948 sec

Mean Execution Time: 0.428296 sec
Standard Deviation: 0.933486 sec

Linear Regression Results:
y = 0.000001 * x + 0.308662
R-squared: 0.008160 (Goodness of Fit)
Observed Data
Pasted Graphic 6

After

Five-Number Summary of Execution Times:
Min: 0.038174 sec
Q1: 0.121363 sec
Median: 0.158175 sec
Q3: 0.215866 sec
Max: 16.587903 sec

Mean Execution Time: 0.239239 sec
Standard Deviation: 0.762387 sec

Linear Regression Results:
y = 0.000001 * x + 0.151169
R-squared: 0.006918 (Goodness of Fit)
P-value: 0.063105 (Significance)
Observed Data
Observed Data

Statistical Analysis, outliers excluded

Before

removed 76 data points
Five-Number Summary of Execution Times (After Outlier Removal):
Min: 0.035916 sec
Q1: 0.211060 sec
Median: 0.319278 sec
Q3: 0.357963 sec
Max: 0.572785 sec

Mean Execution Time: 0.289764 sec
Standard Deviation: 0.101140 sec

Linear Regression Results (After Outlier Removal):
y = 0.0000000199 * x + 0.286447
R-squared: 0.000496 (Goodness of Fit)
Pasted Graphic 10

After

removed 32 data points
Five-Number Summary of Execution Times (After Outlier Removal):
Min: 0.038174 sec
Q1: 0.119880 sec
Median: 0.151724 sec
Q3: 0.205017 sec
Max: 0.355078 sec

Mean Execution Time: 0.164165 sec
Standard Deviation: 0.060919 sec

Linear Regression Results (After Outlier Removal):
y = 0.0000003119 * x + 0.111002
R-squared: 0.399298 (Goodness of Fit)

Observed Data

How Has This Been Tested?

Ran unit tests locally; reindexing currently, going to let CI run functional tests

Breaking Changes

Should be none; but please think through the diff specifically related to https://github.com/dashpay/dash/compare/develop...PastaPastaPasta:dash:perf-build-simplified-mn-list-diff-bls-compare-to-null?expand=1#diff-0998f8dfc4c1089e90cbaafe9607b361035b904cd103df31e3c2339a3cbf790dR480

Checklist:

Go over all the following points, and put an x in all the boxes that apply.

  • 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 (for repository code-owners and collaborators only)

@PastaPastaPasta PastaPastaPasta added this to the 23 milestone Feb 16, 2025
Copy link

coderabbitai bot commented Feb 16, 2025

Walkthrough

This pull request introduces changes in three main areas. In the BLS module, the CBLSLazyWrapper class is updated with a new mutable Boolean member variable, bufValid, which indicates the validity of the internal byte vector based on whether any byte is non-zero during unserialization. The equality operator is also modified to include checks for default versus non-default object states. In the deterministic masternode management, the validation logic for the pubKeyOperator property in the AddMN, RemoveMN, and BuildNewListFromBlock methods of the CDeterministicMNList class is revised. The validation now compares the pubKeyOperator against a default constructed CBLSLazyPublicKey instead of using the IsValid() method. Additionally, a new DummyBLS class is introduced in the testing framework, along with new test cases for LazyDummyBLS, which validate various functionalities of the BLS implementation. These changes refine the evaluation of state validity and equality in their respective components.

✨ Finishing Touches
  • 📝 Generate Docstrings (Beta)

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ 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.
    • Generate unit testing code for this file.
    • 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. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • 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 src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

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

Documentation and Community

  • 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.

@PastaPastaPasta
Copy link
Member Author

Reindex finished successfully

…default / null object; speedup CDeterministicMNList::AddMN by avoiding check to IsValid when a nullcheck is sufficient
@PastaPastaPasta PastaPastaPasta force-pushed the perf-build-simplified-mn-list-diff-bls-compare-to-null branch from 40909d7 to ada6f2b Compare February 17, 2025 18:14
knst
knst previously approved these changes Feb 18, 2025
Copy link
Collaborator

@knst knst left a comment

Choose a reason for hiding this comment

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

LGTM ada6f2b

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (2)
src/test/bls_tests.cpp (2)

475-524: Enhance class documentation and exception safety.

Consider the following improvements to the DummyBLS class:

  1. Add method documentation explaining the purpose and contract of each public method
  2. Add noexcept specifications where applicable (e.g., constructors, Reset, IsValid)
  3. Mark methods that don't modify state as const
 class DummyBLS {
 public:
     static const size_t SerSize = 4;
     std::array<uint8_t, SerSize> data{};
 
-    DummyBLS() {
+    /// Default constructor initializing data to zeros
+    DummyBLS() noexcept {
         data.fill(0);
     }
 
+    /// @returns true if any byte in data is non-zero
     bool IsValid() const {
         return std::any_of(data.begin(), data.end(), [](uint8_t c){ return c != 0; });
     }
 
+    /// @returns data array ignoring legacy flag
     std::array<uint8_t, SerSize> ToBytes(bool /*legacy*/) const {
         return data;
     }
 
+    /// Sets data from bytes array ignoring legacy flag
     void SetBytes(const std::array<uint8_t, SerSize>& bytes, bool /*legacy*/) {
         data = bytes;
     }
 
+    /// @returns true if data matches given bytes ignoring legacy flag
     bool CheckMalleable(const std::array<uint8_t, SerSize>& bytes, bool /*legacy*/) const {
         return data == bytes;
     }
 
+    /// Resets data to zeros
     void Reset() noexcept {
         data.fill(0);
     }

529-609: Add test coverage for edge cases and error conditions.

While the current test suite is good, consider adding the following test cases:

  1. Test behavior with invalid data
  2. Test error conditions in serialization/deserialization
  3. Test the Reset() functionality
  4. Test behavior with zero-filled but non-default objects

Would you like me to generate the additional test cases?

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between ada6f2b and e18f621.

📒 Files selected for processing (3)
  • src/bls/bls.h (3 hunks)
  • src/evo/deterministicmns.cpp (3 hunks)
  • src/test/bls_tests.cpp (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
  • src/bls/bls.h
  • src/evo/deterministicmns.cpp
🧰 Additional context used
🪛 GitHub Actions: Clang Diff Format Check
src/test/bls_tests.cpp

[error] 473-473: Clang format differences found. Please run 'clang-format' to format the code.

⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: Build container / Build container
🔇 Additional comments (1)
src/test/bls_tests.cpp (1)

526-527: LGTM!

The type alias is well-defined and appropriately named.

@@ -472,4 +472,140 @@ BOOST_AUTO_TEST_CASE(bls_threshold_signature_tests)
FuncThresholdSignature(false);
}
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Fix clang-format issue.

The CI pipeline indicates a formatting issue. Please run clang-format on the file to resolve this.

🧰 Tools
🪛 GitHub Actions: Clang Diff Format Check

[error] 473-473: Clang format differences found. Please run 'clang-format' to format the code.

knst
knst previously approved these changes Feb 24, 2025
Copy link
Collaborator

@knst knst left a comment

Choose a reason for hiding this comment

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

utACK e18f621

DummyBLS obj;
obj.data = {1, 2, 3, 4}; // nonzero data makes the object valid
lazy_a.Set(obj, false);
obj.data = {4, 3, 2, 1}; // nonzero data makes the object valid
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: maybe make only 1 byte difference here, to catch error "any_of" vs "all_of" in operator ==

obj.data = {0, 2, 3, 4}; // nonzero data makes the object valid

LazyDummyBLS lazy2;
BOOST_CHECK(lazy1 == lazy2);
DummyBLS obj;
obj.data = {5, 6, 7, 8};
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: maybe make only 1 byte difference here, to catch error "any_of" vs "all_of".

such as:

obj.data = {1, 2, 3, 5};


// Produce a string representation.
std::string ToString(bool /*legacy*/) const {
std::ostringstream oss;
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: refactor by using HexStr from src/util/strencodings.h

@UdjinM6
Copy link

UdjinM6 commented Feb 24, 2025

Looks good but clang-format complains a lot. Pls consider formatting new tests accordingly (and also pls add a new line at eof).

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (2)
src/test/bls_tests.cpp (2)

475-515: LGTM! Clean and efficient implementation of the DummyBLS class.

The class provides a good minimal interface for testing CBLSLazyWrapper with efficient implementations. The use of std::array and std::any_of shows good C++ practices.

Consider simplifying the ToString method using HexStr from src/util/strencodings.h as suggested in the past review:

-    std::string ToString(bool /*legacy*/) const
-    {
-        std::ostringstream oss;
-        for (auto b : data) {
-            oss << std::setfill('0') << std::setw(2) << std::hex << static_cast<int>(b);
-        }
-        return oss.str();
-    }
+    std::string ToString(bool /*legacy*/) const
+    {
+        return HexStr(data);
+    }

520-600: LGTM! Comprehensive test coverage for LazyDummyBLS.

The test suite thoroughly covers equality comparisons, serialization, and hash consistency.

Consider modifying the test data to better catch potential edge cases. As suggested in past reviews, use minimal differences:

-    obj.data = {1, 2, 3, 4}; // nonzero data makes the object valid
+    obj.data = {1, 0, 0, 0}; // single nonzero byte to catch any_of vs all_of errors

This change would help catch potential issues with the IsValid() implementation if it were mistakenly using all_of instead of any_of.

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between e18f621 and 56ac184.

📒 Files selected for processing (2)
  • src/evo/deterministicmns.cpp (3 hunks)
  • src/test/bls_tests.cpp (1 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 (1)
  • GitHub Check: Build container / Build container
🔇 Additional comments (2)
src/test/bls_tests.cpp (2)

517-518: LGTM! Clear type alias definition.

The type alias improves readability in the test cases.


473-473: Fix clang-format issue.

The CI pipeline indicates a formatting issue. Please run clang-format on the file to resolve this.

@UdjinM6
Copy link

UdjinM6 commented Feb 24, 2025

pls consider fe5250a

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (5)
src/test/bls_tests.cpp (5)

489-489: Consider using std::ranges::any_of for modern C++.

For better readability and maintainability, consider using the more modern std::ranges::any_of algorithm.

-        return std::any_of(data.begin(), data.end(), [](uint8_t c) { return c != 0; });
+        return std::ranges::any_of(data, [](uint8_t c) { return c != 0; });

529-529: Consider using more diverse test data.

The test data could be more diverse to catch edge cases. For example:

  • Test with all zeros except the last byte
  • Test with alternating patterns
  • Test with all maximum values (0xFF)
-    obj.data = {1, 0, 0, 0}; // nonzero data makes the object valid
+    obj.data = {0, 0, 0, 1}; // Test last byte non-zero
-    obj.data = {1, 2, 3, 4}; // nonzero data makes the object valid
+    obj.data = {0xAA, 0x55, 0xAA, 0x55}; // Test alternating pattern
-    obj.data = {5, 6, 7, 8};
+    obj.data = {0xFF, 0xFF, 0xFF, 0xFF}; // Test maximum values

Also applies to: 541-541, 555-555


535-535: Fix duplicate test case comment.

The comment for this test case is identical to the previous one (Test 2), but the test case is different.

-// Test 2: A default wrapper and one initialized with a nonzero DummyBLS should compare unequal.
+// Test 3: Two different non-default wrappers should compare unequal.

572-573: Consider testing different serialization versions.

The serialization tests only verify with a single legacy flag value. Consider testing both legacy and non-legacy serialization.

     CDataStream ds(SER_DISK, CLIENT_VERSION);
-    lazy1.Serialize(ds, true);
+    // Test both legacy and non-legacy serialization
+    lazy1.Serialize(ds, true);
+    lazy1.Serialize(ds, false);

     LazyDummyBLS lazy2;
-    lazy2.Unserialize(ds, true);
-    BOOST_CHECK(lazy1 == lazy2);
+    lazy2.Unserialize(ds, true);
+    BOOST_CHECK(lazy1 == lazy2);
+    lazy2.Unserialize(ds, false);
+    BOOST_CHECK(lazy1 == lazy2);

Also applies to: 577-578


582-594: Consider adding hash collision test.

The hash consistency test is good, but consider adding a test to verify that different objects produce different hashes.

     uint256 hash1 = lazy1.GetHash();
     uint256 hash2 = lazy2.GetHash();
     BOOST_CHECK(hash1 == hash2);
+
+    // Test that different objects have different hashes
+    DummyBLS different_obj;
+    different_obj.data = {16, 15, 14, 13};  // Different from original
+    lazy2.Set(different_obj, false);
+    uint256 hash3 = lazy2.GetHash();
+    BOOST_CHECK(hash1 != hash3);
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 56ac184 and 0cf8a46.

📒 Files selected for processing (1)
  • src/test/bls_tests.cpp (2 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: Build container / Build container
🔇 Additional comments (2)
src/test/bls_tests.cpp (2)

476-509: LGTM! Well-structured mock implementation.

The DummyBLS class provides a clean and minimal interface required by CBLSLazyWrapper. The implementation includes all necessary methods with clear documentation.


505-505: Good use of HexStr utility.

Excellent use of the HexStr utility from strencodings.h for string representation.

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 0cf8a46

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