From fd52b85eed3ff9d470787ba2b59674bb7f262f82 Mon Sep 17 00:00:00 2001 From: Kamil Tekiela Date: Mon, 19 Dec 2022 18:17:45 +0000 Subject: [PATCH] Microoptimization of fetchResult() Signed-off-by: Kamil Tekiela --- libraries/classes/DatabaseInterface.php | 46 ++++++++++++------------- psalm-baseline.xml | 12 +++---- 2 files changed, 28 insertions(+), 30 deletions(-) diff --git a/libraries/classes/DatabaseInterface.php b/libraries/classes/DatabaseInterface.php index 09750f10a5a5..adc8d7f56d19 100644 --- a/libraries/classes/DatabaseInterface.php +++ b/libraries/classes/DatabaseInterface.php @@ -25,6 +25,7 @@ use stdClass; use function __; +use function array_column; use function array_diff; use function array_keys; use function array_map; @@ -1293,11 +1294,7 @@ private function fetchValueOrValueByIndex($row, $value) */ private function fetchByMode(ResultInterface $result, string $mode): array { - if ($mode === self::FETCH_NUM) { - return $result->fetchRow(); - } - - return $result->fetchAssoc(); + return $mode === self::FETCH_NUM ? $result->fetchRow() : $result->fetchAssoc(); } /** @@ -1364,23 +1361,20 @@ public function fetchResult( // return empty array if result is empty or false if ($result === false) { - return $resultRows; + return []; } - $fetchFunction = self::FETCH_ASSOC; - if ($key === null) { // no nested array if only one field is in result - if ($result->numFields() === 1) { - $value = 0; - $fetchFunction = self::FETCH_NUM; + if ($value === 0 || $result->numFields() === 1) { + return $result->fetchAllColumn(); } - while ($row = $this->fetchByMode($result, $fetchFunction)) { - $resultRows[] = $this->fetchValueOrValueByIndex($row, $value); - } - } elseif (is_array($key)) { - while ($row = $this->fetchByMode($result, $fetchFunction)) { + return $value === null ? $result->fetchAllAssoc() : array_column($result->fetchAllAssoc(), $value); + } + + if (is_array($key)) { + while ($row = $result->fetchAssoc()) { $resultTarget =& $resultRows; foreach ($key as $keyIndex) { if ($keyIndex === null) { @@ -1397,15 +1391,19 @@ public function fetchResult( $resultTarget = $this->fetchValueOrValueByIndex($row, $value); } - } else { - // if $key is an integer use non associative mysql fetch function - if (is_int($key)) { - $fetchFunction = self::FETCH_NUM; - } - while ($row = $this->fetchByMode($result, $fetchFunction)) { - $resultRows[$row[$key]] = $this->fetchValueOrValueByIndex($row, $value); - } + return $resultRows; + } + + if ($key === 0 && $value === 1) { + return $result->fetchAllKeyPair(); + } + + // if $key is an integer use non associative mysql fetch function + $fetchFunction = is_int($key) ? self::FETCH_NUM : self::FETCH_ASSOC; + + while ($row = $this->fetchByMode($result, $fetchFunction)) { + $resultRows[$row[$key]] = $this->fetchValueOrValueByIndex($row, $value); } return $resultRows; diff --git a/psalm-baseline.xml b/psalm-baseline.xml index c8938c0a3db6..aacc1a0b8597 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -5685,22 +5685,18 @@ $tableData['Engine'] $tableData['Index_length'] - + $resultRows[$row[$key]] - $resultTarget[$row[$keyIndex]] - $resultTarget[$row[$keyIndex]] - $resultTarget[$row[$keyIndex]] $row[$keyIndex] $row[$keyIndex] $row[$keyIndex] - + $aLength $bLength $keyIndex $map['real_column'] $resultRows[$row[$key]] - $resultRows[] $resultTarget $table $tableData @@ -5738,6 +5734,10 @@ $row[$value] + + $resultTarget + $resultTarget + $row['Data_free'] $row['Data_length']