From aab03422bc099eab918a03a76b059c838b467c2c Mon Sep 17 00:00:00 2001 From: r888888888 Date: Thu, 24 Apr 2014 22:18:14 -0700 Subject: [PATCH] performance tweaks for rails 4.1 --- Gemfile | 1 + Gemfile.lock | 3 +++ app/controllers/advertisements_controller.rb | 2 +- app/controllers/comments_controller.rb | 2 +- app/controllers/forum_topics_controller.rb | 2 +- app/controllers/moderator/post/queues_controller.rb | 2 +- app/controllers/pool_elements_controller.rb | 4 +++- app/logical/post_sets/intro.rb | 2 +- app/logical/post_sets/post.rb | 2 +- app/logical/upload_error_checker.rb | 2 +- app/models/post_version.rb | 2 +- app/models/tag.rb | 2 +- app/models/tag_implication.rb | 2 +- app/models/user_name_change_request.rb | 2 +- app/presenters/wiki_page_presenter.rb | 6 +++--- app/views/post_versions/index.html.erb | 2 +- 16 files changed, 22 insertions(+), 16 deletions(-) diff --git a/Gemfile b/Gemfile index 5e8b33318..1c547b5d1 100644 --- a/Gemfile +++ b/Gemfile @@ -51,5 +51,6 @@ end group :development do gem 'ruby-prof' + gem 'sql-logging' end diff --git a/Gemfile.lock b/Gemfile.lock index bf48f2110..81545bbb8 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -183,6 +183,8 @@ GEM actionpack (>= 3.0) activesupport (>= 3.0) sprockets (~> 2.8) + sql-logging (3.0.8) + rails (> 3.0.0) statistics2 (0.54) term-ansicolor (1.3.0) tins (~> 1.0) @@ -253,6 +255,7 @@ DEPENDENCIES shoulda simple_form simplecov + sql-logging statistics2 term-ansicolor therubyracer diff --git a/app/controllers/advertisements_controller.rb b/app/controllers/advertisements_controller.rb index 109829d7e..5bfc91841 100644 --- a/app/controllers/advertisements_controller.rb +++ b/app/controllers/advertisements_controller.rb @@ -13,7 +13,7 @@ class AdvertisementsController < ApplicationController end def index - @advertisements = Advertisement.order("id desc").all + @advertisements = Advertisement.order("id desc") @start_date = 1.month.ago.to_date @end_date = Date.today end diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb index 07a5a6f3f..db07823f5 100644 --- a/app/controllers/comments_controller.rb +++ b/app/controllers/comments_controller.rb @@ -79,7 +79,7 @@ private def index_by_post @posts = Post.where("last_comment_bumped_at IS NOT NULL").tag_match(params[:tags]).reorder("last_comment_bumped_at DESC").paginate(params[:page], :limit => 5, :search_count => params[:search]) - @posts.all + @posts.each # hack to force rails to eager load respond_with(@posts) do |format| format.html {render :action => "index_by_post"} format.xml do diff --git a/app/controllers/forum_topics_controller.rb b/app/controllers/forum_topics_controller.rb index b332c75f8..95b2dd770 100644 --- a/app/controllers/forum_topics_controller.rb +++ b/app/controllers/forum_topics_controller.rb @@ -31,7 +31,7 @@ class ForumTopicsController < ApplicationController def show @forum_topic = ForumTopic.find(params[:id]) @forum_posts = ForumPost.search(:topic_id => @forum_topic.id).order("forum_posts.id").paginate(params[:page]) - @forum_posts.all + @forum_posts.each # hack to force rails to eager load respond_with(@forum_topic) unless CurrentUser.user.is_anonymous? session[:read_forum_topics] = @forum_topic.mark_as_read(read_forum_topic_ids) diff --git a/app/controllers/moderator/post/queues_controller.rb b/app/controllers/moderator/post/queues_controller.rb index d0dfa2fbc..0456f570d 100644 --- a/app/controllers/moderator/post/queues_controller.rb +++ b/app/controllers/moderator/post/queues_controller.rb @@ -7,7 +7,7 @@ module Moderator def show ::Post.without_timeout do @posts = ::Post.order("posts.id asc").pending_or_flagged.available_for_moderation(params[:hidden]).search(:tag_match => "#{params[:query]} status:any").paginate(params[:page], :limit => 100) - @posts.all # cache the data + @posts.each # hack to force rails to eager load end respond_with(@posts) end diff --git a/app/controllers/pool_elements_controller.rb b/app/controllers/pool_elements_controller.rb index ab7709097..ed38361c8 100644 --- a/app/controllers/pool_elements_controller.rb +++ b/app/controllers/pool_elements_controller.rb @@ -22,7 +22,9 @@ class PoolElementsController < ApplicationController end def all_select - @pools = Pool.undeleted.where("is_active = true").order("name").select("id, name").all + @pools = Pool.undeleted.where("is_active = true").order("name").select("id, name") + @pools.each # hack to force rails to eager load + @pools end private diff --git a/app/logical/post_sets/intro.rb b/app/logical/post_sets/intro.rb index 4449cb957..4c73d3798 100644 --- a/app/logical/post_sets/intro.rb +++ b/app/logical/post_sets/intro.rb @@ -7,7 +7,7 @@ module PostSets def posts @posts ||= begin temp = ::Post.tag_match("#{tag_string} favcount:>3").paginate(page, :search_count => nil, :limit => 5) - temp.all + temp.each # hack to force rails to eager load temp end end diff --git a/app/logical/post_sets/post.rb b/app/logical/post_sets/post.rb index c4008d296..5fecd22d5 100644 --- a/app/logical/post_sets/post.rb +++ b/app/logical/post_sets/post.rb @@ -73,7 +73,7 @@ module PostSets else temp = ::Post.tag_match(tag_string).paginate(page, :count => ::Post.fast_count(tag_string), :limit => per_page) end - temp.all + temp.each # hack to force rails to eager load temp end end diff --git a/app/logical/upload_error_checker.rb b/app/logical/upload_error_checker.rb index f9c32afb1..cf8794364 100644 --- a/app/logical/upload_error_checker.rb +++ b/app/logical/upload_error_checker.rb @@ -4,7 +4,7 @@ require 'mail' class UploadErrorChecker def check! - uploads = Upload.where("status like 'error%' and status not like 'error: RuntimeError - duplicate%' and created_at >= ?", 1.hour.ago).all + uploads = Upload.where("status like 'error%' and status not like 'error: RuntimeError - duplicate%' and created_at >= ?", 1.hour.ago) if uploads.size > 5 mail = Mail.new do from "webmaster@danbooru.donmai.us" diff --git a/app/models/post_version.rb b/app/models/post_version.rb index 708eb5213..04e345e58 100644 --- a/app/models/post_version.rb +++ b/app/models/post_version.rb @@ -66,7 +66,7 @@ class PostVersion < ActiveRecord::Base end def sequence_for_post - versions = PostVersion.where(:post_id => post_id).order("updated_at desc, id desc").all + versions = PostVersion.where(:post_id => post_id).order("updated_at desc, id desc") diffs = [] versions.each_index do |i| if i < versions.size - 1 diff --git a/app/models/tag.rb b/app/models/tag.rb index 46c3ff5cf..19adbfccd 100644 --- a/app/models/tag.rb +++ b/app/models/tag.rb @@ -607,7 +607,7 @@ class Tag < ActiveRecord::Base search_for = "%" + query.to_escaped_for_sql_like + "%" end - Tag.where(["name LIKE ? ESCAPE E'\\\\' AND post_count > 0 AND name <> ?", search_for, query]).all(:order => "post_count DESC", :limit => 6, :select => "name").map(&:name).sort + Tag.where(["name LIKE ? ESCAPE E'\\\\' AND post_count > 0 AND name <> ?", search_for, query]).order("post_count DESC").limit(6).select("name").map(&:name).sort end end diff --git a/app/models/tag_implication.rb b/app/models/tag_implication.rb index cc36fbb96..9e74132bd 100644 --- a/app/models/tag_implication.rb +++ b/app/models/tag_implication.rb @@ -29,7 +29,7 @@ class TagImplication < ActiveRecord::Base until children.empty? all.concat(children) - children = TagImplication.where("antecedent_name IN (?) and status in (?)", children, ["active", "processing"]).all.map(&:consequent_name) + children = TagImplication.where("antecedent_name IN (?) and status in (?)", children, ["active", "processing"]).map(&:consequent_name) end end.sort.uniq end diff --git a/app/models/user_name_change_request.rb b/app/models/user_name_change_request.rb index 0d9f0e0ea..75eefd2ff 100644 --- a/app/models/user_name_change_request.rb +++ b/app/models/user_name_change_request.rb @@ -32,7 +32,7 @@ class UserNameChangeRequest < ActiveRecord::Base end def feedback - UserFeedback.for_user(user_id).order("id desc").all + UserFeedback.for_user(user_id).order("id desc") end def notify_admins diff --git a/app/presenters/wiki_page_presenter.rb b/app/presenters/wiki_page_presenter.rb index 6137e06c3..9c7b01d90 100644 --- a/app/presenters/wiki_page_presenter.rb +++ b/app/presenters/wiki_page_presenter.rb @@ -14,7 +14,7 @@ class WikiPagePresenter end def consequent_tag_aliases - @consequent_tag_aliases ||= TagAlias.where("status = 'active' and consequent_name = ?", wiki_page.title).all + @consequent_tag_aliases ||= TagAlias.where("status = 'active' and consequent_name = ?", wiki_page.title) end def antecedent_tag_alias @@ -22,11 +22,11 @@ class WikiPagePresenter end def consequent_tag_implications - @consequent_tag_implications ||= TagImplication.where("status = 'active' and consequent_name = ?", wiki_page.title).all + @consequent_tag_implications ||= TagImplication.where("status = 'active' and consequent_name = ?", wiki_page.title) end def antecedent_tag_implications - @antecedent_tag_implications ||= TagImplication.where("status = 'active' and antecedent_name = ?", wiki_page.title).all + @antecedent_tag_implications ||= TagImplication.where("status = 'active' and antecedent_name = ?", wiki_page.title) end # Produce a formatted page that shows the difference between two versions of a page. diff --git a/app/views/post_versions/index.html.erb b/app/views/post_versions/index.html.erb index 64cba84b0..d5df9dd64 100644 --- a/app/views/post_versions/index.html.erb +++ b/app/views/post_versions/index.html.erb @@ -2,7 +2,7 @@

Changes

- <% if @post_versions.all.empty? %> + <% if @post_versions.empty? %> <%= render "post_sets/blank" %> <% else %> <%= render "listing", :post_versions => @post_versions %>