-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Optimize SafeCast
#576
Optimize SafeCast
#576
Conversation
In the `SafeCast` library, a new function `toUint128` has been added to support casting uint256 to uint128. Accompanying the update, a new fuzz test for `toUint128` is also added to verify the behavior when `toUint128` encounters a potential overflow condition. The test ensures its correct functionality and robustness against edge cases.
Refactor `SafeCast.sol` library using inline assembly for better memory-safe operations. By using inline assembly, the code optimizes casting operations which result in reduced gas usage. These changes affect numerous `.forge-snapshot` files, demonstrating an overall reduction in gas usage across multiple contracts.
16181ad
to
3cd89aa
Compare
The SafeCast library code has been refactored to increase readability. Specifically, repetition of in-line assembly code has been eliminated in favour of a more abstracted '_revertOverflow()' function. This not only simplifies multiple number casting functions but also helps reducing the bytecode size of the contract subtly.
The new function tests the casting method `toUint128` in the `SafeCast` library. It checks that the casting correctly handles 0, maximum uint128 values, and overflows.
The modification simplifies the SafeCast.sol functions to return types directly rather than using a separate variable. Changes have been made to parameter naming for better understanding. Additionally, an overflow check has been added to the toInt128 function for greater accuracy in casting data types.
/// @param x The uint256 to be downcasted | ||
/// @return The downcasted integer, now type uint160 | ||
function toUint160(uint256 x) internal pure returns (uint160) { | ||
if (x >= 1 << 160) _revertOverflow(); |
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.
technically 1<<160 and 1 << 128 can be constants which may make this even cheaper?
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.
The compiler would compute it to a constant at compile time. It increases readability imo.
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.
lgtm
Related Issue
Which issue does this pull request resolve?
Description of changes
In the
SafeCast
library, a new functiontoUint128
has been added to support casting uint256 to uint128. Accompanying the update, a new fuzz test fortoUint128
is also added to verify the behavior whentoUint128
encounters a potential overflow condition.Refactor
SafeCast.sol
library to produce optimal bytecode while keeping inline assembly to a minimal. As a result, the code optimizes casting operations which result in reduced gas usage. These changes affect numerous.forge-snapshot
files, demonstrating an overall reduction in gas usage across multiple contracts.@Vectorized Thanks for the inspiration in Vectorized/solady#928 and Vectorized/solady#929.