Skip to content

Conversation

@senthilengg
Copy link

@senthilengg senthilengg commented Oct 4, 2025

Description (*)

  1. Moving $this->getEntity()->getEntityIdField(); lookup outside of the foreach
  2. Processing the result of select query and preparing [product_id => [attribute_code => value, attribute_code1 => value1]]
  3. Calling _setItemAttributeValue only once per $selectGroups instead of per $selectGroups, per attribute and per product
  4. Return same response as the original function

Fixed Issues (if relevant)

  1. Fixes magento/magento2 Time complexity of _loadAttributes in Eav\Model\Entity\Collection\AbstractCollection increases with number of products in cart and attributes #40216

Manual testing scenarios (*)

  1. Check all areas thats hitting the EAV

Result

Without this PR - Too many calls to _setItemAttributeValue function
image

With PR - Only one call to _setItemAttributeValue function
image

Additional Questions

Since array_replace is memory intensive for large array would it benefit over all or not ?
array union (+) seems to be memory optimised and used here

@m2-assistant
Copy link

m2-assistant bot commented Oct 4, 2025

Hi @senthilengg. Thank you for your contribution!
Here are some useful tips on how you can test your changes using Magento test environment.
❗ Automated tests can be triggered manually with an appropriate comment:

  • @magento run all tests - run or re-run all required tests against the PR changes
  • @magento run <test-build(s)> - run or re-run specific test build(s)
    For example: @magento run Unit Tests

<test-build(s)> is a comma-separated list of build names.

Allowed build names are:
  1. Database Compare
  2. Functional Tests CE
  3. Functional Tests EE
  4. Functional Tests B2B
  5. Integration Tests
  6. Magento Health Index
  7. Sample Data Tests CE
  8. Sample Data Tests EE
  9. Sample Data Tests B2B
  10. Static Tests
  11. Unit Tests
  12. WebAPI Tests
  13. Semantic Version Checker

You can find more information about the builds here
ℹ️ Run only required test builds during development. Run all test builds before sending your pull request for review.


For more details, review the Code Contributions documentation.
Join Magento Community Engineering Slack and ask your questions in #github channel.

@magento-automated-testing
Copy link

Failed to run the builds. Please try to re-run them later.

@senthilengg
Copy link
Author

@magento run unit Tests

@senthilengg
Copy link
Author

@magento run Database Compare,Functional Tests CE,Functional Tests EE,Functional Tests B2B,Integration Tests,Magento Health Index,Sample Data Tests CE,Sample Data Tests EE,Sample Data Tests B2B,Static Tests,WebAPI Tests,Semantic Version Checker

@senthilengg senthilengg changed the title #40216-Replacing nested foreach with array_replace to reduce time complexity #40216-Replacing nested foreach with array_union to reduce time complexity Oct 4, 2025
@senthilengg
Copy link
Author

@magento run Functional Tests CE,Static Tests,WebAPI Tests

@senthilengg senthilengg changed the title #40216-Replacing nested foreach with array_union to reduce time complexity #40216-Replacing nested foreach with array union (+) to reduce time complexity Oct 4, 2025
@senthilengg
Copy link
Author

@magento run Functional Tests EE,Functional Tests B2B,Integration Tests

@senthilengg
Copy link
Author

@magento run Functional Tests CE

@senthilengg
Copy link
Author

@magento run WebAPI Tests

@engcom-Hotel engcom-Hotel added the Priority: P2 A defect with this priority could have functionality issues which are not to expectations. label Oct 7, 2025
@github-project-automation github-project-automation bot moved this to Pending Review in Pull Requests Dashboard Oct 7, 2025
@senthilengg
Copy link
Author

@magento run WebAPI Tests

@senthilengg
Copy link
Author

@magento run WebAPI Tests

@senthilengg
Copy link
Author

@magento run all tests

@engcom-Hotel
Copy link
Contributor

@magento run all tests

@engcom-Hotel engcom-Hotel self-requested a review October 14, 2025 06:42
@engcom-Hotel engcom-Hotel moved this from Pending Review to Review in Progress in Pull Requests Dashboard Oct 14, 2025
* @return $this
* @throws LocalizedException
*/
protected function _setItemAttributeValue($valueInfo)
Copy link
Contributor

Choose a reason for hiding this comment

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

Hello @senthilengg,

Thanks for your reply!

but I am still thinks that this is the BiC change. Please go through with the below link:

https://developer.adobe.com/commerce/contributor/guides/code-contributions/backward-compatibility-policy/#modifying-the-default-values-of-optional-arguments-in-public-and-protected-methods

and let me know If I missed anything here.

__('A header row is missing for an attribute. Verify the header row and try again.')
);
}
// Run only once
Copy link
Contributor

Choose a reason for hiding this comment

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

Thank you for your analysis @senthilengg. I agrees with your point. But I have suggestion below to change the comment as below:

// _itemsById[$entityId] is always an array (typically with one element)
// Foreach handles edge cases where multiple objects share the same entity ID

@senthilengg
Copy link
Author

@magento run Unit Tests, WebAPI Tests, Static Tests

@engcom-Hotel
Copy link
Contributor

@magento run all tests

Copy link
Contributor

@engcom-Hotel engcom-Hotel left a comment

Choose a reason for hiding this comment

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

Thank you @senthilengg for making the changes.

Failed tests seems flaky to me, hence approving this PR.

@ct-prd-projects-boards-automation ct-prd-projects-boards-automation bot moved this from Review in Progress to Ready for Testing in Community Dashboard Oct 16, 2025
@engcom-Bravo engcom-Bravo added the Triage: Dev.Experience Issue related to Developer Experience and needs help with Triage to Confirm or Reject it label Oct 16, 2025
@engcom-Dash
Copy link
Contributor

@magento run all tests

@engcom-Dash
Copy link
Contributor

Hi @senthilengg
Thanks for the collaboration & contribution!

✔️ QA Passed

Preconditions:
Install fresh Magento 2.4-develop

Manual testing scenarios

  1. Create 3 attributes with type text-field.
  2. Create attribute-set and assign above attributes to this attribute-set.
  3. Create 3 products with the same attribute-set created above.
  4. Add all the products to the cart from frontend.
  5. Access the shopping cart page.
  6. Check how many times _setItemAttributeValue function is called

Before: ✖️
image

Count 32

After: ✔️image

Count 1

Builds are failed. Hence, moving this PR to Extended Testing.

Thanks.

@engcom-Dash engcom-Dash moved this from Testing in Progress to Extended testing (optional) in Community Dashboard Oct 24, 2025
@engcom-Dash
Copy link
Contributor

@magento run Functional Tests B2B, Functional Tests EE, Static Tests

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Priority: P2 A defect with this priority could have functionality issues which are not to expectations. Progress: testing in progress Project: Community Picked PRs upvoted by the community Triage: Dev.Experience Issue related to Developer Experience and needs help with Triage to Confirm or Reject it

Projects

Status: Extended testing (optional)

Development

Successfully merging this pull request may close these issues.

Time complexity of _loadAttributes in Eav\Model\Entity\Collection\AbstractCollection increases with number of products in cart and attributes

5 participants