-
Notifications
You must be signed in to change notification settings - Fork 12.2k
Optimize _zeroBytes
function for gas efficiency
#5989
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
base: master
Are you sure you want to change the base?
Conversation
|
WalkthroughIn contracts/utils/math/Math.sol, a byte-by-byte loop that checked for non-zero values was replaced with an implementation that reads 32-byte words from memory first, then performs a tail check for remaining bytes. The function now returns false upon encountering any non-zero word and true otherwise. Functional behavior is unchanged. Pre-merge checks and finishing touches✅ Passed checks (3 passed)
✨ Finishing touches🧪 Generate unit tests (beta)
📜 Recent review detailsConfiguration used: CodeRabbit UI Review profile: CHILL Plan: Pro 📒 Files selected for processing (1)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (6)
🔇 Additional comments (1)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
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.
Should we reuse the Array.clz(bytes)
function and replace _zeroBytes(bytes)
check with Array.clz(buffer) == 8 * buffer.length
?
@Amxx Thanks for the suggestion! I wouldn’t switch to
If we want reuse elsewhere, we could add a small |
Description
This PR optimizes the
_zeroBytes
function incontracts/utils/math/Math.sol
to improve gas efficiency when checking for zero byte arrays. The current implementation processes bytes one-by-one, which is inefficient for large inputs.Key improvements:
mload
to read full 32-byte chunks instead of individual bytes