Replace Hash#keys.each with Hash#each_key for some perf boost

This commit is contained in:
Tan Le 2015-04-18 21:53:53 +10:00
parent d16df04c76
commit 9fbc763902
12 changed files with 15 additions and 15 deletions

View file

@ -436,7 +436,7 @@ class PostsController < ApplicationController
result[:is_warning] = false result[:is_warning] = false
end end
PostRevisor.tracked_topic_fields.keys.each do |f| PostRevisor.tracked_topic_fields.each_key do |f|
params.permit(f => []) params.permit(f => [])
result[f] = params[f] if params.has_key?(f) result[f] = params[f] if params.has_key?(f)
end end

View file

@ -128,7 +128,7 @@ class TopicsController < ApplicationController
guardian.ensure_can_edit!(topic) guardian.ensure_can_edit!(topic)
changes = {} changes = {}
PostRevisor.tracked_topic_fields.keys.each do |f| PostRevisor.tracked_topic_fields.each_key do |f|
changes[f] = params[f] if params.has_key?(f) changes[f] = params[f] if params.has_key?(f)
end end

View file

@ -19,7 +19,7 @@ class DirectoryItem < ActiveRecord::Base
def self.refresh! def self.refresh!
ActiveRecord::Base.transaction do ActiveRecord::Base.transaction do
exec_sql "TRUNCATE TABLE directory_items" exec_sql "TRUNCATE TABLE directory_items"
period_types.keys.each {|p| refresh_period!(p)} period_types.each_key {|p| refresh_period!(p)}
end end
end end

View file

@ -151,7 +151,7 @@ class Group < ActiveRecord::Base
end end
def self.ensure_automatic_groups! def self.ensure_automatic_groups!
AUTO_GROUPS.keys.each do |name| AUTO_GROUPS.each_key do |name|
refresh_automatic_group!(name) unless lookup_group(name) refresh_automatic_group!(name) unless lookup_group(name)
end end
end end

View file

@ -36,7 +36,7 @@ class IncomingLinksReport
num_clicks = link_count_per_user num_clicks = link_count_per_user
num_topics = topic_count_per_user num_topics = topic_count_per_user
report.data = [] report.data = []
num_clicks.keys.each do |username| num_clicks.each_key do |username|
report.data << {username: username, num_clicks: num_clicks[username], num_topics: num_topics[username]} report.data << {username: username, num_clicks: num_clicks[username], num_topics: num_topics[username]}
end end
report.data = report.data.sort_by {|x| x[:num_clicks]}.reverse[0,10] report.data = report.data.sort_by {|x| x[:num_clicks]}.reverse[0,10]
@ -67,7 +67,7 @@ class IncomingLinksReport
num_clicks = link_count_per_domain num_clicks = link_count_per_domain
num_topics = topic_count_per_domain(num_clicks.keys) num_topics = topic_count_per_domain(num_clicks.keys)
report.data = [] report.data = []
num_clicks.keys.each do |domain| num_clicks.each_key do |domain|
report.data << {domain: domain, num_clicks: num_clicks[domain], num_topics: num_topics[domain]} report.data << {domain: domain, num_clicks: num_clicks[domain], num_topics: num_topics[domain]}
end end
report.data = report.data.sort_by {|x| x[:num_clicks]}.reverse[0,10] report.data = report.data.sort_by {|x| x[:num_clicks]}.reverse[0,10]

View file

@ -221,7 +221,7 @@ class Post < ActiveRecord::Base
TopicLink.where(domain: hosts.keys, user_id: acting_user.id) TopicLink.where(domain: hosts.keys, user_id: acting_user.id)
.group(:domain, :post_id) .group(:domain, :post_id)
.count.keys.each do |tuple| .count.each_key do |tuple|
domain = tuple[0] domain = tuple[0]
hosts[domain] = (hosts[domain] || 0) + 1 hosts[domain] = (hosts[domain] || 0) + 1
end end

View file

@ -613,7 +613,7 @@ class Topic < ActiveRecord::Base
end end
def update_action_counts def update_action_counts
PostActionType.types.keys.each do |type| PostActionType.types.each_key do |type|
count_field = "#{type}_count" count_field = "#{type}_count"
update_column(count_field, Post.where(topic_id: id).sum(count_field)) update_column(count_field, Post.where(topic_id: id).sum(count_field))
end end

View file

@ -178,7 +178,7 @@ class PostRevisionSerializer < ApplicationSerializer
} }
# Retrieve any `tracked_topic_fields` # Retrieve any `tracked_topic_fields`
PostRevisor.tracked_topic_fields.keys.each do |field| PostRevisor.tracked_topic_fields.each_key do |field|
if topic.respond_to?(field) if topic.respond_to?(field)
latest_modifications[field.to_s] = [topic.send(field)] latest_modifications[field.to_s] = [topic.send(field)]
end end
@ -198,7 +198,7 @@ class PostRevisionSerializer < ApplicationSerializer
revision[:revision] = pr.number revision[:revision] = pr.number
revision[:hidden] = pr.hidden revision[:hidden] = pr.hidden
pr.modifications.keys.each do |field| pr.modifications.each_key do |field|
revision[field] = pr.modifications[field][0] revision[field] = pr.modifications[field][0]
end end
@ -210,7 +210,7 @@ class PostRevisionSerializer < ApplicationSerializer
cur = @all_revisions[r] cur = @all_revisions[r]
prev = @all_revisions[r - 1] prev = @all_revisions[r - 1]
cur.keys.each do |field| cur.each_key do |field|
prev[field] = prev.has_key?(field) ? prev[field] : cur[field] prev[field] = prev.has_key?(field) ? prev[field] : cur[field]
end end
end end

View file

@ -300,7 +300,7 @@ class PostRevisor
return unless revision = PostRevision.find_by(post_id: @post.id, number: @post.version) return unless revision = PostRevision.find_by(post_id: @post.id, number: @post.version)
revision.user_id = @post.last_editor_id revision.user_id = @post.last_editor_id
modifications = post_changes.merge(@topic_changes.diff) modifications = post_changes.merge(@topic_changes.diff)
modifications.keys.each do |field| modifications.each_key do |field|
if revision.modifications.has_key?(field) if revision.modifications.has_key?(field)
old_value = revision.modifications[field][0] old_value = revision.modifications[field][0]
new_value = modifications[field][1] new_value = modifications[field][1]

View file

@ -33,7 +33,7 @@ class ScoreCalculator
def update_posts_score(min_topic_age) def update_posts_score(min_topic_age)
components = [] components = []
@weightings.keys.each { |k| components << "COALESCE(#{k}, 0) * :#{k}" } @weightings.each_key { |k| components << "COALESCE(#{k}, 0) * :#{k}" }
components = components.join(" + ") components = components.join(" + ")
builder = SqlBuilder.new( builder = SqlBuilder.new(

View file

@ -16,7 +16,7 @@ class SiteSettings::YamlLoader
def load def load
yaml = YAML.load_file(@file) yaml = YAML.load_file(@file)
yaml.keys.each do |category| yaml.each_key do |category|
yaml[category].each do |setting_name, hash| yaml[category].each do |setting_name, hash|
if hash.is_a?(Hash) if hash.is_a?(Hash)
# Get default value for the site setting: # Get default value for the site setting:

View file

@ -16,7 +16,7 @@ describe "i18n integrity checks" do
end end
it "needs an i18n key (notification_types) for each Notification type" do it "needs an i18n key (notification_types) for each Notification type" do
Notification.types.keys.each do |type| Notification.types.each_key do |type|
I18n.t("notification_types.#{type}").should_not =~ /translation missing/ I18n.t("notification_types.#{type}").should_not =~ /translation missing/
end end
end end