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
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
22 changed files with 52 additions and 26 deletions

View file

@ -24,8 +24,7 @@ class ActivityPub::FetchFeaturedCollectionService < BaseService
def process_items(items)
status_ids = items.map { |item| value_or_id(item) }
.reject { |uri| ActivityPub::TagManager.instance.local_uri?(uri) }
.map { |uri| ActivityPub::FetchRemoteStatusService.new.call(uri) }
.compact
.filter_map { |uri| ActivityPub::FetchRemoteStatusService.new.call(uri) }
.select { |status| status.account_id == @account.id }
.map(&:id)

View file

@ -37,7 +37,7 @@ class ActivityPub::ProcessCollectionService < BaseService
end
def process_items(items)
items.reverse_each.map { |item| process_item(item) }.compact
items.reverse_each.filter_map { |item| process_item(item) }
end
def supported_context?

View file

@ -30,7 +30,7 @@ class ActivityPub::ProcessPollService < BaseService
voters_count = @json['votersCount']
latest_options = items.map { |item| item['name'].presence || item['content'] }.compact
latest_options = items.filter_map { |item| item['name'].presence || item['content'] }
# If for some reasons the options were changed, it invalidates all previous
# votes, so we need to remove them

View file

@ -14,7 +14,7 @@ class ActivityPub::SynchronizeFollowersService < BaseService
# should never happen in practice, since in almost all cases we keep an
# Account record, and should we not do that, we should have sent a Delete.
# In any case there is not much we can do if that occurs.
@expected_followers = items.map { |uri| ActivityPub::TagManager.instance.uri_to_resource(uri, Account) }.compact
@expected_followers = items.filter_map { |uri| ActivityPub::TagManager.instance.uri_to_resource(uri, Account) }
remove_unexpected_local_followers!
handle_unexpected_outgoing_follows!

View file

@ -67,7 +67,7 @@ class FetchLinkCardService < BaseService
else
html = Nokogiri::HTML(@status.text)
links = html.css('a')
urls = links.map { |a| Addressable::URI.parse(a['href']) unless skip_link?(a) }.compact.map(&:normalize).compact
urls = links.filter_map { |a| Addressable::URI.parse(a['href']) unless skip_link?(a) }.filter_map(&:normalize)
end
urls.reject { |uri| bad_url?(uri) }.first

View file

@ -107,12 +107,12 @@ class ImportService < BaseService
end
end
statuses = items.map do |uri|
statuses = items.filter_map do |uri|
status = ActivityPub::TagManager.instance.uri_to_resource(uri, Status)
next if status.nil? && ActivityPub::TagManager.instance.local_uri?(uri)
status || ActivityPub::FetchRemoteStatusService.new.call(uri)
end.compact
end
account_ids = statuses.map(&:account_id)
preloaded_relations = relations_map_for_account(@account, account_ids)