From f55502899c420e053a2f59bb1b0a23b0ffd25b9f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20W=C3=B3js?= Date: Mon, 21 Oct 2024 20:35:21 +0200 Subject: [PATCH] Reduced Ibexa\Core\Persistence\Legacy\Content\Type\Mapper::extractTypesFromRows computational complexity from n^2 to 2n --- .../Persistence/Legacy/Content/Type/Mapper.php | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/lib/Persistence/Legacy/Content/Type/Mapper.php b/src/lib/Persistence/Legacy/Content/Type/Mapper.php index af8c060d95..0fadda0537 100644 --- a/src/lib/Persistence/Legacy/Content/Type/Mapper.php +++ b/src/lib/Persistence/Legacy/Content/Type/Mapper.php @@ -4,6 +4,7 @@ * @copyright Copyright (C) Ibexa AS. All rights reserved. * @license For full copyright and license information view LICENSE file distributed with this source code. */ + namespace Ibexa\Core\Persistence\Legacy\Content\Type; use Ibexa\Contracts\Core\Persistence\Content\Type; @@ -57,9 +58,9 @@ public function __construct( * * @param \Ibexa\Contracts\Core\Persistence\Content\Type\Group\CreateStruct $struct * + * @return \Ibexa\Contracts\Core\Persistence\Content\Type\Group * @todo $description is not supported by database, yet * - * @return \Ibexa\Contracts\Core\Persistence\Content\Type\Group */ public function createGroupFromCreateStruct(GroupCreateStruct $struct) { @@ -119,6 +120,16 @@ public function extractTypesFromRows(array $rows, bool $keepTypeIdAsKey = false) $types = []; $fields = []; + $rowsByAttributeId = []; + foreach ($rows as $row) { + $attributeId = (int)$row['ezcontentclass_attribute_id']; + if (!isset($rowsByAttributeId[$attributeId])) { + $rowsByAttributeId[$attributeId] = []; + } + + $rowsByAttributeId[$attributeId][] = $row; + } + foreach ($rows as $row) { $typeId = (int)$row['ezcontentclass_id']; if (!isset($types[$typeId])) { @@ -128,9 +139,7 @@ public function extractTypesFromRows(array $rows, bool $keepTypeIdAsKey = false) $fieldId = (int)$row['ezcontentclass_attribute_id']; if ($fieldId && !isset($fields[$fieldId])) { - $fieldDataRows = array_filter($rows, static function (array $row) use ($fieldId) { - return (int) $row['ezcontentclass_attribute_id'] === (int) $fieldId; - }); + $fieldDataRows = $rowsByAttributeId[$fieldId]; $multilingualData = $this->extractMultilingualData($fieldDataRows);