Skip to content

Commit

Permalink
Merge pull request #36 from Uxio0/master
Browse files Browse the repository at this point in the history
Just initialize auto backend once
  • Loading branch information
carver committed Jun 28, 2022
2 parents fc3b9dd + 49030b7 commit 3aa5237
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
11 changes: 7 additions & 4 deletions eth_hash/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,18 @@ def _hasher_first_run(self, in_data: Union[bytearray, bytes]) -> bytes:
This is a bit of a hacky way to minimize overhead on hash calls after
this first one.
"""
# Execute directly before saving method, to let any side-effects settle (see AutoBackend)
result = self._backend.keccak256(in_data)
new_hasher = self._backend.keccak256
assert new_hasher(b'') == b"\xc5\xd2F\x01\x86\xf7#<\x92~}\xb2\xdc\xc7\x03\xc0\xe5\x00\xb6S\xca\x82';\x7b\xfa\xd8\x04]\x85\xa4p" # noqa: E501
self.hasher = new_hasher
return new_hasher(in_data)
return result

def _preimage_first_run(self, in_data: Union[bytearray, bytes]) -> PreImageAPI:
new_preimage = self._backend.preimage
self.preimage = new_preimage
return new_preimage(in_data)
# Execute directly before saving method, to let any side-effects settle (see AutoBackend)
result = self._backend.preimage(in_data)
self.preimage = self._backend.preimage
return result

def __call__(self, preimage: Union[bytearray, bytes]) -> bytes:
if not isinstance(preimage, (bytearray, bytes)):
Expand Down
1 change: 1 addition & 0 deletions newsfragments/36.performance.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Keccak backend was initialized every time it was called. Now it's initialized only the first time it's called.

0 comments on commit 3aa5237

Please sign in to comment.