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

Make CL calculations not update accumulators #7689

Merged
merged 4 commits into from
Mar 13, 2024

Conversation

ValarDragon
Copy link
Member

@ValarDragon ValarDragon commented Mar 7, 2024

Make CL CalcOutAmtGivenIn and CalcInAmtGivenOut skip fetching and setting the accumulators. This saves notably on the CPU and I/O time at hand here. This should also help smart contracts.

At our last benchmark, this should be saving .75% of sync speed, but is of much higher impact with all our v24 work conjoined. (sync speed saving all coming from protorev) This also paves the way for a second PR removing the CacheCtx on CalcOutAmtGivenIn which would save another 1% at current benchmarks. (Also from protorev)

Summary by CodeRabbit

  • New Features

    • Improved speed and gas efficiency in CL price estimations by preventing unnecessary state writes.
  • Refactor

    • Updated calculation functions in concentrated liquidity to support conditional accumulator updates, enhancing operational efficiency.

Comment on lines 665 to +666
spreadRewardAccum *accum.AccumulatorObject, uptimeAccums *[]*accum.AccumulatorObject,
tokenInDenom string) (SwapState, error) {
tokenInDenom string, updateAccumulators bool) (SwapState, error) {
Copy link
Member Author

Choose a reason for hiding this comment

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

in a subsequent PR I think I'll package these accum vars into their own struct.

return swapState, err
// Retrieve the liquidity held in the next closest initialized tick
spreadRewardGrowth := sdk.DecCoin{Denom: tokenInDenom, Amount: swapState.globalSpreadRewardGrowthPerUnitLiquidity}
_, err := k.crossTick(ctx, p.GetId(), nextInitializedTick, &nextInitializedTickInfo, spreadRewardGrowth, spreadRewardAccum.GetValue(), *uptimeAccums)
Copy link
Member Author

Choose a reason for hiding this comment

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

In a subsequent PR I'll refactor crossTick to not return LiquidityNet, it just reads that from one of the args.

Copy link
Member

@czarcas7ic czarcas7ic left a comment

Choose a reason for hiding this comment

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

Nice find!

@czarcas7ic
Copy link
Member

This should have a changelog

@p0mvn p0mvn closed this Mar 12, 2024
@p0mvn p0mvn reopened this Mar 12, 2024
Copy link
Member

@p0mvn p0mvn left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Contributor

coderabbitai bot commented Mar 13, 2024

Walkthrough

The recent updates introduce enhancements focusing on speed and gas efficiency for the Osmosis project. A notable feature prevents state writes during CL price estimations, optimizing performance. Modifications in the Keeper struct and swap-related functions incorporate a new parameter to control accumulator updates during calculations. These changes aim to improve the flexibility and efficiency of liquidity swaps and price computations within the concentrated liquidity module.

Changes

File(s) Summary
CHANGELOG.md Added feature to improve speed and gas efficiency by preventing state writes during CL price estimations.
x/concentrated-liquidity/export_test.go, x/concentrated-liquidity/swaps_test.go Updated functions to accept an additional boolean parameter updateAccumulators to control the updating of accumulators.
x/concentrated-liquidity/swaps.go Introduced a new parameter getAccumulators in swapSetup affecting various swap calculations and conditional retrieval of spread accumulators.

🐇✨
In the land of code and byte,
A rabbit hopped, a sight so bright.
With each leap, efficiency found,
In swaps and states, improvements abound.
"To faster realms, we now proceed,"
Whispered the rabbit, as code took lead.
🚀🌌

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share

Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit-tests for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit tests for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit tests.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • The JSON schema for the configuration file is available here.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/coderabbit-overrides.v2.json

CodeRabbit Discord Community

Join our Discord Community to get help, request features, and share feedback.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Review Status

Actionable comments generated: 1

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between a51040c and 4515abd.
Files selected for processing (4)
  • CHANGELOG.md (1 hunks)
  • x/concentrated-liquidity/export_test.go (2 hunks)
  • x/concentrated-liquidity/swaps.go (14 hunks)
  • x/concentrated-liquidity/swaps_test.go (4 hunks)
Check Runs (9)
osmosisd-linux-arm64 completed (1)
osmosisd-darwin-arm64 completed (1)
osmosisd-linux-amd64 completed (1)
get_diff completed (2)
Run golangci-lint completed (1)
osmosisd-darwin-amd64 completed (1)
test completed (1)
Run super-linter completed (1)
Check Actions completed (2)
Additional comments: 11
x/concentrated-liquidity/export_test.go (2)
  • 73-75: The addition of the updateAccumulators boolean parameter to the ComputeOutAmtGivenIn function signature is aligned with the PR objectives to optimize performance by conditionally updating accumulators. This change allows for bypassing accumulator updates when not necessary, potentially reducing CPU and I/O time. Ensure that all calls to this function have been updated to include the new parameter.
  • 96-98: Similarly, the ComputeInAmtGivenOut function now accepts an updateAccumulators boolean parameter, enabling the same performance optimization as ComputeOutAmtGivenIn. This consistent approach across both functions is beneficial for maintaining the efficiency of the system. As with the previous function, verify that all invocations of ComputeInAmtGivenOut have been adjusted to pass the new parameter.
x/concentrated-liquidity/swaps.go (6)
  • 253-253: The introduction of the updateAccumulators parameter in computeOutAmtGivenIn and computeInAmtGivenOut functions is a significant change aimed at optimizing performance by conditionally updating accumulators. This aligns with the PR's objective to enhance efficiency by reducing unnecessary operations.
  • 338-349: Adding the getAccumulators parameter to the swapSetup function is a strategic change that enables conditional retrieval of spread accumulators. This is a crucial optimization step that contributes to the overall goal of improving synchronization speed and reducing CPU and I/O time.
  • 441-447: The conditional update of spread reward growth within the computeOutAmtGivenIn function, controlled by the updateAccumulators flag, is a thoughtful addition. It ensures that spread reward growth is only updated when necessary, potentially reducing the computational overhead. However, it's important to ensure that this conditional logic does not inadvertently skip necessary updates in scenarios where accumulators should be updated.
  • 498-501: The conditional addition of spread reward growth to the pool-global spread reward accumulator in computeOutAmtGivenIn is another optimization that aligns with the PR's objectives. This selective updating mechanism can contribute to performance improvements by avoiding unnecessary state changes.
  • 576-581: Similar to the computeOutAmtGivenIn function, the conditional update of spread reward growth in computeInAmtGivenOut based on the updateAccumulators flag is a prudent optimization. It's essential to ensure that this conditional logic is thoroughly tested to confirm that it behaves as expected across all relevant scenarios.
  • 629-631: The conditional addition of spread reward growth to the pool-global spread reward accumulator in computeInAmtGivenOut is consistent with the PR's goal of optimizing performance. This selective approach to updating the accumulator should be beneficial, provided it's correctly implemented to cover all necessary cases.
CHANGELOG.md (1)
x/concentrated-liquidity/swaps_test.go (2)
  • 2030-2030: Please verify the correctness of the line number reference 2030~ in the comment. It seems to be placed as a placeholder or error. Ensure that the correct line number or code reference is provided to avoid confusion.
  • 2759-2759: Please verify the correctness of the line number reference 2759~ in the comment. It seems to be placed as a placeholder or error. Ensure that the correct line number or code reference is provided to avoid confusion.

Comment on lines 2027 to 2033
cacheCtx,
pool.GetId(),
test.TokenIn, test.TokenOutDenom,
test.SpreadFactor, test.PriceLimit)
test.SpreadFactor, test.PriceLimit, true)

if test.ExpectErr {
s.Require().Error(err)
Copy link
Contributor

Choose a reason for hiding this comment

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

📝 NOTE
This review was outside the diff hunks, and no overlapping diff hunk was found. Original lines [203-203]

The function populateSwapTestFields modifies the test cases in place to ensure that certain fields are not nil. Consider adding a comment to clarify the purpose of this function and why it's necessary to modify the test cases in this manner. This will improve the readability and maintainability of the test suite.

+ // populateSwapTestFields ensures that all test cases have non-nil expected values for certain fields.

@ValarDragon ValarDragon merged commit 9f8d711 into main Mar 13, 2024
1 check passed
@ValarDragon ValarDragon deleted the dev/make_cl_calcs_not_update_ticks branch March 13, 2024 09:04
@github-actions github-actions bot mentioned this pull request May 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants