From 0f94402b7e8b7e25ee605f830b7c7becbe78ba8b Mon Sep 17 00:00:00 2001 From: Alexandre Alapetite Date: Sat, 29 Feb 2020 18:19:09 +0100 Subject: Better performance with yield (#2588) * Better performance with yield Largely decrease the time to first byte, and reduced memory consumtion. Before, we used to make several copies in memory of the whole list of articles before sending them to the client. Now streamed as they are processed. * Travis --- app/Models/EntryDAO.php | 34 ++++++++++------------------------ 1 file changed, 10 insertions(+), 24 deletions(-) (limited to 'app/Models/EntryDAO.php') diff --git a/app/Models/EntryDAO.php b/app/Models/EntryDAO.php index d2e7664fc..ebe530ec1 100644 --- a/app/Models/EntryDAO.php +++ b/app/Models/EntryDAO.php @@ -634,8 +634,7 @@ SQL; $stm->bindParam(':guid', $guid); $stm->execute(); $res = $stm->fetchAll(PDO::FETCH_ASSOC); - $entries = self::daoToEntries($res); - return isset($entries[0]) ? $entries[0] : null; + return isset($res[0]) ? self::daoToEntry($res[0]) : null; } public function searchById($id) { @@ -647,8 +646,7 @@ SQL; $stm->bindParam(':id', $id, PDO::PARAM_INT); $stm->execute(); $res = $stm->fetchAll(PDO::FETCH_ASSOC); - $entries = self::daoToEntries($res); - return isset($entries[0]) ? $entries[0] : null; + return isset($res[0]) ? self::daoToEntry($res[0]) : null; } public function searchIdByGuid($id_feed, $guid) { @@ -885,15 +883,17 @@ SQL; public function listWhere($type = 'a', $id = '', $state = FreshRSS_Entry::STATE_ALL, $order = 'DESC', $limit = 1, $firstId = '', $filters = null, $date_min = 0) { $stm = $this->listWhereRaw($type, $id, $state, $order, $limit, $firstId, $filters, $date_min); if ($stm) { - return self::daoToEntries($stm->fetchAll(PDO::FETCH_ASSOC)); + while ($row = $stm->fetch(PDO::FETCH_ASSOC)) { + yield self::daoToEntry($row); + } } else { - return false; + yield false; } } public function listByIds($ids, $order = 'DESC') { if (count($ids) < 1) { - return array(); + yield false; } $sql = 'SELECT id, guid, title, author, ' @@ -905,7 +905,9 @@ SQL; $stm = $this->pdo->prepare($sql); $stm->execute($ids); - return self::daoToEntries($stm->fetchAll(PDO::FETCH_ASSOC)); + while ($row = $stm->fetch(PDO::FETCH_ASSOC)) { + yield self::daoToEntry($row); + } } public function listIdsWhere($type = 'a', $id = '', $state = FreshRSS_Entry::STATE_ALL, $order = 'DESC', $limit = 1, $firstId = '', $filters = null) { //For API @@ -1058,20 +1060,4 @@ SQL; } return $entry; } - - private static function daoToEntries($listDAO) { - $list = array(); - - if (!is_array($listDAO)) { - $listDAO = array($listDAO); - } - - foreach ($listDAO as $key => $dao) { - $list[] = self::daoToEntry($dao); - } - - unset($listDAO); - - return $list; - } } -- cgit v1.2.3