Commit 69d0803f authored by Luke Duncalfe's avatar Luke Duncalfe

Merge branch '271242_fix_n_1_in_custom_emoji_filter' into 'master'

Fix N+1 problem in CustomEmojiFilter

See merge request gitlab-org/gitlab!60910
parents 7e9b35dc 625f4071
---
title: Fix N+1 problem in CustomEmojiFilter
merge_request: 60910
author:
type: performance
...@@ -3,6 +3,8 @@ ...@@ -3,6 +3,8 @@
module Banzai module Banzai
module Filter module Filter
class CustomEmojiFilter < HTML::Pipeline::Filter class CustomEmojiFilter < HTML::Pipeline::Filter
include Gitlab::Utils::StrongMemoize
IGNORED_ANCESTOR_TAGS = %w(pre code tt).to_set IGNORED_ANCESTOR_TAGS = %w(pre code tt).to_set
def call def call
...@@ -14,7 +16,7 @@ module Banzai ...@@ -14,7 +16,7 @@ module Banzai
next if has_ancestor?(node, IGNORED_ANCESTOR_TAGS) next if has_ancestor?(node, IGNORED_ANCESTOR_TAGS)
next unless content.include?(':') next unless content.include?(':')
next unless namespace && namespace.custom_emoji.any? next unless has_custom_emoji?
html = custom_emoji_name_element_filter(content) html = custom_emoji_name_element_filter(content)
...@@ -46,6 +48,12 @@ module Banzai ...@@ -46,6 +48,12 @@ module Banzai
private private
def has_custom_emoji?
strong_memoize(:has_custom_emoji) do
namespace&.custom_emoji&.any?
end
end
def namespace def namespace
context[:project].namespace.root_ancestor context[:project].namespace.root_ancestor
end end
......
...@@ -53,7 +53,7 @@ RSpec.describe Banzai::Filter::CustomEmojiFilter do ...@@ -53,7 +53,7 @@ RSpec.describe Banzai::Filter::CustomEmojiFilter do
end end
expect do expect do
filter('<p>:tanuki: :party-parrot:</p>') filter('<p>:tanuki:</p> <p>:party-parrot:</p>')
end.not_to exceed_all_query_limit(control_count.count) end.not_to exceed_all_query_limit(control_count.count)
end end
end end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment