Skip to content
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

Merged
merged 10 commits into from
May 14, 2024
Merged

Conversation

shuhuiluo
Copy link
Contributor

@shuhuiluo shuhuiluo commented Apr 9, 2024

Related Issue

Which issue does this pull request resolve?

Description of changes

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.

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.

@snreynolds snreynolds self-requested a review April 18, 2024 20:38
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.
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.
@shuhuiluo shuhuiluo marked this pull request as draft May 9, 2024 04:31
shuhuiluo added 3 commits May 11, 2024 02:08
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.
@shuhuiluo shuhuiluo marked this pull request as ready for review May 11, 2024 06:26
/// @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();
Copy link
Member

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?

Copy link
Contributor Author

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.

snreynolds
snreynolds previously approved these changes May 14, 2024
Copy link
Member

@snreynolds snreynolds left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@snreynolds snreynolds merged commit e427108 into Uniswap:main May 14, 2024
5 of 6 checks passed
@shuhuiluo shuhuiluo deleted the optimize-safecast branch May 14, 2024 18:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants