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

Improve QuoteToBaseQuantums #1554

Merged
merged 3 commits into from
May 21, 2024
Merged

Improve QuoteToBaseQuantums #1554

merged 3 commits into from
May 21, 2024

Conversation

BrendanChou
Copy link
Contributor

@BrendanChou BrendanChou commented May 20, 2024

Changelist

  • Add benchmark for function
  • Improve function to not use big.Rat
  • Some performance gains expected but the main point is to move further away from using big.Rat anywhere

Test Plan

  • New benchmark, existing unit tests cover any possible regression
Before
BenchmarkQuoteToBaseQuantums-4   	 3567890	      1678 ns/op	     936 B/op	      42 allocs/op

After
BenchmarkQuoteToBaseQuantums-4   	14310079	       409.8 ns/op	     248 B/op	      14 allocs/op

Summary by CodeRabbit

  • Performance Improvements

    • Optimized the calculation logic in the QuoteToBaseQuantums function for better efficiency.
  • New Features

    • Added benchmarking for QuoteToBaseQuantums to ensure performance under various scenarios.

@BrendanChou BrendanChou requested a review from roy-dydx May 20, 2024 23:02
Copy link
Contributor

coderabbitai bot commented May 20, 2024

Walkthrough

The QuoteToBaseQuantums function in the lib package has been enhanced in quantums.go. The update involves optimizing the calculation logic by incorporating division by 10^(exponent) and priceValue, adjusting for positive and negative exponents. The implementation details have been refined while preserving the rounding behavior.

Changes

File Change Summary
protocol/lib/quantums_test.go Added BenchmarkQuoteToBaseQuantums to test QuoteToBaseQuantums with varied parameters.
protocol/lib/quantums.go Updated QuoteToBaseQuantums with optimized calculation logic, division adjustments, and refined implementation details. Return type changed to result *big.Int.

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.

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.
  • 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

Review Details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits Files that changed from the base of the PR and between 722ec24 and 522441f.
Files selected for processing (1)
  • protocol/lib/quantums_test.go (1 hunks)
Additional Context Used
Path-based Instructions (1)
protocol/lib/quantums_test.go (1)

Pattern **/**: Do not include a walkthrough. For all comments on line changes that are longer than 15 lines, add a prefix to the comment of what lines you are commenting on (for example: "The changes from lines 100 to 200...". Add your comment to be on the last 15 lines of the chunk rather than adding your comment to the entire chunk.

Additional comments not posted (1)
protocol/lib/quantums_test.go (1)

255-283: The new benchmark function BenchmarkQuoteToBaseQuantums is well-implemented and covers a variety of scenarios to ensure robustness. Good use of require.Equal to validate the results.

@@ -63,7 +63,7 @@ func BaseToQuoteQuantums(
// quoteQuantums / priceValue /
// 10^(priceExponent + baseCurrencyAtomicResolution - quoteCurrencyAtomicResolution)
//
// The result is rounded down.
// The result is rounded down. It is okay to do the divisions separately, as the result is the same.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

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

Review Details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits Files that changed from the base of the PR and between 522441f and 564a5ac.
Files selected for processing (1)
  • protocol/lib/quantums.go (2 hunks)
Additional Context Used
Path-based Instructions (1)
protocol/lib/quantums.go (1)

Pattern **/**: Do not include a walkthrough. For all comments on line changes that are longer than 15 lines, add a prefix to the comment of what lines you are commenting on (for example: "The changes from lines 100 to 200...". Add your comment to be on the last 15 lines of the chunk rather than adding your comment to the entire chunk.

Additional comments not posted (3)
protocol/lib/quantums.go (3)

Line range hint 66-94: Review of QuoteToBaseQuantums function changes.

The changes in the function QuoteToBaseQuantums align with the PR's objective to optimize performance by avoiding big.Rat. The logic for handling positive and negative exponents (lines 81-85) and the separate division by priceValue (line 88) are correctly implemented. Ensure that the new logic is covered by the new benchmark and existing unit tests to prevent regressions.


Line range hint 94-126: Review of multiplyByPrice function.

The function multiplyByPrice appears to be correctly implemented with proper handling of big rational operations and exponent adjustments. It is crucial to ensure that changes in QuoteToBaseQuantums do not affect the logic in multiplyByPrice, especially since it is used by other functions like FundingRateToIndex.


Line range hint 1-64: Ensure consistency in handling exponents across functions.

The function BaseToQuoteQuantums uses similar exponent handling logic. Verify that the changes in QuoteToBaseQuantums do not introduce inconsistencies in how exponents are managed across these functions. Consider aligning the handling of exponents and big integer operations in both functions to maintain uniformity and prevent logical errors.

Verification successful

The search results indicate that both BaseToQuoteQuantums and QuoteToBaseQuantums handle exponents in a similar manner. Specifically, both functions calculate the exponent as priceExponent + baseCurrencyAtomicResolution - QuoteCurrencyAtomicResolution and use similar logic for handling positive and negative exponents.

  • BaseToQuoteQuantums:

    • Calculates the exponent.
    • Checks if the exponent is zero.
    • Uses bigPow10Helper for exponentiation.
    • Multiplies or divides based on the sign of the exponent.
  • QuoteToBaseQuantums:

    • Calculates the exponent.
    • Uses BigPow10 for exponentiation.
    • Divides based on the sign of the exponent.

Given the consistency in handling exponents and big integer operations, there are no discrepancies between the two functions.

Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify consistency in exponent handling between `BaseToQuoteQuantums` and `QuoteToBaseQuantums`.

# Test: Search for exponent handling logic in both functions. Expect: Similar logic and handling.
rg --type go $'exponent' protocol/lib/quantums.go

Length of output: 693

@BrendanChou BrendanChou changed the title Improve QuoteToBaseQuantums Improve QuoteToBaseQuantums May 21, 2024
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

Review Details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits Files that changed from the base of the PR and between 564a5ac and 50d8459.
Files selected for processing (1)
  • protocol/lib/quantums.go (1 hunks)
Files skipped from review as they are similar to previous changes (1)
  • protocol/lib/quantums.go

@BrendanChou BrendanChou merged commit 4278dd3 into main May 21, 2024
18 checks passed
@BrendanChou BrendanChou deleted the bc/quote-to-base-quantums branch May 21, 2024 19:10
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.

2 participants