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] Add some class cache to StatusLabel #36652

Open
1 of 5 tasks
Nuranto opened this issue Dec 20, 2022 · 9 comments · May be fixed by #36716 or #36961
Open
1 of 5 tasks

[Performance] Add some class cache to StatusLabel #36652

Nuranto opened this issue Dec 20, 2022 · 9 comments · May be fixed by #36716 or #36961
Assignees
Labels
Area: Framework Issue: Confirmed Gate 3 Passed. Manual verification of the issue completed. Issue is confirmed Priority: P2 A defect with this priority could have functionality issues which are not to expectations. Progress: PR Created Indicates that Pull Request has been created to fix issue Progress: PR in progress Reported on 2.4.5-p1 Indicates original Magento version for the Issue report. Reproduced on 2.4.x The issue has been reproduced on latest 2.4-develop branch Triage: Dev.Experience Issue related to Developer Experience and needs help with Triage to Confirm or Reject it

Comments

@Nuranto
Copy link
Contributor

Nuranto commented Dec 20, 2022

Summary

Magento 2.4.5-p1

A DB call is made every time we call Magento\Sales\Model\Order\StatusLabel::getStatusFrontendLabel, even if we call it for the same status code.

Examples

$orderComments = $order->getStatusHistoryCollection(false);
foreach ($orderComments as $comment) {
     $comment->getStatusLabel()
}

If history contains 10 comments with "New" status and 10 comments with "Complete" status. 20 calls select sales_order_status.* from sales_order_status where (sales_order_status.status = ?) will occur instead of 2 (or 1...)

Let say you want to display history comments on an order list page so :

foreach($orders as $order) {
  $orderComments = $order->getStatusHistoryCollection(false);
  foreach ($orderComments as $comment) {
       $comment->getStatusLabel()
  }
}

That's 4000 calls to DB instead of 2 (or 1..). (I consider each order has same history as previous example to simplify the calculation/example)

Proposed solution

(pseudo code. The cache key will of course need to contain area and store)

  1. Add class property in StatusLabel class to store status Label if($this->labels[$code]) return $this->labels[$code]; else return $this->labels[$code] = loadLabelForCode(code);
  2. Add class property in StatusLabel class to store all status Labels if($this->labels[$code]) return $this->labels[$code]; else return $this->labels = loadLabels();

Solution 1 is calling DB only once per needed label.
Solution 2 is calling DB only once. Gives better performance in most cases, but not if the runtime only need one label.
Both solutions are far better than the existing one though.

See https://github.com/magento/magento2/blob/2.4-develop/app/code/Magento/Sales/Model/Order/StatusLabel.php#L57

Additional steps to reproduce

We have tried to reproduce this issue with the Magento latest development branch ie 2.4-develop with sample data installed. And the issue is reproducible for us. We have followed the below steps in order to reproduce the issue:

  1. Create 2 orders with Complete status for a user.
  2. Add a breakpoint on
    $select = $this->getConnection()->select()
  3. Go to orders page from storefront.
  4. Here breakpoint has been hit for 2 times for the same complete order status. It can be one time by using cache.

Release note

No response

Triage and priority

  • Severity: S0 - Affects critical data or functionality and leaves users without workaround.
  • Severity: S1 - Affects critical data or functionality and forces users to employ a workaround.
  • Severity: S2 - Affects non-critical data or functionality and forces users to employ a workaround.
  • Severity: S3 - Affects non-critical data or functionality and does not force users to employ a workaround.
  • Severity: S4 - Affects aesthetics, professional look and feel, “quality” or “usability”.
@Nuranto Nuranto added the Triage: Dev.Experience Issue related to Developer Experience and needs help with Triage to Confirm or Reject it label Dec 20, 2022
@m2-assistant
Copy link

m2-assistant bot commented Dec 20, 2022

Hi @Nuranto. Thank you for your report.
To speed up processing of this issue, make sure that you provided the following information:

  • Summary of the issue
  • Information on your environment
  • Steps to reproduce
  • Expected and actual results

Make sure that the issue is reproducible on the vanilla Magento instance following Steps to reproduce. To deploy vanilla Magento instance on our environment, Add a comment to the issue:

@magento give me 2.4-develop instance - upcoming 2.4.x release

For more details, review the Magento Contributor Assistant documentation.

Add a comment to assign the issue: @magento I am working on this

To learn more about issue processing workflow, refer to the Code Contributions.


⚠️ According to the Magento Contribution requirements, all issues must go through the Community Contributions Triage process. Community Contributions Triage is a public meeting.

🕙 You can find the schedule on the Magento Community Calendar page.

📞 The triage of issues happens in the queue order. If you want to speed up the delivery of your contribution, join the Community Contributions Triage session to discuss the appropriate ticket.

✏️ Feel free to post questions/proposals/feedback related to the Community Contributions Triage process to the corresponding Slack Channel

@faizan-shk
Copy link
Contributor

@magento give me 2.4-develop instance

@magento-deployment-service
Copy link

Hi @faizan-shk. Thank you for your request. I'm working on Magento instance for you.

@magento-deployment-service
Copy link

@engcom-Hotel engcom-Hotel added the Reported on 2.4.5-p1 Indicates original Magento version for the Issue report. label Dec 21, 2022
@engcom-Hotel engcom-Hotel self-assigned this Jan 3, 2023
@m2-assistant
Copy link

m2-assistant bot commented Jan 3, 2023

Hi @engcom-Hotel. Thank you for working on this issue.
In order to make sure that issue has enough information and ready for development, please read and check the following instruction: 👇

  • 1. Verify that issue has all the required information. (Preconditions, Steps to reproduce, Expected result, Actual result).

    DetailsIf the issue has a valid description, the label Issue: Format is valid will be added to the issue automatically. Please, edit issue description if needed, until label Issue: Format is valid appears.

  • 2. Verify that issue has a meaningful description and provides enough information to reproduce the issue. If the report is valid, add Issue: Clear Description label to the issue by yourself.

  • 3. Add Component: XXXXX label(s) to the ticket, indicating the components it may be related to.

  • 4. Verify that the issue is reproducible on 2.4-develop branch

    Details- Add the comment @magento give me 2.4-develop instance to deploy test instance on Magento infrastructure.
    - If the issue is reproducible on 2.4-develop branch, please, add the label Reproduced on 2.4.x.
    - If the issue is not reproducible, add your comment that issue is not reproducible and close the issue and stop verification process here!

  • 5. Add label Issue: Confirmed once verification is complete.

  • 6. Make sure that automatic system confirms that report has been added to the backlog.

@engcom-Hotel
Copy link
Contributor

Hello @Nuranto,

Thanks for the report and collaboration!

We have tried to reproduce this issue with the Magento latest development branch ie 2.4-develop with sample data installed. And the issue is reproducible for us. We have followed the below steps in order to reproduce the issue:

  1. Create 2 orders with Complete status for a user.
  2. Add a breakpoint on
    $select = $this->getConnection()->select()
  3. Go to orders page from storefront.
  4. Here breakpoint has been hit for 2 times for the same complete order status. It can be one time by using cache.

Hence confirming the issue.

Thanks

@engcom-Hotel engcom-Hotel added Issue: Confirmed Gate 3 Passed. Manual verification of the issue completed. Issue is confirmed Reproduced on 2.4.x The issue has been reproduced on latest 2.4-develop branch Priority: P2 A defect with this priority could have functionality issues which are not to expectations. Area: Framework labels Jan 3, 2023
@m2-community-project m2-community-project bot added this to Dev In Progress in High Priority Backlog Jan 3, 2023
@m2-community-project m2-community-project bot removed this from Ready for Confirmation in Issue Confirmation and Triage Board Jan 3, 2023
@m2-community-project m2-community-project bot moved this from Dev In Progress to Ready for Development in High Priority Backlog Jan 3, 2023
@m2-community-project m2-community-project bot moved this from Ready for Development to Dev In Progress in High Priority Backlog Jan 3, 2023
@github-jira-sync-bot
Copy link

✅ Jira issue https://jira.corp.adobe.com/browse/AC-7573 is successfully created for this GitHub issue.

@m2-assistant
Copy link

m2-assistant bot commented Jan 3, 2023

✅ Confirmed by @engcom-Hotel. Thank you for verifying the issue.
Issue Available: @engcom-Hotel, You will be automatically unassigned. Contributors/Maintainers can claim this issue to continue. To reclaim and continue work, reassign the ticket to yourself.

@m2-community-project m2-community-project bot moved this from Dev In Progress to Ready for Development in High Priority Backlog Jan 3, 2023
@rogerdz rogerdz linked a pull request Jan 9, 2023 that will close this issue
5 tasks
@m2-community-project m2-community-project bot moved this from Ready for Development to Pull Request In Progress in High Priority Backlog Jan 9, 2023
@Nuranto
Copy link
Contributor Author

Nuranto commented Jan 11, 2023

Just tested MR #36716 : thanks @rogerdz, but I'm afraid it does not solve the issue with my use case above.
What you're caching in your MR is already cached here :

public function getStoreLabels()
{
if ($this->hasData('store_labels')) {
return $this->_getData('store_labels');
}
$labels = $this->_getResource()->getStoreLabels($this);
$this->setData('store_labels', $labels);
return $labels;

My issue was not about queries on sales_order_status_label.

My issue is that I got a huge number of select calls on sales_order_status. That comes from Magento\Sales\Model\Order\StatusLabel::getStatusFrontendLabel()

The solution I proposed in my initial message was this :

--- Model/Order/StatusLabel.php	2023-01-11 16:50:45
+++ Model/Order/StatusLabel.php	2023-01-11 16:50:29
@@ -31,6 +31,8 @@
      */
     private $maskStatusesMapping;
 
+    private static $statusLabels = [];
+
     /**
      * @param StatusFactory $orderStatusFactory
      * @param State $state
@@ -56,14 +58,18 @@
      */
     public function getStatusFrontendLabel(?string $code, string $area, int $storeId = null): ?string
     {
-        $code = $this->maskStatusForArea($area, $code);
-        $status = $this->orderStatusFactory->create()->load($code);
+        $cacheKey = $code . $area . ($storeId??'0');
+        if (!isset(self::$statusLabels[$cacheKey])) {
+            $code = $this->maskStatusForArea($area, $code);
+            $status = $this->orderStatusFactory->create()->load($code);
 
-        if ($area === Area::AREA_ADMINHTML) {
-            return $status->getLabel();
+            if ($area === Area::AREA_ADMINHTML) {
+                self::$statusLabels[$cacheKey] = (string) $status->getLabel();
+            } else {
+                self::$statusLabels[$cacheKey] = (string) $status->getStoreLabel($storeId);
+            }
         }
-
-        return (string) $status->getStoreLabel($storeId);
+        return self::$statusLabels[$cacheKey];
     }
 
     /**

This one does solve the issue.

But after reflexion, maybe a better solution would be to create an order status repository with a registry, and refactor all calls to $this->orderStatusFactory to use the new repository instead ? That would be more elegant, and more effective since this factory is also used in Order\Config, which means a class-cache fix cannot be optimal for all cases.

Nuranto added a commit to webcooking/magento2 that referenced this issue Mar 3, 2023
@Nuranto Nuranto linked a pull request Mar 3, 2023 that will close this issue
5 tasks
Nuranto added a commit to webcooking/magento2 that referenced this issue Mar 3, 2023
@github-jira-sync-bot github-jira-sync-bot added Progress: PR Created Indicates that Pull Request has been created to fix issue and removed Progress: ready for grooming labels Aug 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: Framework Issue: Confirmed Gate 3 Passed. Manual verification of the issue completed. Issue is confirmed Priority: P2 A defect with this priority could have functionality issues which are not to expectations. Progress: PR Created Indicates that Pull Request has been created to fix issue Progress: PR in progress Reported on 2.4.5-p1 Indicates original Magento version for the Issue report. Reproduced on 2.4.x The issue has been reproduced on latest 2.4-develop branch Triage: Dev.Experience Issue related to Developer Experience and needs help with Triage to Confirm or Reject it
Projects
High Priority Backlog
  
Pull Request In Progress
5 participants