Remove intermediary arrays when creating hash maps from results (#9291)

master
Eugen Rochko 6 years ago committed by GitHub
parent 367ad21995
commit 8069fd636b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      app/controllers/application_controller.rb
  2. 2
      app/lib/entity_cache.rb
  3. 4
      app/lib/formatter.rb
  4. 4
      app/lib/settings/scoped_settings.rb
  5. 4
      app/models/concerns/account_interactions.rb
  6. 2
      app/models/notification.rb
  7. 2
      app/models/setting.rb
  8. 10
      app/models/status.rb
  9. 2
      app/models/trending_tags.rb
  10. 6
      app/services/batched_remove_status_service.rb
  11. 2
      spec/models/notification_spec.rb

@ -113,7 +113,7 @@ class ApplicationController < ActionController::Base
klass.reload_stale_associations!(cached_keys_with_value.values) if klass.respond_to?(:reload_stale_associations!) klass.reload_stale_associations!(cached_keys_with_value.values) if klass.respond_to?(:reload_stale_associations!)
unless uncached_ids.empty? unless uncached_ids.empty?
uncached = klass.where(id: uncached_ids).with_includes.map { |item| [item.id, item] }.to_h uncached = klass.where(id: uncached_ids).with_includes.each_with_object({}) { |item, h| h[item.id] = item }
uncached.each_value do |item| uncached.each_value do |item|
Rails.cache.write(item, item) Rails.cache.write(item, item)

@ -21,7 +21,7 @@ class EntityCache
end end
unless uncached_ids.empty? unless uncached_ids.empty?
uncached = CustomEmoji.where(shortcode: shortcodes, domain: domain, disabled: false).map { |item| [item.shortcode, item] }.to_h uncached = CustomEmoji.where(shortcode: shortcodes, domain: domain, disabled: false).each_with_object({}) { |item, h| h[item.shortcode] = item }
uncached.each_value { |item| Rails.cache.write(to_key(:emoji, item.shortcode, domain), item, expires_in: MAX_EXPIRATION) } uncached.each_value { |item| Rails.cache.write(to_key(:emoji, item.shortcode, domain), item, expires_in: MAX_EXPIRATION) }
end end

@ -128,9 +128,9 @@ class Formatter
return html if emojis.empty? return html if emojis.empty?
emoji_map = if animate emoji_map = if animate
emojis.map { |e| [e.shortcode, full_asset_url(e.image.url)] }.to_h emojis.each_with_object({}) { |e, h| h[e.shortcode] = full_asset_url(e.image.url) }
else else
emojis.map { |e| [e.shortcode, full_asset_url(e.image.url(:static))] }.to_h emojis.each_with_object({}) { |e, h| h[e.shortcode] = full_asset_url(e.image.url(:static)) }
end end
i = -1 i = -1

@ -31,7 +31,7 @@ module Settings
def all_as_records def all_as_records
vars = thing_scoped vars = thing_scoped
records = vars.map { |r| [r.var, r] }.to_h records = vars.each_with_object({}) { |r, h| h[r.var] = r }
Setting.default_settings.each do |key, default_value| Setting.default_settings.each do |key, default_value|
next if records.key?(key) || default_value.is_a?(Hash) next if records.key?(key) || default_value.is_a?(Hash)
@ -65,7 +65,7 @@ module Settings
class << self class << self
def default_settings def default_settings
defaulting = DEFAULTING_TO_UNSCOPED.map { |k| [k, Setting[k]] }.to_h defaulting = DEFAULTING_TO_UNSCOPED.each_with_object({}) { |k, h| h[k] = Setting[k] }
Setting.default_settings.merge!(defaulting) Setting.default_settings.merge!(defaulting)
end end
end end

@ -45,9 +45,9 @@ module AccountInteractions
end end
def domain_blocking_map(target_account_ids, account_id) def domain_blocking_map(target_account_ids, account_id)
accounts_map = Account.where(id: target_account_ids).select('id, domain').map { |a| [a.id, a.domain] }.to_h accounts_map = Account.where(id: target_account_ids).select('id, domain').each_with_object({}) { |a, h| h[a.id] = a.domain }
blocked_domains = domain_blocking_map_by_domain(accounts_map.values.compact, account_id) blocked_domains = domain_blocking_map_by_domain(accounts_map.values.compact, account_id)
accounts_map.map { |id, domain| [id, blocked_domains[domain]] }.to_h accounts_map.reduce({}) { |h, (id, domain)| h.merge(id => blocked_domains[domain]) }
end end
def domain_blocking_map_by_domain(target_domains, account_id) def domain_blocking_map_by_domain(target_domains, account_id)

@ -75,7 +75,7 @@ class Notification < ApplicationRecord
return if account_ids.empty? return if account_ids.empty?
accounts = Account.where(id: account_ids).map { |a| [a.id, a] }.to_h accounts = Account.where(id: account_ids).each_with_object({}) { |a, h| h[a.id] = a }
cached_items.each do |item| cached_items.each do |item|
item.from_account = accounts[item.from_account_id] item.from_account = accounts[item.from_account_id]

@ -40,7 +40,7 @@ class Setting < RailsSettings::Base
def all_as_records def all_as_records
vars = thing_scoped vars = thing_scoped
records = vars.map { |r| [r.var, r] }.to_h records = vars.each_with_object({}) { |r, h| h[r.var] = r }
default_settings.each do |key, default_value| default_settings.each do |key, default_value|
next if records.key?(key) || default_value.is_a?(Hash) next if records.key?(key) || default_value.is_a?(Hash)

@ -321,19 +321,19 @@ class Status < ApplicationRecord
end end
def favourites_map(status_ids, account_id) def favourites_map(status_ids, account_id)
Favourite.select('status_id').where(status_id: status_ids).where(account_id: account_id).map { |f| [f.status_id, true] }.to_h Favourite.select('status_id').where(status_id: status_ids).where(account_id: account_id).each_with_object({}) { |f, h| h[f.status_id] = true }
end end
def reblogs_map(status_ids, account_id) def reblogs_map(status_ids, account_id)
select('reblog_of_id').where(reblog_of_id: status_ids).where(account_id: account_id).reorder(nil).map { |s| [s.reblog_of_id, true] }.to_h select('reblog_of_id').where(reblog_of_id: status_ids).where(account_id: account_id).reorder(nil).each_with_object({}) { |s, h| h[s.reblog_of_id] = true }
end end
def mutes_map(conversation_ids, account_id) def mutes_map(conversation_ids, account_id)
ConversationMute.select('conversation_id').where(conversation_id: conversation_ids).where(account_id: account_id).map { |m| [m.conversation_id, true] }.to_h ConversationMute.select('conversation_id').where(conversation_id: conversation_ids).where(account_id: account_id).each_with_object({}) { |m, h| h[m.conversation_id] = true }
end end
def pins_map(status_ids, account_id) def pins_map(status_ids, account_id)
StatusPin.select('status_id').where(status_id: status_ids).where(account_id: account_id).map { |p| [p.status_id, true] }.to_h StatusPin.select('status_id').where(status_id: status_ids).where(account_id: account_id).each_with_object({}) { |p, h| h[p.status_id] = true }
end end
def reload_stale_associations!(cached_items) def reload_stale_associations!(cached_items)
@ -348,7 +348,7 @@ class Status < ApplicationRecord
return if account_ids.empty? return if account_ids.empty?
accounts = Account.where(id: account_ids).map { |a| [a.id, a] }.to_h accounts = Account.where(id: account_ids).each_with_object({}) { |a, h| h[a.id] = a }
cached_items.each do |item| cached_items.each do |item|
item.account = accounts[item.account_id] item.account = accounts[item.account_id]

@ -18,7 +18,7 @@ class TrendingTags
def get(limit) def get(limit)
key = "#{KEY}:#{Time.now.utc.beginning_of_day.to_i}" key = "#{KEY}:#{Time.now.utc.beginning_of_day.to_i}"
tag_ids = redis.zrevrange(key, 0, limit - 1).map(&:to_i) tag_ids = redis.zrevrange(key, 0, limit - 1).map(&:to_i)
tags = Tag.where(id: tag_ids).to_a.map { |tag| [tag.id, tag] }.to_h tags = Tag.where(id: tag_ids).to_a.each_with_object({}) { |tag, h| h[tag.id] = tag }
tag_ids.map { |tag_id| tags[tag_id] }.compact tag_ids.map { |tag_id| tags[tag_id] }.compact
end end

@ -12,12 +12,12 @@ class BatchedRemoveStatusService < BaseService
def call(statuses) def call(statuses)
statuses = Status.where(id: statuses.map(&:id)).includes(:account, :stream_entry).flat_map { |status| [status] + status.reblogs.includes(:account, :stream_entry).to_a } statuses = Status.where(id: statuses.map(&:id)).includes(:account, :stream_entry).flat_map { |status| [status] + status.reblogs.includes(:account, :stream_entry).to_a }
@mentions = statuses.map { |s| [s.id, s.active_mentions.includes(:account).to_a] }.to_h @mentions = statuses.each_with_object({}) { |s, h| h[s.id] = s.active_mentions.includes(:account).to_a }
@tags = statuses.map { |s| [s.id, s.tags.pluck(:name)] }.to_h @tags = statuses.each_with_object({}) { |s, h| h[s.id] = s.tags.pluck(:name) }
@stream_entry_batches = [] @stream_entry_batches = []
@salmon_batches = [] @salmon_batches = []
@json_payloads = statuses.map { |s| [s.id, Oj.dump(event: :delete, payload: s.id.to_s)] }.to_h @json_payloads = statuses.each_with_object({}) { |s, h| h[s.id] = Oj.dump(event: :delete, payload: s.id.to_s) }
@activity_xml = {} @activity_xml = {}
# Ensure that rendered XML reflects destroyed state # Ensure that rendered XML reflects destroyed state

@ -101,7 +101,7 @@ RSpec.describe Notification, type: :model do
before do before do
allow(accounts_with_ids).to receive(:[]).with(stale_account1.id).and_return(account1) allow(accounts_with_ids).to receive(:[]).with(stale_account1.id).and_return(account1)
allow(accounts_with_ids).to receive(:[]).with(stale_account2.id).and_return(account2) allow(accounts_with_ids).to receive(:[]).with(stale_account2.id).and_return(account2)
allow(Account).to receive_message_chain(:where, :map, :to_h).and_return(accounts_with_ids) allow(Account).to receive_message_chain(:where, :each_with_object).and_return(accounts_with_ids)
end end
let(:cached_items) do let(:cached_items) do

Loading…
Cancel
Save