Skip to content

Optimize SqrtPriceMath.getAmount1Delta#676

Merged
hensha256 merged 3 commits into
Uniswap:mainfrom
shuhuiluo:get-amount-1-delta
May 21, 2024
Merged

Optimize SqrtPriceMath.getAmount1Delta#676
hensha256 merged 3 commits into
Uniswap:mainfrom
shuhuiluo:get-amount-1-delta

Conversation

@shuhuiluo

Copy link
Copy Markdown
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
Copy Markdown
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
@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