-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
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
ada6f2b
e18f621
56ac184
0cf8a46
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 |
|---|---|---|
|
|
@@ -8,6 +8,7 @@ | |
| #include <random.h> | ||
| #include <streams.h> | ||
| #include <util/irange.h> | ||
| #include <util/strencodings.h> | ||
|
|
||
| #include <boost/test/unit_test.hpp> | ||
|
|
||
|
|
@@ -472,4 +473,124 @@ BOOST_AUTO_TEST_CASE(bls_threshold_signature_tests) | |
| FuncThresholdSignature(false); | ||
| } | ||
|
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. 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. |
||
|
|
||
| // A dummy BLS object that satisfies the minimal interface expected by CBLSLazyWrapper. | ||
| class DummyBLS | ||
| { | ||
| public: | ||
| // Define a fixed serialization size (for testing purposes). | ||
| static const size_t SerSize = 4; | ||
| std::array<uint8_t, SerSize> data{}; | ||
|
|
||
| DummyBLS() { data.fill(0); } | ||
|
|
||
| // A dummy validity check: valid if any byte is non-zero. | ||
| bool IsValid() const | ||
| { | ||
| return std::any_of(data.begin(), data.end(), [](uint8_t c) { return c != 0; }); | ||
| } | ||
|
|
||
| // Convert to bytes; ignore the legacy flag for simplicity. | ||
| std::array<uint8_t, SerSize> ToBytes(bool /*legacy*/) const { return data; } | ||
|
|
||
| // Set from bytes; again, ignore the legacy flag. | ||
| void SetBytes(const std::array<uint8_t, SerSize>& bytes, bool /*legacy*/) { data = bytes; } | ||
|
|
||
| // A dummy malleability check: simply compares the stored data to the given bytes. | ||
| bool CheckMalleable(const std::array<uint8_t, SerSize>& bytes, bool /*legacy*/) const { return data == bytes; } | ||
|
|
||
| // Reset the object to an "empty" state. | ||
| void Reset() { data.fill(0); } | ||
|
|
||
| // Produce a string representation. | ||
| std::string ToString(bool /*legacy*/) const { return HexStr(data); } | ||
|
|
||
| // Equality operator. | ||
| bool operator==(const DummyBLS& other) const { return data == other.data; } | ||
| }; | ||
|
|
||
| // Define a type alias for our lazy wrapper instantiated with DummyBLS. | ||
| using LazyDummyBLS = CBLSLazyWrapper<DummyBLS>; | ||
|
|
||
| // Test 1: Two default (unset) wrappers should compare equal. | ||
| BOOST_AUTO_TEST_CASE(test_default_equality) | ||
| { | ||
| LazyDummyBLS lazy1; | ||
| LazyDummyBLS lazy2; | ||
| // Neither instance has been set, so they represent the default/null object. | ||
| BOOST_CHECK(lazy1 == lazy2); | ||
| } | ||
|
|
||
| // Test 2: A default wrapper and one initialized with a nonzero DummyBLS should compare unequal. | ||
| BOOST_AUTO_TEST_CASE(test_non_default_vs_default) | ||
| { | ||
| LazyDummyBLS lazy_default; | ||
| LazyDummyBLS lazy_set; | ||
| DummyBLS obj; | ||
| obj.data = {1, 0, 0, 0}; // nonzero data makes the object valid | ||
| lazy_set.Set(obj, false); | ||
| BOOST_CHECK(!(lazy_default == lazy_set)); | ||
| BOOST_CHECK(lazy_default != lazy_set); | ||
| } | ||
|
|
||
| // Test 2: A default wrapper and one initialized with a nonzero DummyBLS should compare unequal. | ||
| BOOST_AUTO_TEST_CASE(test_non_default_vs_different) | ||
| { | ||
| LazyDummyBLS lazy_a; | ||
| LazyDummyBLS lazy_b; | ||
| DummyBLS obj; | ||
| obj.data = {1, 2, 3, 4}; // nonzero data makes the object valid | ||
| lazy_a.Set(obj, false); | ||
| obj.data = {2, 2, 3, 4}; // nonzero data makes the object valid | ||
| lazy_b.Set(obj, false); | ||
| BOOST_CHECK(lazy_a != lazy_b); | ||
| } | ||
|
|
||
| // Test 3: Two wrappers set with the same underlying DummyBLS value compare equal. | ||
| BOOST_AUTO_TEST_CASE(test_equality_same_value) | ||
| { | ||
| LazyDummyBLS lazy1; | ||
| LazyDummyBLS lazy2; | ||
| BOOST_CHECK(lazy1 == lazy2); | ||
| DummyBLS obj; | ||
| obj.data = {5, 6, 7, 8}; | ||
|
Collaborator
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. nit: maybe make only 1 byte difference here, to catch error "any_of" vs "all_of". such as: |
||
| lazy1.Set(obj, false); | ||
| BOOST_CHECK(lazy1 != lazy2); | ||
| lazy2.Set(obj, false); | ||
| BOOST_CHECK(lazy1 == lazy2); | ||
| } | ||
|
|
||
| // Test 4: Serialization and unserialization preserve the wrapped value. | ||
| BOOST_AUTO_TEST_CASE(test_serialization_unserialization) | ||
| { | ||
| LazyDummyBLS lazy1; | ||
| DummyBLS obj; | ||
| obj.data = {9, 10, 11, 12}; | ||
| // Set with a specific legacy flag (true in this case) | ||
| lazy1.Set(obj, true); | ||
|
|
||
| // Serialize the lazy object into a data stream. | ||
| CDataStream ds(SER_DISK, CLIENT_VERSION); | ||
| lazy1.Serialize(ds, true); | ||
|
|
||
| // Create a new instance and unserialize the data into it. | ||
| LazyDummyBLS lazy2; | ||
| lazy2.Unserialize(ds, true); | ||
| BOOST_CHECK(lazy1 == lazy2); | ||
| BOOST_CHECK(lazy2.Get() == obj); | ||
| } | ||
|
|
||
| // Test 5: Two wrappers wrapping the same object should have the same hash. | ||
| BOOST_AUTO_TEST_CASE(test_get_hash_consistency) | ||
| { | ||
| LazyDummyBLS lazy1; | ||
| LazyDummyBLS lazy2; | ||
| DummyBLS obj; | ||
| obj.data = {13, 14, 15, 16}; | ||
| lazy1.Set(obj, false); | ||
| lazy2.Set(obj, false); | ||
| uint256 hash1 = lazy1.GetHash(); | ||
| uint256 hash2 = lazy2.GetHash(); | ||
| BOOST_CHECK(hash1 == hash2); | ||
| } | ||
|
|
||
| BOOST_AUTO_TEST_SUITE_END() | ||
Uh oh!
There was an error while loading. Please reload this page.