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

optimize hash_tree_root using hashtree #35

Merged
merged 11 commits into from
May 1, 2024
Merged

optimize hash_tree_root using hashtree #35

merged 11 commits into from
May 1, 2024

Conversation

arnetheduck
Copy link
Member

@arnetheduck arnetheduck commented Aug 16, 2022

brings hash_tree_root(BeaconState) down ~800ms to ~660ms - further optimization is possible by hashing multiple blocks in a single call, but this requires more significant surgery.


func best_sha256_implementation(): Hasher {.importc.}

let digest64* = block:
Copy link

Choose a reason for hiding this comment

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

digest64 is a really bad name. Maybe digest2x or digest_2way?

Copy link
Member Author

Choose a reason for hiding this comment

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

well .. it's 64 bytes which is the significant requirement here .. ie unlike the other digest functions, it only works for blocks of that length.

Copy link
Contributor

Choose a reason for hiding this comment

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

just call it digestBlockSize :)

h.update a
h.update b
h.update c
func digest(a, b, c: openArray[byte]): Digest {.noinit.} =
Copy link

Choose a reason for hiding this comment

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

When does this happen?

Copy link
Member Author

Choose a reason for hiding this comment

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

when joining a 32-byte data chunk with a <32-byte chunk - c is the zero padding mandated by ssz

20% from digest64
10% from working around nim-lang/Nim#20232

brings `hash_tree_root(BeaconState)` down ~480ms to ~320ms from the
padding optimization alone - further optimization possible by
parallelizing the hashing of each tree level, but that requires more
surgery.
@arnetheduck arnetheduck marked this pull request as ready for review February 9, 2024 10:32
@arnetheduck arnetheduck changed the title wip: optimize hash_tree_root using hashtree optimize hash_tree_root using hashtree Feb 9, 2024
@arnetheduck arnetheduck merged commit ec87026 into master May 1, 2024
12 checks passed
@arnetheduck arnetheduck deleted the hashtree branch May 1, 2024 12:57
arnetheduck added a commit to status-im/nimbus-eth2 that referenced this pull request May 2, 2024
status-im/nim-ssz-serialization#35 brings in https://github.com/prysmaticlabs/hashtree as a supported backend for SHA256, giving a nice little performance boost to all hash_tree_root calls on supported platforms / compilers.

Expected gains are on the order of 30% which in the case of a replayed state nets us 0.2-0.3s improvement.

More about this design here: https://hackmd.io/@potuz/BJyrx9DOF - kudos to @potuz for this excellent library!
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.

None yet

3 participants