8000 Optimize `price` calculation in `TickMath.getSqrtPriceAtTick` by shuhuiluo · Pull Request #664 · Uniswap/v4-core · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Optimize price calculation in TickMath.getSqrtPriceAtTick #664

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

Merged
merged 3 commits into from
May 21, 2024

Conversation

shuhuiluo
Copy link
Contributor

Part of #273.

Related Issue

Which issue does this pull request resolve?

Description of changes

The way the 'price' variable is calculated within the TickMath library has been significantly refactored. Previously, the variable was set through a ternary conditional statement. Now, a more intricate assembly logic is used, which involves shifting, AND operations, and masking. Apart from the improved readability, this change also aims to optimize the math operations for 'price' calculation.

@shuhuiluo shuhuiluo force-pushed the get-sqrt-ratio-at-tick-1 branch from d7a7d9f to 88d8f92 Compare May 20, 2024 20:37
shuhuiluo added 2 commits May 20, 2024 22:06
The way the 'price' variable is calculated within the TickMath library has been significantly refactored. Previously, the variable was set through a ternary conditional statement. Now, a more intricate assembly logic is used, which involves shifting, AND operations, and masking. Apart from the improved readability, this change also aims to optimize the math operations for 'price' calculation.
@shuhuiluo shuhuiluo force-pushed the get-sqrt-ratio-at-tick-1 branch from 88d8f92 to 9bebbae Compare May 21, 2024 02:08
// or price = int(2**128 / sqrt(1.0001)) if (absTick & 0x1) else 1 << 128
uint256 price;
assembly {
price := xor(shl(128, 1), mul(xor(shl(128, 1), 0xfffcb933bd6fad37aa2d162d1a594001), and(absTick, 0x1)))
Copy link
Contributor

Choose a reason for hiding this comment

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

does the optimiser deal with cutting down this line? theres lots of things it knows it advance
xor(shl(128, 1), 0xfffcb933bd6fad37aa2d162d1a594001)
shl(128, 1)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes. The compiler optimizer is able to reduce literal math expressions.

@hensha256 hensha256 merged commit 86532f8 into Uniswap:main May 21, 2024
5 of 6 checks passed
@shuhuiluo shuhuiluo deleted the get-sqrt-ratio-at-tick-1 branch May 21, 2024 23:45
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
0