-
Notifications
You must be signed in to change notification settings - Fork 9.4k
#40216-Replacing nested foreach with array union (+) to reduce time complexity #40217
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
base: 2.4-develop
Are you sure you want to change the base?
Changes from 8 commits
2c3a2c9
f3f5bc9
85c13c6
f8d70b6
1ce65b5
072db63
ab42d68
48b92ff
189dd72
48c2f62
5062fef
409afc2
5399f76
cd2251d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
| @@ -1,7 +1,7 @@ | ||||
| <?php | ||||
| /** | ||||
| * Copyright © Magento, Inc. All rights reserved. | ||||
| * See COPYING.txt for license details. | ||||
| * Copyright 2025 Adobe | ||||
| * All Rights Reserved. | ||||
| */ | ||||
|
|
||||
| namespace Magento\Eav\Model\Entity\Collection; | ||||
|
|
@@ -1233,8 +1233,27 @@ public function _loadAttributes($printQuery = false, $logQuery = false) | |||
| throw $e; | ||||
| } | ||||
|
|
||||
| $attributeCode = $data = []; | ||||
| $entityIdField = $entity->getEntityIdField(); | ||||
|
|
||||
| foreach ($values as $value) { | ||||
| $this->_setItemAttributeValue($value); | ||||
| $entityId = $value[$entityIdField]; | ||||
| $attributeId = $value['attribute_id']; | ||||
| if (!isset($attributeCode[$attributeId])) { | ||||
| $attributeCode[$attributeId] = array_search($attributeId, $this->_selectAttributes); | ||||
| if (!$attributeCode[$attributeId]) { | ||||
| $attribute = $this->_eavConfig->getAttribute( | ||||
| $this->getEntity()->getType(), | ||||
| $attributeId | ||||
| ); | ||||
| $attributeCode[$attributeId] = $attribute->getAttributeCode(); | ||||
| } | ||||
| } | ||||
| $data[$entityId][$attributeCode[$attributeId]] = $value['value']; | ||||
| } | ||||
|
|
||||
| if ($data) { | ||||
| $this->_setItemAttributeValue($data); | ||||
| } | ||||
| } | ||||
| } | ||||
|
|
@@ -1303,32 +1322,24 @@ protected function _addLoadAttributesSelectValues($select, $table, $type) | |||
| /** | ||||
| * Initialize entity object property value | ||||
| * | ||||
| * Parameter $valueInfo is _getLoadAttributesSelect fetch result row | ||||
| * Parameter $valueInfo is [product_id => [attribute_code => value]] | ||||
| * | ||||
| * @param array $valueInfo | ||||
| * @return $this | ||||
| * @throws LocalizedException | ||||
| */ | ||||
| protected function _setItemAttributeValue($valueInfo) | ||||
| { | ||||
| $entityIdField = $this->getEntity()->getEntityIdField(); | ||||
| $entityId = $valueInfo[$entityIdField]; | ||||
| if (!isset($this->_itemsById[$entityId])) { | ||||
| throw new LocalizedException( | ||||
| __('A header row is missing for an attribute. Verify the header row and try again.') | ||||
| ); | ||||
| } | ||||
| $attributeCode = array_search($valueInfo['attribute_id'], $this->_selectAttributes); | ||||
| if (!$attributeCode) { | ||||
| $attribute = $this->_eavConfig->getAttribute( | ||||
| $this->getEntity()->getType(), | ||||
| $valueInfo['attribute_id'] | ||||
| ); | ||||
| $attributeCode = $attribute->getAttributeCode(); | ||||
| } | ||||
|
|
||||
| foreach ($this->_itemsById[$entityId] as $object) { | ||||
| $object->setData($attributeCode, $valueInfo['value']); | ||||
| foreach ($valueInfo as $entityId => $value) { | ||||
| if (!isset($this->_itemsById[$entityId])) { | ||||
| throw new LocalizedException( | ||||
| __('A header row is missing for an attribute. Verify the header row and try again.') | ||||
| ); | ||||
| } | ||||
| // Run only once | ||||
|
||||
| if (isset($this->_itemsById[$object->getId()])) { |
$object->getId() here being the entity ID from the DB can't be a duplicated. So to me it looks like a legacy code sitting there and push the object to the _itemsById array if the same id exists. And this is the reason why MTF Web API test also expecting something, similar however in real world, I couldn't think of a scenario happens like this and thats the reason for my comment that it will run only once.
In any case if you feel its better to remove the please do so. Because if the object contains multiple it should have affected the stores much more since it would have run O( abc) c being the number of objects inside _itemsById
And Thank you so much for the review. Appreciate it.
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.
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
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.
@engcom-Hotel Its done!
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.
The method signature behavior has changed. Any plugin/extension overriding
_setItemAttributeValue()will break.Recommendations:
_setItemAttributeValues()(plural) for batch processingThere 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.
@engcom-Hotel I think you didn't notice its a protected function in an abstract class. Let me know if you still think we need to introduce new function for other reasons. I couldn't think of a scenario someone wants to override the under the hood Eav Object processing.
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.
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
@engcom-Hotel Thank you for sharing the link. Its done!