From a61765b9e4dfb73442151833a9d76dee36391403 Mon Sep 17 00:00:00 2001 From: Sam Date: Wed, 23 Sep 2015 13:13:34 +1000 Subject: [PATCH] PERF: improve perf of initial payload also reduce querying in topic query --- app/models/site.rb | 11 +++++++++-- lib/guardian/category_guardian.rb | 7 +++++-- lib/topic_query.rb | 5 +++-- 3 files changed, 17 insertions(+), 6 deletions(-) diff --git a/app/models/site.rb b/app/models/site.rb index c6a3313f1..6c4c05dfd 100644 --- a/app/models/site.rb +++ b/app/models/site.rb @@ -41,7 +41,7 @@ class Site @categories ||= begin categories = Category .secured(@guardian) - .includes(:topic_only_relative_url, :subcategories) + .includes(:topic_only_relative_url) .order(:position) unless SiteSetting.allow_uncategorized_topics @@ -50,6 +50,13 @@ class Site categories = categories.to_a + with_children = Set.new + categories.each do |c| + if c.parent_category_id + with_children << c.parent_category_id + end + end + allowed_topic_create = Set.new(Category.topic_create_allowed(@guardian).pluck(:id)) by_id = {} @@ -62,7 +69,7 @@ class Site categories.each do |category| category.notification_level = category_user[category.id] category.permission = CategoryGroup.permission_types[:full] if allowed_topic_create.include?(category.id) - category.has_children = category.subcategories.present? + category.has_children = with_children.include?(category.id) by_id[category.id] = category end diff --git a/lib/guardian/category_guardian.rb b/lib/guardian/category_guardian.rb index 63bbd5be7..c24c652b6 100644 --- a/lib/guardian/category_guardian.rb +++ b/lib/guardian/category_guardian.rb @@ -54,8 +54,11 @@ module CategoryGuardian # all allowed category ids def allowed_category_ids - unrestricted = Category.where(read_restricted: false).pluck(:id) - unrestricted.concat(secure_category_ids) + @allowed_category_ids ||= + begin + unrestricted = Category.where(read_restricted: false).pluck(:id) + unrestricted.concat(secure_category_ids) + end end def topic_create_allowed_category_ids diff --git a/lib/topic_query.rb b/lib/topic_query.rb index 73d47fd4a..434fdbb0e 100644 --- a/lib/topic_query.rb +++ b/lib/topic_query.rb @@ -46,6 +46,7 @@ class TopicQuery options.assert_valid_keys(VALID_OPTIONS) @options = options.dup @user = user + @guardian = Guardian.new(@user) end def joined_topic_user(list=nil) @@ -359,7 +360,7 @@ class TopicQuery when 'unlisted' result = result.where('NOT topics.visible') when 'deleted' - guardian = Guardian.new(@user) + guardian = @guardian if guardian.is_staff? result = result.where('topics.deleted_at IS NOT NULL') require_deleted_clause = false @@ -391,7 +392,7 @@ class TopicQuery result = result.where('topics.posts_count <= ?', options[:max_posts]) if options[:max_posts].present? result = result.where('topics.posts_count >= ?', options[:min_posts]) if options[:min_posts].present? - Guardian.new(@user).filter_allowed_categories(result) + @guardian.filter_allowed_categories(result) end def remove_muted_categories(list, user, opts=nil)