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

Cache MessageDigests #1984

Merged
merged 4 commits into from
Mar 10, 2021
Merged

Cache MessageDigests #1984

merged 4 commits into from
Mar 10, 2021

Conversation

shemnon
Copy link
Contributor

@shemnon shemnon commented Mar 6, 2021

Fetching the hash from the java security providers is often longer than
the hashing itself, so we should cache results from the providers in a
reusable fashion.

Signed-off-by: Danno Ferrin danno.ferrin@gmail.com

Changelog

Fetching the hash from the java security providers is often longer than
the hashing itself, so we should cache results from the providers in a
reusable fashion.

Signed-off-by: Danno Ferrin <danno.ferrin@gmail.com>
@shemnon
Copy link
Contributor Author

shemnon commented Mar 6, 2021

This will eliminate ~10% of the time wasted in mainnet block processing. More time was spent fetching the hash algo then actually executing it.

Comment on lines +34 to +41
private static final Supplier<MessageDigest> KECCAK256_SUPPLIER =
Suppliers.memoize(() -> messageDigest(KECCAK256_ALG));
private static final Supplier<MessageDigest> SHA256_SUPPLIER =
Suppliers.memoize(() -> messageDigest(SHA256_ALG));
private static final Supplier<MessageDigest> RIPEMD160_SUPPLIER =
Suppliers.memoize(() -> messageDigest(RIPEMD160_ALG));
private static final Supplier<MessageDigest> BLAKE2BF_SUPPLIER =
Suppliers.memoize(() -> messageDigest(BLAKE2BF_ALG));
Copy link
Contributor

Choose a reason for hiding this comment

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

How do you feel about just initializing these statically instead of using Suppliers?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The security providers may not be properly set up when the class initializes.

For example, the blake2f provider is loaded via besu code on startup, and since the algorithms are loaded by string reference the code that adds blake2f to the providers is not guaranteed to have run since there is no class file dependency. And this is the wrong place to do that provider setup.

@RatanRSur RatanRSur enabled auto-merge (squash) March 10, 2021 14:21
@RatanRSur RatanRSur merged commit bdf3163 into hyperledger:master Mar 10, 2021
RichardH92 pushed a commit to RichardH92/besu that referenced this pull request Mar 29, 2021
Fetching the hash from the java security providers is often longer than
the hashing itself, so we should cache results from the providers in a
reusable fashion.

Signed-off-by: Danno Ferrin <danno.ferrin@gmail.com>
Signed-off-by: Richard Hart <richardhart92@gmail.com>
@shemnon shemnon deleted the cacheDigest branch February 26, 2022 18:44
eum602 pushed a commit to lacchain/besu that referenced this pull request Nov 3, 2023
Fetching the hash from the java security providers is often longer than
the hashing itself, so we should cache results from the providers in a
reusable fashion.

Signed-off-by: Danno Ferrin <danno.ferrin@gmail.com>
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.

2 participants