Optimize map { ... }.compact calls (#15513)

* Optimize map { ... }.compact

using Enumerable#filter_map, supported since Ruby 2.7

* Add poyfill for Enumerable#filter_map
This commit is contained in:
luigi
2021-01-09 18:32:01 -05:00
committed by GitHub
parent 9395143126
commit 087ed84367
22 changed files with 52 additions and 26 deletions

View File

@ -508,7 +508,7 @@ class Account < ApplicationRecord
def from_text(text)
return [] if text.blank?
text.scan(MENTION_RE).map { |match| match.first.split('@', 2) }.uniq.map do |(username, domain)|
text.scan(MENTION_RE).map { |match| match.first.split('@', 2) }.uniq.filter_map do |(username, domain)|
domain = begin
if TagManager.instance.local_domain?(domain)
nil
@ -517,7 +517,7 @@ class Account < ApplicationRecord
end
end
EntityCache.instance.mention(username, domain)
end.compact
end
end
private

View File

@ -83,7 +83,7 @@ module StatusThreadingConcern
def find_statuses_from_tree_path(ids, account, promote: false)
statuses = Status.with_accounts(ids).to_a
account_ids = statuses.map(&:account_id).uniq
domains = statuses.map(&:account_domain).compact.uniq
domains = statuses.filter_map(&:account_domain).uniq
relations = relations_map_for_account(account, account_ids, domains)
statuses.reject! { |status| StatusFilter.new(status, account, relations).filtered? }

View File

@ -46,7 +46,7 @@ class CustomFilter < ApplicationRecord
private
def clean_up_contexts
self.context = Array(context).map(&:strip).map(&:presence).compact
self.context = Array(context).map(&:strip).filter_map(&:presence)
end
def remove_cache

View File

@ -92,7 +92,7 @@ class Notification < ApplicationRecord
end
def reload_stale_associations!(cached_items)
account_ids = (cached_items.map(&:from_account_id) + cached_items.map { |item| item.target_status&.account_id }.compact).uniq
account_ids = (cached_items.map(&:from_account_id) + cached_items.filter_map { |item| item.target_status&.account_id }).uniq
return if account_ids.empty?

View File

@ -334,7 +334,7 @@ class Status < ApplicationRecord
def from_text(text)
return [] if text.blank?
text.scan(FetchLinkCardService::URL_PATTERN).map(&:first).uniq.map do |url|
text.scan(FetchLinkCardService::URL_PATTERN).map(&:first).uniq.filter_map do |url|
status = begin
if TagManager.instance.local_url?(url)
ActivityPub::TagManager.instance.uri_to_resource(url, Status)
@ -343,7 +343,7 @@ class Status < ApplicationRecord
end
end
status&.distributable? ? status : nil
end.compact
end
end
end