Skip to content

Commit

Permalink
MC-20225: Low performance in getting configurable product attribute o…
Browse files Browse the repository at this point in the history
…ptions functionality

- Fix performance issue related to AbstractAttribute::getOptions due to the expansive cost of the function AbstractAttribute::convertToObjects
- Fix performance issue related to loading options multiple times
  • Loading branch information
bubasuma committed Sep 23, 2019
1 parent d6ef3ef commit b2a4ec9
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
* @SuppressWarnings(PHPMD.TooManyFields)
* @SuppressWarnings(PHPMD.ExcessiveClassComplexity)
* @SuppressWarnings(PHPMD.ExcessiveClassLength)
* @since 101.0.0
*/
class Eav extends AbstractModifier
Expand Down Expand Up @@ -1048,6 +1049,10 @@ private function isScopeGlobal($attribute)
*/
private function getAttributeModel($attribute)
{
// The statement below solves performance issue related to loading same attribute options on different models
if ($attribute instanceof EavAttribute) {
return $attribute;
}
$attributeId = $attribute->getAttributeId();

if (!array_key_exists($attributeId, $this->attributesCache)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ protected function getAttributes()
foreach ($attributes as $key => $attribute) {
if (isset($configurableData[$key])) {
$attributes[$key] = array_replace_recursive($attribute, $configurableData[$key]);
// phpcs:ignore Magento2.Performance.ForeachArrayMerge
$attributes[$key]['values'] = array_merge(
isset($attribute['values']) ? $attribute['values'] : [],
isset($configurableData[$key]['values'])
Expand Down Expand Up @@ -412,14 +413,15 @@ private function prepareAttributes(
'position' => $configurableAttributes[$attribute->getAttributeId()]['position'],
'chosen' => [],
];
foreach ($attribute->getOptions() as $option) {
if (!empty($option->getValue())) {
$options = $attribute->usesSource() ? $attribute->getSource()->getAllOptions() : [];
foreach ($options as $option) {
if (!empty($option['value'])) {
$attributes[$attribute->getAttributeId()]['options'][] = [
'attribute_code' => $attribute->getAttributeCode(),
'attribute_label' => $attribute->getStoreLabel(0),
'id' => $option->getValue(),
'label' => $option->getLabel(),
'value' => $option->getValue(),
'id' => $option['value'],
'label' => $option['label'],
'value' => $option['value'],
'__disableTmpl' => true,
];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
use Magento\Framework\Escaper;

/**
* Associated products helper
*
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class AssociatedProducts
Expand Down Expand Up @@ -231,6 +233,8 @@ public function getConfigurableAttributesData()
*
* @return void
* @throws \Zend_Currency_Exception
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
* phpcs:disable Generic.Metrics.NestingLevel
*/
protected function prepareVariations()
{
Expand Down Expand Up @@ -262,14 +266,15 @@ protected function prepareVariations()
'position' => $configurableAttributes[$attribute->getAttributeId()]['position'],
'chosen' => [],
];
foreach ($attribute->getOptions() as $option) {
if (!empty($option->getValue())) {
$attributes[$attribute->getAttributeId()]['options'][$option->getValue()] = [
$options = $attribute->usesSource() ? $attribute->getSource()->getAllOptions() : [];
foreach ($options as $option) {
if (!empty($option['value'])) {
$attributes[$attribute->getAttributeId()]['options'][$option['value']] = [
'attribute_code' => $attribute->getAttributeCode(),
'attribute_label' => $attribute->getStoreLabel(0),
'id' => $option->getValue(),
'label' => $option->getLabel(),
'value' => $option->getValue(),
'id' => $option['value'],
'label' => $option['label'],
'value' => $option['value'],
];
}
}
Expand Down

0 comments on commit b2a4ec9

Please sign in to comment.