Skip to content

Optimize keccak() for raw bytes inputs#326

Open
sanjaysudagani wants to merge 2 commits into
ethereum:mainfrom
sanjaysudagani:codex/optimize-keccak-bytes-fastpath
Open

Optimize keccak() for raw bytes inputs#326
sanjaysudagani wants to merge 2 commits into
ethereum:mainfrom
sanjaysudagani:codex/optimize-keccak-bytes-fastpath

Conversation

@sanjaysudagani
Copy link
Copy Markdown

Summary

This adds a direct fast path in eth_utils.keccak() for raw bytes, bytearray, and memoryview inputs when no hexstr or text argument is provided.

Today those inputs always go through to_bytes(), which means the common raw-bytes case still pays the conversion-argument validation and dispatch cost before hashing.

What changed

  • add a raw-bytes fast path in eth_utils.crypto.keccak
  • preserve the existing conversion path for hexstr, text, ints, bools, and conflicting argument validation
  • add dedicated keccak() tests, including a regression test that verifies raw bytes input no longer calls to_bytes()

Validation

  • pytest tests/core/crypto-utils/test_crypto.py tests/core/conversion-utils/test_conversions.py -q
  • pre-commit run --files eth_utils/crypto.py tests/core/crypto-utils/test_crypto.py

Benchmark

On this machine, the common keccak(bytes) path went from about 2.90us per call to about 2.33us per call for a 32-byte payload, reducing overhead over the raw backend from about 29% to about 4%.

Closes #95

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.

to_bytes() adds significant overhead to keccak()

1 participant