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 SqrtPriceMath.getAmount1Delta #676

Merged
merged 3 commits into from
May 21, 2024

Conversation

shuhuiluo
Copy link
Contributor

Factored out of #258.

Related Issue

Which issue does this pull request resolve?

Description of changes

Added a new function absDiff to SqrtPriceMath library which calculates the absolute difference between two numbers. Refactored the way price delta is computed between two prices in the getAmount1Delta function. The calculation is now more efficient.

Saves as much as 300 in runtime gas and 97 in bytecode size.

Added a new function `absDiff` to `SqrtPriceMath` library which calculates the absolute difference between two numbers. Refactored the way price delta is computed between two prices in the `getAmount1Delta` function. The calculation is now more efficient.
function absDiff(uint160 a, uint160 b) internal pure returns (uint256 res) {
assembly {
let diff := sub(a, b)
let mask := sar(255, diff)
Copy link
Contributor

Choose a reason for hiding this comment

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

could you add a similar comment to absTick? just something like
mask is all 1s if b > a or all 0s when a >= b
b-a = -(a-b) = invert (a-b-1)

@hensha256 hensha256 merged commit d5c21e7 into Uniswap:main May 21, 2024
5 of 6 checks passed
@shuhuiluo shuhuiluo deleted the get-amount-1-delta branch May 21, 2024 23:19
@hensha256 hensha256 mentioned this pull request Aug 30, 2024
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.

2 participants