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

cryptonote_basic: faster and more readable is_valid_decomposed_amount #9122

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

jeffro256
Copy link
Contributor

No description provided.

Copy link
Collaborator

@0xFFFC0000 0xFFFC0000 left a comment

Choose a reason for hiding this comment

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

Left a comment on while loop.

if (0 == amount)
return false;

while (amount % 10 == 0)
Copy link
Collaborator

@0xFFFC0000 0xFFFC0000 Jan 15, 2024

Choose a reason for hiding this comment

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

Thanks @jeffro256 for this PR. It is great. I have a few comments that we can talk about.

a. It would be great if we could add a small benchmark, and test included in this PR.
b. While a while loop is simple and easy to read. I believe we can test the algorithm performance:

inline // better to have FORCEINLINE here
int c_t_z(uint64_t n) {
    int zeros = 0;
    if (n % 10000000000000000ULL == 0) { zeros += 16; n /= 10000000000000000ULL; }
    if (n % 100000000 == 0) { zeros += 8; n /= 100000000; }
    if (n % 10000 == 0) { zeros += 4; n /= 10000; }
    if (n % 100 == 0) { zeros += 2; n /= 100; }
    if (n % 10 == 0) { zeros += 1; }
    return zeros;
}
.
.
.
amount >>= zeros;

I am not sure how something like this would perform in comparison with the while loop. But worth looking at. Code size-wise, of course while version wins, but performance-wise I would look into the if implementation [1].

I will ping you on Matrix, to talk about these, even maybe we can work together on this.

Overall everything is clean, and ready to merge after making sure that we are not missing something.

  1. https://godbolt.org/z/E3fxaf8Wh
@jeffro256 jeffro256 force-pushed the faster_is_valid_decomposed_amount branch from 0a37878 to 7e571a0 Compare January 15, 2024 21:32
@jeffro256
Copy link
Contributor Author

Add a performance test suite and used fast power decomposition.

Results:

Perf test applied to master:

test_is_valid_decomposed_amount (3 calls) - OK: 5724 ms/call
Tests finished. Elapsed time: 17 sec

PR:

test_is_valid_decomposed_amount (3 calls) - OK: 2672 ms/call
Tests finished. Elapsed time: 8 sec
@0xFFFC0000
Copy link
Collaborator

Add a performance test suite and used fast power decomposition.

Results:

Perf test applied to master:

test_is_valid_decomposed_amount (3 calls) - OK: 5724 ms/call
Tests finished. Elapsed time: 17 sec

PR:

test_is_valid_decomposed_amount (3 calls) - OK: 2672 ms/call
Tests finished. Elapsed time: 8 sec

Just out of curiosity, do you have perf numbers for loop version too?

Overall it is now very clean. Thanks for considering my suggestion :)

@jeffro256 jeffro256 force-pushed the faster_is_valid_decomposed_amount branch from 7e571a0 to 45ad242 Compare January 15, 2024 21:41
@jeffro256
Copy link
Contributor Author

Was just about to test that when you commented lol

@jeffro256
Copy link
Contributor Author

I'm reverting back to the loop after performance testing since the compiler apparently was able to beat me. Using a simple while made the code easier to read and speeds it up almost 2x compared to the unrolled version (which itself is 2x faster than the lookup table).

With loop:

test_is_valid_decomposed_amount (3 calls) - OK: 1506 ms/call
Tests finished. Elapsed time: 4 sec
@jeffro256 jeffro256 force-pushed the faster_is_valid_decomposed_amount branch from 45ad242 to a79734c Compare January 15, 2024 21:44
@0xFFFC0000
Copy link
Collaborator

0xFFFC0000 commented Jan 15, 2024

Great to have the performance test and numbers too. I am fine with loop version too.

Overall, everything is good. Running the test locally in few hours, and will approve once the local run finish.

Copy link
Collaborator

@0xFFFC0000 0xFFFC0000 left a comment

Choose a reason for hiding this comment

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

Test are running correctly too. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
3 participants