diff options
| author | 2020-02-29 18:19:09 +0100 | |
|---|---|---|
| committer | 2020-02-29 18:19:09 +0100 | |
| commit | 0f94402b7e8b7e25ee605f830b7c7becbe78ba8b (patch) | |
| tree | 473adf4e21e8cbe2f6e36eae69dca3ed8b39a424 /app/Controllers/indexController.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/Controllers/indexController.php')
| -rwxr-xr-x | app/Controllers/indexController.php | 76 |
1 files changed, 33 insertions, 43 deletions
diff --git a/app/Controllers/indexController.php b/app/Controllers/indexController.php index 276d56acd..ddd51455d 100755 --- a/app/Controllers/indexController.php +++ b/app/Controllers/indexController.php @@ -32,6 +32,13 @@ class FreshRSS_index_Controller extends Minz_ActionController { Minz_Error::error(404); } + $this->_csp([ + 'default-src' => "'self'", + 'frame-src' => '*', + 'img-src' => '* data:', + 'media-src' => '*', + ]); + $this->view->categories = FreshRSS_Context::$categories; $this->view->rss_title = FreshRSS_Context::$name . ' | ' . Minz_View::title(); @@ -54,42 +61,35 @@ class FreshRSS_index_Controller extends Minz_ActionController { } }; - $this->view->callbackBeforePagination = function ($view) { + $this->view->callbackBeforeEntries = function ($view) { try { FreshRSS_Context::$number++; //+1 for pagination - $entries = FreshRSS_index_Controller::listEntriesByContext(); + $view->entries = FreshRSS_index_Controller::listEntriesByContext(); FreshRSS_Context::$number--; - - $nb_entries = count($entries); - if ($nb_entries > FreshRSS_Context::$number) { - // We have more elements for pagination - $last_entry = array_pop($entries); - FreshRSS_Context::$next_id = $last_entry->id(); - } - - $first_entry = $nb_entries > 0 ? $entries[0] : null; - FreshRSS_Context::$id_max = $first_entry === null ? (time() - 1) . '000000' : $first_entry->id(); - if (FreshRSS_Context::$order === 'ASC') { - // In this case we do not know but we guess id_max - $id_max = (time() - 1) . '000000'; - if (strcmp($id_max, FreshRSS_Context::$id_max) > 0) { - FreshRSS_Context::$id_max = $id_max; - } - } - - $view->entries = $entries; + ob_start(); } catch (FreshRSS_EntriesGetter_Exception $e) { Minz_Log::notice($e->getMessage()); Minz_Error::error(404); } }; - $this->_csp([ - 'default-src' => "'self'", - 'frame-src' => '*', - 'img-src' => '* data:', - 'media-src' => '*', - ]); + $this->view->callbackBeforePagination = function ($view, $nbEntries, $firstEntry, $lastEntry) { + if ($nbEntries >= FreshRSS_Context::$number) { + //We have enough entries: we discard the last one to use it for the next pagination + ob_clean(); + FreshRSS_Context::$next_id = $lastEntry->id(); + } + ob_end_flush(); + + FreshRSS_Context::$id_max = $firstEntry === null ? (time() - 1) . '000000' : $firstEntry->id(); + if (FreshRSS_Context::$order === 'ASC') { + // In this case we do not know but we guess id_max + $id_max = (time() - 1) . '000000'; + if (strcmp($id_max, FreshRSS_Context::$id_max) > 0) { + FreshRSS_Context::$id_max = $id_max; + } + } + }; } /** @@ -247,23 +247,13 @@ class FreshRSS_index_Controller extends Minz_ActionController { $limit = FreshRSS_Context::$user_conf->max_posts_per_rss; } - $entries = $entryDAO->listWhere( - $type, $id, FreshRSS_Context::$state, FreshRSS_Context::$order, - $limit, FreshRSS_Context::$first_id, - FreshRSS_Context::$search, $date_min - ); - - if (FreshRSS_Context::$sinceHours && (count($entries) < FreshRSS_Context::$user_conf->min_posts_per_rss)) { - $date_min = 0; - $limit = FreshRSS_Context::$user_conf->min_posts_per_rss; - $entries = $entryDAO->listWhere( - $type, $id, FreshRSS_Context::$state, FreshRSS_Context::$order, - $limit, FreshRSS_Context::$first_id, - FreshRSS_Context::$search, $date_min - ); + foreach ($entryDAO->listWhere( + $type, $id, FreshRSS_Context::$state, FreshRSS_Context::$order, + $limit, FreshRSS_Context::$first_id, + FreshRSS_Context::$search, $date_min) + as $entry) { + yield $entry; } - - return $entries; } /** |
