From b2a4ec972baac842250471320d28d3cc1823f227 Mon Sep 17 00:00:00 2001 From: Buba Suma Date: Fri, 20 Sep 2019 17:40:55 -0500 Subject: [PATCH] MC-20225: Low performance in getting configurable product attribute options 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 --- .../DataProvider/Product/Form/Modifier/Eav.php | 5 +++++ .../Edit/Tab/Variations/Config/Matrix.php | 12 +++++++----- .../Form/Modifier/Data/AssociatedProducts.php | 17 +++++++++++------ 3 files changed, 23 insertions(+), 11 deletions(-) diff --git a/app/code/Magento/Catalog/Ui/DataProvider/Product/Form/Modifier/Eav.php b/app/code/Magento/Catalog/Ui/DataProvider/Product/Form/Modifier/Eav.php index 5d1e853cef3d1..4039ff862f6fe 100644 --- a/app/code/Magento/Catalog/Ui/DataProvider/Product/Form/Modifier/Eav.php +++ b/app/code/Magento/Catalog/Ui/DataProvider/Product/Form/Modifier/Eav.php @@ -46,6 +46,7 @@ * @SuppressWarnings(PHPMD.CouplingBetweenObjects) * @SuppressWarnings(PHPMD.TooManyFields) * @SuppressWarnings(PHPMD.ExcessiveClassComplexity) + * @SuppressWarnings(PHPMD.ExcessiveClassLength) * @since 101.0.0 */ class Eav extends AbstractModifier @@ -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)) { diff --git a/app/code/Magento/ConfigurableProduct/Block/Adminhtml/Product/Edit/Tab/Variations/Config/Matrix.php b/app/code/Magento/ConfigurableProduct/Block/Adminhtml/Product/Edit/Tab/Variations/Config/Matrix.php index 4874dc8ea03ae..11384263b59a2 100644 --- a/app/code/Magento/ConfigurableProduct/Block/Adminhtml/Product/Edit/Tab/Variations/Config/Matrix.php +++ b/app/code/Magento/ConfigurableProduct/Block/Adminhtml/Product/Edit/Tab/Variations/Config/Matrix.php @@ -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']) @@ -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, ]; } diff --git a/app/code/Magento/ConfigurableProduct/Ui/DataProvider/Product/Form/Modifier/Data/AssociatedProducts.php b/app/code/Magento/ConfigurableProduct/Ui/DataProvider/Product/Form/Modifier/Data/AssociatedProducts.php index c474acbec5094..4bde97fa8022a 100644 --- a/app/code/Magento/ConfigurableProduct/Ui/DataProvider/Product/Form/Modifier/Data/AssociatedProducts.php +++ b/app/code/Magento/ConfigurableProduct/Ui/DataProvider/Product/Form/Modifier/Data/AssociatedProducts.php @@ -21,6 +21,8 @@ use Magento\Framework\Escaper; /** + * Associated products helper + * * @SuppressWarnings(PHPMD.CouplingBetweenObjects) */ class AssociatedProducts @@ -231,6 +233,8 @@ public function getConfigurableAttributesData() * * @return void * @throws \Zend_Currency_Exception + * @SuppressWarnings(PHPMD.CyclomaticComplexity) + * phpcs:disable Generic.Metrics.NestingLevel */ protected function prepareVariations() { @@ -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'], ]; } }