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

Directory currency - performance optimalization #1252

Merged

Conversation

Sekiphp
Copy link
Contributor

@Sekiphp Sekiphp commented Oct 7, 2020

Description (*)

Lowering count of SQL queries when someone use this methods:

  • getConfigAllowCurrencies
  • getConfigBaseCurrencies

Fix unique currency which is at the end of string for mthod getConfigAllowCurrencies.

Manual testing scenarios getConfigBaseCurrencies (*)

Call this code:

var_dump(Mage::getModel('directory/currency')->getConfigBaseCurrencies());
var_dump(Mage::getModel('directory/currency')->getConfigBaseCurrencies());

Database:
image

SQL queries before: 2
SQL queries after: 0
Output from method is in same format.

Manual testing scenarios getConfigAllowCurrencies (*)

**For this data in database: **
image

Call this code:

print_r(Mage::getModel('directory/currency')->getConfigAllowCurrencies());
print_r(Mage::getModel('directory/currency')->getConfigAllowCurrencies());

Output before changes:

Array
(
    [0] => BAM
    [2] => BGN
    [6] => CZK
    [9] => EUR
    [14] => HUF
    [15] => PLN
    [17] => PLN

    [18] => RON
    [20] => RSD
    [21] => RUB
    [22] => USD
)

Output afterchanges:

Array
(
    [0] => BAM
    [2] => BGN
    [6] => CZK
    [9] => EUR
    [14] => HUF
    [15] => PLN
    [18] => RON
    [20] => RSD
    [21] => RUB
    [22] => USD
)

Related SQL queries before:
image

Related SQL queries after:
image

Contribution checklist (*)

  • Pull request has a meaningful description of its purpose
  • All commits are accompanied by meaningful commit messages
  • All automated tests passed successfully (all builds are green)

@Sekiphp Sekiphp changed the title Caching all allowed currencies Directory currency - performance optimalization Oct 7, 2020
@github-actions github-actions bot added the Component: Directory Relates to Mage_Directory label Oct 7, 2020
@kiatng kiatng added the performance Performance related label Oct 8, 2020
FreeWall
FreeWall previously approved these changes Oct 8, 2020
Comment on lines 336 to 337
$defaultCurrencies = (string) Mage::app()->getConfig()->getNode(self::XML_PATH_CURRENCY_BASE, 'default');
return [$defaultCurrencies];
Copy link
Contributor

Choose a reason for hiding this comment

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

why not change only the Mage_Directory_Model_Resource_Currency::getConfigCurrencies to use config (which is cached) ?

Copy link
Member

Choose a reason for hiding this comment

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

Agreed, there doesn't seem to be any good reason to directly query the config table so getConfigCurrencies could simply be:

    /**
     * @deprecated
     */
    public function getConfigCurrencies($model, $path)
    {
        return (string) Mage::app()->getConfig()->getNode($path, 'default');
    }

Copy link
Contributor Author

Choose a reason for hiding this comment

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

you can't do this return explode(',', (string) Mage::app()->getConfig()->getNode($path, 'default'));, bcs. you can customize settings per website/store scope

@tmotyl
Copy link
Contributor

tmotyl commented Oct 8, 2020

This sounds like a nice improvement.
What scenarios should be tested before the merge?

  • product , cart, checkout flow
  • switching currency by selector (the same store view)
  • switching currency in code using simulated environment
  • checkout when base currency is different with current
  • what else?

@Sekiphp
Copy link
Contributor Author

Sekiphp commented Oct 9, 2020

@colinmollenhour @midlan

Or alternative solution (in my opinion us ugly, but complete without any additional SQL query):

    public function getConfigCurrencies($model, $path)
    {
        $result  = [];

        // default
        $result = array_merge($result, explode(',', trim(Mage::app()->getConfig()->getNode($path, 'default'))));

        // stores
        foreach (Mage::app()->getStores(true) as $store) {
            $result = array_merge($result, explode(',', trim(Mage::app()->getConfig()->getNode($path, 'stores', $store->getCode()))));
        }

        // websites
        foreach (Mage::app()->getWebsites(true) as $website) {
            $result = array_merge($result, explode(',', trim(Mage::app()->getConfig()->getNode($path, 'websites', $website->getCode()))));
        }

        sort($result);

        return array_unique($result);
    }

@midlan
Copy link
Contributor

midlan commented Oct 10, 2020

@Sekiphp I like it! I would only create variable for Mage::app()->getConfig()

@Sekiphp
Copy link
Contributor Author

Sekiphp commented Oct 10, 2020

@midlan Yeah, I can do it, it can improve readability of code

@Sekiphp
Copy link
Contributor Author

Sekiphp commented Oct 16, 2020

@midlan @colinmollenhour @tmotyl @FreeWall Final solution is here :-)

FreeWall
FreeWall previously approved these changes Oct 16, 2020
midlan
midlan previously approved these changes Oct 16, 2020
Copy link
Contributor

@midlan midlan left a comment

Choose a reason for hiding this comment

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

approved
worth to the note that the trim seems fixing an issue that occurred elsewhere (probably the config edit form)

@colinmollenhour
Copy link
Member

Very nice. May I suggest a few improvements?

  1. Use array_filter on the final result in case the config value is empty for one or more stores. The result of explode(',','') is [''] not [] so the empty string would end up as one of the elements in result.
  2. Cache the result in case the method is called multiple times in one process. (maybe it never is in which case don't bother)
  3. Skip inactive stores/websites unless there is a reason to keep currencies that are only active for disabled stores.

@Flyingmana Flyingmana changed the base branch from 1.9.4.x to 20.0 March 27, 2021 13:27
@Flyingmana Flyingmana dismissed stale reviews from midlan and FreeWall March 27, 2021 13:27

The base branch was changed.

@Flyingmana Flyingmana merged commit ca3d916 into OpenMage:20.0 Mar 27, 2021
@github-actions
Copy link
Contributor

Unit Test Results

1 files  ±0  1 suites  ±0   0s ⏱️ ±0s
0 tests ±0  0 ✔️ ±0  0 💤 ±0  0 ❌ ±0 
2 runs  ±0  2 ✔️ ±0  0 💤 ±0  0 ❌ ±0 

Results for commit ca3d916. ± Comparison against base commit a4b82bf.

@Sekiphp
Copy link
Contributor Author

Sekiphp commented Apr 7, 2021

@colinmollenhour Can you create issue for your suggestions?

@Sekiphp Sekiphp deleted the performance/directory-default-currencies branch April 7, 2021 07:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Component: Directory Relates to Mage_Directory hacktoberfest-accepted performance Performance related
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants