-
Couldn't load subscription status.
- Fork 1
Do not use go-ethereum for hasher. #159
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
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
dd2e1b8
Do not use go-ethereum for hasher.
winder a5f9e2b
Fix lint
winder 1cc84fa
Avoid panic
winder 1004434
No need to call reset twice.
winder 085ba4d
Add benchmark for hashing with and without object pooling.
winder cdd33ec
Fix lint
winder 977f9c4
tidy
winder 134e63f
???
winder f87fcce
Merge branch 'main' into will/no-evm-hasher
winder File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,30 @@ | ||
| package protocol | ||
|
|
||
| import "github.com/ethereum/go-ethereum/crypto" | ||
| import ( | ||
| "hash" | ||
| "sync" | ||
|
|
||
| "golang.org/x/crypto/sha3" | ||
| ) | ||
|
|
||
| var hasherPool = sync.Pool{ | ||
| New: func() any { | ||
| return sha3.NewLegacyKeccak256() | ||
| }, | ||
| } | ||
|
|
||
| // Keccak256 computes the Keccak256 hash of the input. | ||
| func Keccak256(data []byte) [32]byte { | ||
| hash := crypto.Keccak256(data) | ||
| var result [32]byte | ||
| copy(result[:], hash) | ||
| return result | ||
| h, ok := hasherPool.Get().(hash.Hash) | ||
| if !ok { | ||
| // This should never happen, but just in case. | ||
| h = sha3.NewLegacyKeccak256() | ||
| } | ||
|
|
||
| h.Reset() | ||
| h.Write(data) //nolint // keccak256 never returns an error | ||
| var out [32]byte | ||
| copy(out[:], h.Sum(nil)) | ||
| hasherPool.Put(h) | ||
| return out | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| package protocol | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "golang.org/x/crypto/sha3" | ||
| ) | ||
|
|
||
| var data = []byte("The quick brown fox jumps over the lazy dogThe quick brown fox jumps over the lazy dogThe quick brown fox jumps over the lazy dogThe quick brown fox jumps over the lazy dogThe quick brown fox jumps over the lazy dogThe quick brown fox jumps over the lazy dog") | ||
|
|
||
| func BenchmarkHashing(b *testing.B) { | ||
| for i := 0; i < b.N; i++ { | ||
| Keccak256(data) | ||
| } | ||
| } | ||
|
|
||
| func BenchmarkHashinbBaseline(b *testing.B) { | ||
| for i := 0; i < b.N; i++ { | ||
| h := sha3.NewLegacyKeccak256() | ||
| h.Write(data) | ||
| var out [32]byte | ||
| copy(out[:], h.Sum(nil)) | ||
| hasherPool.Put(h) | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is this needed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It lets you reuse the keccak objects instead of reallocating them every time. I'll add a benchmark, now I'm curious how impactful it is.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like object pooling improves things from 378ns -> 251.2ns. I tested with different inputs, and it seems to be a fixed ~100ns overhead for object allocation with the total duration going up based on the input data size.