diff options
| author | 2020-02-29 18:19:09 +0100 | |
|---|---|---|
| committer | 2020-02-29 18:19:09 +0100 | |
| commit | 0f94402b7e8b7e25ee605f830b7c7becbe78ba8b (patch) | |
| tree | 473adf4e21e8cbe2f6e36eae69dca3ed8b39a424 /app/Models/EntryDAO.php | |
| parent | e9f879b411ac6af9d102702fb52c8deff161b0e6 (diff) | |
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
Diffstat (limited to 'app/Models/EntryDAO.php')
| -rw-r--r-- | app/Models/EntryDAO.php | 34 |
1 files changed, 10 insertions, 24 deletions
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; - } } |
