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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(advanced-logic): use currency manager instance #1268

Merged
merged 8 commits into from Nov 28, 2023

Conversation

kevindavee
Copy link
Contributor

Description of the changes

Context

Initiating a CurrencyManager is expensive. I was debugging the computeRequestId function. I'm sharing the logs of my test.

Screenshot 2023-11-28 at 12 09 16

Based on my test, computeRequestId is executed for 130ms. I'm using a M1 Macbook Pro. External network calls (API/DB calls) only take around 20ms (from verifying signature input until signed). So, we still have 110ms of processing time, and it's all local execution. I suspected two things: for loops or a computing-intensive operation such as signing/hashing/encrypting, etc. But apparently, the process that takes much time is CurrencyManager.getDefault(). You can see on the logs there are several .getDefault() calls, and it takes approximately 15ms. A total of 6 calls contribute 90ms from the 110ms local execution 馃ゲ

Change

Inject instantiated currencyManager. I think we should re-use the currencyManager that has been instantiated in the AdvancedLogic class and pass it down to the payment networks unless there are specific reasons why certain payment networks can't use the currencyManager from the AdvancedLogic class.

@kevindavee kevindavee changed the title feat(advanced-logic): use currency manager instance chore(advanced-logic): use currency manager instance Nov 28, 2023
Copy link
Contributor

@KolevDarko KolevDarko left a comment

Choose a reason for hiding this comment

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

Good idea, I see there are a lot of for cycles when preparing the currencies.

Another way would be to store a static field the CurrencyManager itself, that way you would modify just one file. But this also works.

@@ -2031,10 +2032,19 @@ describe('request-client.js', () => {
});

describe('allows overriding the default currencies', () => {
const currencyManager = new CurrencyManager(CurrencyManager.getDefaultList());
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think this test was inaccurate. Previously, the AddressBasedPaymentNetwork initialize it's own currency manager; hence the request can be created. Suppose that we only have _TEST currency in the currency manager.

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't understand what you mean? I don't think this was inacurate, why do you need to add ETH?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We need ETH to validate the address.

Copy link
Contributor

Choose a reason for hiding this comment

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

ah got it. It worked before because the default currency manager was instanciated?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yep. So the instantiated one contains _TEST currency only, and we have another currency manager that uses default list.

@coveralls
Copy link

coveralls commented Nov 28, 2023

Coverage Status

coverage: 87.089% (-0.009%) from 87.098%
when pulling beb5c1d on feat/use-injected-currency-manager
into fc215bf on master.

packages/advanced-logic/src/advanced-logic.ts Outdated Show resolved Hide resolved
Copy link
Contributor

Choose a reason for hiding this comment

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

strange. why is this empty?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, not sure 馃し

Copy link
Member

Choose a reason for hiding this comment

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

It's strange that this near-native.ts appears here in this PR. It lives in the near/ directory.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@@ -2031,10 +2032,19 @@ describe('request-client.js', () => {
});

describe('allows overriding the default currencies', () => {
const currencyManager = new CurrencyManager(CurrencyManager.getDefaultList());
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't understand what you mean? I don't think this was inacurate, why do you need to add ETH?

Copy link
Member

@alexandre-abrioux alexandre-abrioux left a comment

Choose a reason for hiding this comment

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

I agree with @KolevDarko , I think it would be easier to save the default instance after the first call to getDefault(), directly in the CurrencyManager like so:

export class CurrencyManager {

  private static defaultInstance: CurrencyManager;

  /**
   * Returns a default instance of CurrencyManager based on default lists
   */
  static getDefault(): CurrencyManager {
    if (this.defaultInstance) return this.defaultInstance;
    this.defaultInstance = new CurrencyManager(
      CurrencyManager.getDefaultList(),
      CurrencyManager.getDefaultLegacyTokens(),
    );
    return this.defaultInstance;
  }

}

This way, we don't have to change any other code. What do you think?

@kevindavee
Copy link
Contributor Author

That would work as well @alexandre-abrioux @KolevDarko . However, I still think we have inconsistency issues across payment networks (some are using injected currencies, some are not). And for another reason, to remove dependency with the currency package, as what Benji suggest.

@KolevDarko
Copy link
Contributor

That would work as well @alexandre-abrioux @KolevDarko . However, I still think we have inconsistency issues across payment networks (some are using injected currencies, some are not). And for another reason, to remove dependency with the currency package, as what Benji suggest.

Ok, that's a good benefit, to decouple the packages.

We could do both then, or at least add a comment in the getDefault method that it's heavy

@alexandre-abrioux
Copy link
Member

@KolevDarko @kevindavee I agree lets do both then

@kevindavee kevindavee merged commit 28cf4b6 into master Nov 28, 2023
25 of 26 checks passed
@kevindavee kevindavee deleted the feat/use-injected-currency-manager branch November 28, 2023 13:23
@benjlevesque
Copy link
Contributor

I've open #1269 for another (middle/long term) approach to this issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants