-
Notifications
You must be signed in to change notification settings - Fork 83
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
chore(currency): search by ID from Map #1465
Conversation
WalkthroughThe changes introduce a new private property, Changes
Sequence Diagram(s)sequenceDiagram
participant Client
participant CurrencyManager
participant Cache
Client->>CurrencyManager: fromId("USDC-multichain-moonbeam")
CurrencyManager->>Cache: Check knownCurrenciesById
alt Currency found in cache
Cache-->>CurrencyManager: Return CurrencyDefinition
CurrencyManager-->>Client: Return CurrencyDefinition
else Currency not found
CurrencyManager->>CurrencyManager: Search knownCurrencies
CurrencyManager->>Cache: Cache CurrencyDefinition
CurrencyManager-->>Client: Return CurrencyDefinition
end
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? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
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 using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
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.
Actionable comments posted: 3
📜 Review details
Configuration used: CodeRabbit UI
Review profile: ASSERTIVE
📒 Files selected for processing (2)
- packages/currency/src/currencyManager.ts (2 hunks)
- packages/currency/test/currencyManager.test.ts (1 hunks)
🔇 Additional comments (1)
packages/currency/src/currencyManager.ts (1)
23-25
: Implementation of caching mechanism looks goodThe addition of
knownCurrenciesById
Map initializes correctly.
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.
Actionable comments posted: 1
📜 Review details
Configuration used: CodeRabbit UI
Review profile: ASSERTIVE
📒 Files selected for processing (2)
- packages/currency/src/currencyManager.ts (3 hunks)
- packages/currency/test/currencyManager.test.ts (1 hunks)
🔇 Additional comments (5)
packages/currency/src/currencyManager.ts (4)
23-24
: LGTM: New property for caching currency definitionsThe addition of the
knownCurrenciesById
property as aMap
is a good choice for caching currency definitions by their IDs. This will allow for O(1) lookup time, addressing the performance issue mentioned in the PR objectives.
47-49
: LGTM: Efficient initialization ofknownCurrenciesById
The initialization of
knownCurrenciesById
in the constructor is implemented efficiently, using a single pass overknownCurrencies
. This approach ensures that the cache is fully populated at construction time, providing immediate O(1) lookups for all known currencies. This change effectively addresses the suggestion from the past review comment.
93-93
: LGTM: OptimizedfromId
method for O(1) lookupThe
fromId
method has been simplified to directly use theknownCurrenciesById
Map, resulting in O(1) lookup time. This change significantly improves the method's performance, especially when handling multiple currency lookups, as mentioned in the PR objectives. The elimination of the fallback linear search is appropriate since the cache is now pre-populated in the constructor.
23-24
: LGTM: Effective implementation of caching for improved performanceThe changes introduced in this PR successfully implement a caching mechanism for currency definitions using a Map. The cache is efficiently populated at construction time and utilized in the
fromId
method, resulting in O(1) lookup time for currency definitions. These modifications effectively address the performance issue described in the PR objectives, particularly for scenarios involving multiple currency lookups.The implementation is well-structured, maintains the integrity of the
CurrencyManager
class, and addresses the suggestions from the past review comment. Overall, these changes should significantly improve the efficiency of currency-related operations in the system.Also applies to: 47-49, 93-93
packages/currency/test/currencyManager.test.ts (1)
156-161
: Good addition to the test suite.This new test case enhances the coverage of the
fromId
method and aligns well with the PR objectives of implementing caching for the.fromId
results. It integrates seamlessly into the existing test structure without disrupting other tests.As the caching functionality evolves, consider adding more comprehensive tests for various caching scenarios, such as cache expiration (if implemented) or cache invalidation.
Reviewing... 🚧 |
Description of the changes
Search currency with
.fromId
results from aMap
.Since Node.js is a single thread, we couldn't loop through an array in parallel.
In the above example, even though we use
Promise.all
, the whole operation will take (200* 20ms) + 10ms = 4010ms. The async task could be executed concurrently, but the array iteration runs serially. If we cache the result in aMap
, we could get the data withO(1)
time complexity.Summary by CodeRabbit