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

[performance]: Don't use branched context for OI calculation if not needed #1668

Closed
wants to merge 1 commit into from

Conversation

BrendanChou
Copy link
Contributor

@BrendanChou BrendanChou commented Jun 11, 2024

Changelist

  • CacheContext() uses a significant amount of time on the profiler. Don't use it if not needed (i.e. the OI delta is zero)

Summary by CodeRabbit

  • Refactor
    • Improved logic for calculating new OIMF after an open interest increase to ensure more accurate updates.
    • Enhanced handling of open interest delta scenarios for better stability and reliability.

Copy link
Contributor

coderabbitai bot commented Jun 11, 2024

Walkthrough

The internalCanUpdateSubaccounts function in subaccount.go has been restructured to better handle the calculation of new Open Interest Maintenance Factor (OIMF) after an open interest increase. The changes include branching the state for OIMF calculation and adjusting the logic to apply open interest delta to perpetuals based on the updated open interest. Additionally, a new variable oiContext has been introduced for context management during these calculations.

Changes

File Path Change Summary
protocol/x/subaccounts/... Restructured internalCanUpdateSubaccounts to handle OIMF calculation after open interest increase. Added oiContext variable and updated function arguments for ModifyOpenInterest and internalGetNetCollateralAndMarginRequirements.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant SubaccountKeeper
    participant Context
    User ->> SubaccountKeeper: Request to update subaccounts
    SubaccountKeeper ->> Context: Create branched context
    SubaccountKeeper ->> Context: Calculate new OIMF
    SubaccountKeeper ->> Context: Apply open interest delta
    SubaccountKeeper ->> Context: Update perpetuals for IMF calculation
    Context ->> SubaccountKeeper: Return updated context
    SubaccountKeeper ->> User: Confirm update
Loading

Poem

In code we trust, the changes flow,
With oiContext, the context grows.
Open interest, now refined,
Perpetuals adjusted, all aligned.
Subaccounts dance in structured glee,
A rabbit's joy, in lines set free.
🌟✨


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 testing code 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 testing code 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 testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

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 an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @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.
  • Please see the configuration documentation for more information.
  • 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/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

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.

Actionable comments posted: 0

Outside diff range and nitpick comments (1)
protocol/x/subaccounts/keeper/subaccount.go (1)

Line range hint 691-713: Optimize the context management for Open Interest calculation.

The introduction of oiContext to manage the context more efficiently during Open Interest calculations is a significant improvement. However, consider handling the error returned by ctx.CacheContext() to ensure robustness. Ignoring errors can lead to unhandled exceptions or logical errors if the context creation fails.

- oiContext, _ := ctx.CacheContext()
+ oiContext, err := ctx.CacheContext()
+ if err != nil {
+     return false, nil, errorsmod.Wrapf(err, "failed to create cache context")
+ }
Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 8f0d3bc and 104ec4f.

Files selected for processing (1)
  • protocol/x/subaccounts/keeper/subaccount.go (2 hunks)

@BrendanChou
Copy link
Contributor Author

Removing as this might be breaking due to gas calculation costs

@BrendanChou BrendanChou deleted the bc/collat/context branch June 11, 2024 19:05
@BrendanChou BrendanChou restored the bc/collat/context branch June 11, 2024 19:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

1 participant