-
Notifications
You must be signed in to change notification settings - Fork 67
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 logic and reduce alloc #17
base: feat/uint256
Are you sure you want to change the base?
Conversation
} | ||
} | ||
|
||
state.sqrtPriceX96, step.amountIn, step.amountOut, step.feeAmount, err = utils.ComputeSwapStep(state.sqrtPriceX96, targetValue, state.liquidity, state.amountSpecifiedRemaining, p.Fee) | ||
var nxtSqrtPriceX96 utils.Uint160 |
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.
nxt typo?
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.
um I don't get your question, it's for the price in next loop so I named it like that
I'm reviewing your PR, but please give me some time to understand it. |
|
||
// result=floor(a×b÷denominator), remainder=a×b%denominator | ||
// (pass remainder=nil if not required) | ||
func MulDivV2(a, b, denominator, result, remainder *uint256.Int) error { |
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.
It was kind of difficult for me to realize that remainder
is an optimization to skip re-calculating mulmod(a, b, denominator)
from the caller, especially when the mulDiv
function in UniswapV3 contract doesn't return this value.
I would really appreciate it if we have a few comments to explain this optimization.
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.
Really sorry for that! I'll add comment here and update part 1 PR description as well, please let me know if there is anything unclear there, thanks!
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.
Beside changes related to move from *big.Int
into uint256
/int256
, I see that most of the changes are pass the result into the function as pointer, to init variable in stack/reduce memory allocation in heap (?). So for me, this PR is not something with a lot of breaking changes.
Memory allocation also reduces by 10x after the improvement (I ran some tests myself). So yeah, I think this PR is good to go!
Thanks a lot, man! |
GetOutputAmountV2
to return only necessary data (without calling NewPool again)