related_tag_calculator.rb: fix memcache lookup in inner loop.

Remove the category constraint option from RelatedTagCalculator.calculate_from_posts.
It slows things down and isn't used.

This method is used to calculate the related tags sidebar during
searches for single metatags. Using Tag.category_for in the inner loop
caused a memcache call on every iteration. At 100 posts per page and
20-30 tags per post, this led to up to 2000-3000 total memcache calls,
which significantly slowed pageloads.
This commit is contained in:
evazion
2017-04-23 13:35:18 -05:00
parent 76eefd0ffe
commit 8404064854
3 changed files with 13 additions and 19 deletions

View File

@@ -11,24 +11,15 @@ class RelatedTagCalculator
convert_hash_to_array(calculate_from_sample(tags, Danbooru.config.post_sample_size, category_constraint))
end
def self.calculate_from_post_set_to_array(post_set, category_constraint = nil)
convert_hash_to_array(calculate_from_post_set(post_set, category_constraint))
def self.calculate_from_posts_to_array(posts)
convert_hash_to_array(calculate_from_posts(posts))
end
def self.calculate_from_post_set(post_set, category_constraint = nil)
def self.calculate_from_posts(posts)
counts = Hash.new {|h, k| h[k] = 0}
post_set.posts.each do |post|
post.tag_array.each do |tag|
category = Tag.category_for(tag)
if category_constraint
if category == category_constraint
counts[tag] += 1
end
else
counts[tag] += 1
end
end
posts.flat_map(&:tag_array).each do |tag|
counts[tag] += 1
end
counts