From c9a8f99f6ad48a4e442a15268b8cd5a2d982d761 Mon Sep 17 00:00:00 2001 From: Marcin Kurczewski Date: Sat, 30 Nov 2013 00:53:09 +0100 Subject: [PATCH] Optimization: preloading moved back to controllers - Nearly twice faster page load - Query count greatly dropped --- src/Controllers/CommentController.php | 3 +++ src/Controllers/PostController.php | 13 ++++++++++++- src/Controllers/UserController.php | 1 + src/Models/AbstractModel.php | 15 --------------- src/Models/Model_Comment.php | 5 ----- src/Models/Model_Post.php | 5 ----- src/Views/post-small.phtml | 4 ++-- 7 files changed, 18 insertions(+), 28 deletions(-) diff --git a/src/Controllers/CommentController.php b/src/Controllers/CommentController.php index e5d5b6c3..3cfaf3be 100644 --- a/src/Controllers/CommentController.php +++ b/src/Controllers/CommentController.php @@ -24,6 +24,7 @@ class CommentController $pageCount = ceil($commentCount / $commentsPerPage); $page = max(1, min($pageCount, $page)); $comments = Model_Comment::getEntities(null, $commentsPerPage, $page); + R::preload($comments, ['commenter' => 'user', 'post', 'post.uploader' => 'user', 'post.sharedTag']); $this->context->postGroups = true; $this->context->transport->paginator = new StdClass; @@ -79,6 +80,8 @@ class CommentController public function deleteAction($id) { $comment = Model_Comment::locate($id); + R::preload($comment, ['commenter' => 'user']); + PrivilegesHelper::confirmWithException(Privilege::DeleteComment, PrivilegesHelper::getIdentitySubPrivilege($comment->commenter)); Model_Comment::remove($comment); diff --git a/src/Controllers/PostController.php b/src/Controllers/PostController.php index 4c27a0b7..d3410226 100644 --- a/src/Controllers/PostController.php +++ b/src/Controllers/PostController.php @@ -107,6 +107,7 @@ class PostController $pageCount = ceil($postCount / $postsPerPage); $page = max(1, min($pageCount, $page)); $posts = Model_Post::getEntitiesFast($query, $postsPerPage, $page); + R::preload($posts, 'sharedTag'); $this->context->transport->paginator = new StdClass; $this->context->transport->paginator->page = $page; @@ -299,6 +300,8 @@ class PostController public function hideAction($id) { $post = Model_Post::locate($id); + R::preload($post, ['uploader' => 'user']); + PrivilegesHelper::confirmWithException(Privilege::HidePost, PrivilegesHelper::getIdentitySubPrivilege($post->uploader)); if (InputHelper::get('submit')) @@ -319,6 +322,8 @@ class PostController public function unhideAction($id) { $post = Model_Post::locate($id); + R::preload($post, ['uploader' => 'user']); + PrivilegesHelper::confirmWithException(Privilege::HidePost, PrivilegesHelper::getIdentitySubPrivilege($post->uploader)); if (InputHelper::get('submit')) @@ -339,6 +344,8 @@ class PostController public function deleteAction($id) { $post = Model_Post::locate($id); + R::preload($post, ['uploader' => 'user']); + PrivilegesHelper::confirmWithException(Privilege::DeletePost, PrivilegesHelper::getIdentitySubPrivilege($post->uploader)); if (InputHelper::get('submit')) @@ -441,8 +448,9 @@ class PostController $post = Model_Post::locate($id); R::preload($post, [ 'tag', - 'comment', + 'uploader' => 'user', 'ownComment.commenter' => 'user']); + R::preload($this->context->user, ['ownFavoritee']); $this->context->transport->lastSearchQuery = InputHelper::get('last-search-query'); @@ -552,6 +560,9 @@ class PostController private function doEdit($post, $isNew) { + if (!$isNew) + R::preload($post, ['uploader' => 'user']); + /* file contents */ if (!empty($_FILES['file']['name'])) { diff --git a/src/Controllers/UserController.php b/src/Controllers/UserController.php index b8f2de4e..e9461d08 100644 --- a/src/Controllers/UserController.php +++ b/src/Controllers/UserController.php @@ -441,6 +441,7 @@ class UserController $pageCount = ceil($postCount / $postsPerPage); $page = max(1, min($pageCount, $page)); $posts = Model_Post::getEntities($query, $postsPerPage, $page); + R::preload($posts, 'sharedTag'); $this->context->transport->tab = $tab; $this->context->transport->lastSearchQuery = $query; diff --git a/src/Models/AbstractModel.php b/src/Models/AbstractModel.php index 336501ac..c87907ac 100644 --- a/src/Models/AbstractModel.php +++ b/src/Models/AbstractModel.php @@ -81,19 +81,4 @@ abstract class AbstractModel extends RedBean_SimpleModel { R::store($entity); } - - /* FUSE methods for RedBeanPHP, preventing some aliasing errors */ - public function open() - { - $this->preload(); - } - - public function after_update() - { - $this->preload(); - } - - public function preload() - { - } } diff --git a/src/Models/Model_Comment.php b/src/Models/Model_Comment.php index e6bc2847..cd19118d 100644 --- a/src/Models/Model_Comment.php +++ b/src/Models/Model_Comment.php @@ -11,11 +11,6 @@ class Model_Comment extends AbstractModel return 'Model_Comment_QueryBuilder'; } - public function preload() - { - R::preload($this->bean, ['commenter' => 'user', 'post', 'post.uploader' => 'user']); - } - public static function locate($key, $throw = true) diff --git a/src/Models/Model_Post.php b/src/Models/Model_Post.php index ccb81845..6a2dd4ba 100644 --- a/src/Models/Model_Post.php +++ b/src/Models/Model_Post.php @@ -18,11 +18,6 @@ class Model_Post extends AbstractModel return 'Model_Post_QueryBuilder'; } - public function preload() - { - R::preload($this->bean, ['uploader' => 'user','favoritee' => 'user']); - } - public static function locate($key, $disallowNumeric = false, $throw = true) diff --git a/src/Views/post-small.phtml b/src/Views/post-small.phtml index ebc00209..8d103db4 100644 --- a/src/Views/post-small.phtml +++ b/src/Views/post-small.phtml @@ -24,8 +24,8 @@ $x = [ 'score' => $this->context->post->score, - 'comments' => $this->context->post->countOwn('comment'), - 'favs' => $this->context->post->countOwn('favoritee'), + 'comments' => $this->context->post->comment_count, + 'favs' => $this->context->post->fav_count, ]; ?>