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

precalculate SHA256 hashes. Speed-up: 1.2x #387

Merged
merged 1 commit into from
Feb 26, 2024
Merged

Conversation

arvidn
Copy link
Contributor

@arvidn arvidn commented Feb 22, 2024

This was @Quexington 's suggestion.

An earlier version of this description had benchmarks numbers that were invalid. It has been updated with correct benchmarks.

change

For op_sha256(), use pre-calculated SHA-256 hashes for common cases. Specifically (sha256 1 <op-code>) which is a common case when computing the tree hash of programs, e.g. puzzles.

This gives a modest speed-up on most blocks.

benchmarks

These are benchmarks in chia_rs, as those are higher level and running real block generators.

All benchmarks:

group                                                    after                 before
-----                                                    -----                 ------
run_block_generator block-1ee588dc                       1.00     76.9±0.38ms  1.20     91.9±0.61ms
run_block_generator block-1ee588dc-compressed            1.00     76.2±0.57ms  1.21     92.3±0.81ms
run_block_generator block-225758                         1.00      3.5±0.02ms  1.09      3.8±0.02ms
run_block_generator block-225758-compressed              1.00      3.5±0.01ms  1.08      3.8±0.01ms
run_block_generator block-4671894                        1.00    199.6±3.00ms  1.07    214.2±0.83ms
run_block_generator block-4671894-compressed             1.00    199.6±1.13ms  1.07    214.3±2.26ms
run_block_generator block-6fe59b24                       1.00     85.1±0.72ms  1.20    102.5±0.17ms
run_block_generator block-6fe59b24-compressed            1.00     83.7±0.51ms  1.20    100.4±1.41ms
run_block_generator block-834752                         1.00      6.4±0.05ms  1.19      7.7±0.06ms
run_block_generator block-834752-compressed              1.00      6.4±0.05ms  1.17      7.5±0.09ms
run_block_generator block-b45268ac                       1.00     75.9±0.46ms  1.19     90.6±0.43ms
run_block_generator block-b45268ac-compressed            1.00     74.5±0.36ms  1.20     89.3±0.76ms
run_block_generator block-c2a8df0d                       1.00     90.6±0.56ms  1.18    106.8±0.35ms
run_block_generator block-c2a8df0d-compressed            1.00     87.4±0.21ms  1.21    105.9±0.30ms
run_block_generator block-e5002df2                       1.00     80.2±0.36ms  1.19     95.7±0.52ms
run_block_generator block-e5002df2-compressed            1.00     78.4±0.23ms  1.21     94.8±0.77ms
run_block_generator deep-recursion-plus                  1.00   1113.4±7.02ms  1.00   1111.0±0.87ms
run_block_generator deep-recursion-plus-compressed       1.00   1109.7±3.14ms  1.00   1112.3±2.23ms
run_block_generator duplicate-coin-announce              1.00  1296.6±20.51ms  1.00   1299.8±4.42ms
run_block_generator duplicate-coin-announce-compressed   1.00  1299.1±21.38ms  1.00   1299.1±4.27ms
run_block_generator recursion-pairs                      1.00    896.2±7.74ms  1.01    901.4±2.30ms
run_block_generator recursion-pairs-compressed           1.00    894.0±2.69ms  1.01    902.2±2.17ms
run_block_generator2 block-1ee588dc                      1.00     41.4±0.36ms  1.21     49.9±0.19ms
run_block_generator2 block-1ee588dc-compressed           1.00     12.2±0.10ms  1.15     14.0±1.12ms
run_block_generator2 block-225758                        1.00      2.8±0.02ms  1.07      3.0±0.02ms
run_block_generator2 block-225758-compressed             1.00      2.8±0.01ms  1.07      3.0±0.01ms
run_block_generator2 block-4671894                       1.00    159.0±0.69ms  1.06    168.9±0.67ms
run_block_generator2 block-4671894-compressed            1.00    160.0±1.07ms  1.06    169.0±0.43ms
run_block_generator2 block-6fe59b24                      1.00     46.5±0.43ms  1.21     56.2±0.32ms
run_block_generator2 block-6fe59b24-compressed           1.00     15.4±0.20ms  1.12     17.2±0.14ms
run_block_generator2 block-834752                        1.00      3.9±0.04ms  1.20      4.7±0.02ms
run_block_generator2 block-834752-compressed             1.00      2.0±0.03ms  1.16      2.4±0.01ms
run_block_generator2 block-b45268ac                      1.00     41.0±0.40ms  1.20     49.3±0.30ms
run_block_generator2 block-b45268ac-compressed           1.00     12.2±0.04ms  1.10     13.5±0.07ms
run_block_generator2 block-c2a8df0d                      1.00     47.4±0.06ms  1.23     58.3±0.44ms
run_block_generator2 block-c2a8df0d-compressed           1.00     11.8±0.02ms  1.10     13.0±0.12ms
run_block_generator2 block-e5002df2                      1.00     42.9±0.37ms  1.23     52.6±0.10ms
run_block_generator2 block-e5002df2-compressed           1.00     13.9±0.06ms  1.13     15.8±0.15ms
run_block_generator2 deep-recursion-plus                 1.00   1106.8±2.95ms  1.00   1111.6±1.62ms
run_block_generator2 deep-recursion-plus-compressed      1.00   1110.0±3.25ms  1.00   1111.5±1.26ms
run_block_generator2 duplicate-coin-announce             1.00  1300.9±17.45ms  1.00   1299.7±3.79ms
run_block_generator2 duplicate-coin-announce-compressed  1.00   1285.2±3.11ms  1.01   1299.1±4.59ms
run_block_generator2 recursion-pairs                     1.00    892.8±1.78ms  1.01    900.2±2.82ms
run_block_generator2 recursion-pairs-compressed          1.00    893.4±1.10ms  1.01    900.6±0.87ms

Copy link

coveralls-official bot commented Feb 22, 2024

Pull Request Test Coverage Report for Build 8013269210

Details

  • 0 of 59 (100.0%) changed or added relevant lines in 2 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.05%) to 94.362%

Totals Coverage Status
Change from base Build 8007877860: 0.05%
Covered Lines: 5858
Relevant Lines: 6208

💛 - Coveralls

@arvidn arvidn changed the title precalculate SHA256 hashes for common values in puzzles (op-codes) precalculate SHA256 hashes. Speed-up: 14x-17x Feb 22, 2024
@arvidn arvidn marked this pull request as ready for review February 22, 2024 16:51
@arvidn
Copy link
Contributor Author

arvidn commented Feb 22, 2024

TODO:

  • extend unit test to cover all optimized cases + some
  • apply the same optimization to the other tree-hash function as well

@richardkiss
Copy link
Contributor

Be sure to do a test for hashing the two values 01 00, ie. the one-byte but non-minimal 0 integer value. We want to make sure it's different from 01, ie. the minimal 0 integer value.

@richardkiss
Copy link
Contributor

Remarkable speed-up!

@arvidn arvidn changed the title precalculate SHA256 hashes. Speed-up: 14x-17x precalculate SHA256 hashes. Speed-up: 1.2x Feb 23, 2024
@arvidn arvidn merged commit d8c9c27 into main Feb 26, 2024
29 checks passed
@arvidn arvidn deleted the sha256-precalculated branch February 26, 2024 10:32
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