Commit 625f4071 authored by Vasilii Iakliushin's avatar Vasilii Iakliushin Committed by Luke Duncalfe

Fix N+1 problem in CustomEmojiFilter

Contributes to https://gitlab.com/gitlab-org/gitlab/-/issues/271242

**Problem**

I discovered repetitive `SELECT 1 AS one FROM "custom_emoji" WHERE
"custom_emoji"."namespace_id" = <id> LIMIT 1` queries generated by
CustomEmojiFilter.

**Solution**

- Update test to catch N+1 queries
- Add memoization to prevent them
parent 7e9b35dc
---
title: Fix N+1 problem in CustomEmojiFilter
merge_request: 60910
author:
type: performance
......@@ -3,6 +3,8 @@
module Banzai
module Filter
class CustomEmojiFilter < HTML::Pipeline::Filter
include Gitlab::Utils::StrongMemoize
IGNORED_ANCESTOR_TAGS = %w(pre code tt).to_set
def call
......@@ -14,7 +16,7 @@ module Banzai
next if has_ancestor?(node, IGNORED_ANCESTOR_TAGS)
next unless content.include?(':')
next unless namespace && namespace.custom_emoji.any?
next unless has_custom_emoji?
html = custom_emoji_name_element_filter(content)
......@@ -46,6 +48,12 @@ module Banzai
private
def has_custom_emoji?
strong_memoize(:has_custom_emoji) do
namespace&.custom_emoji&.any?
end
end
def namespace
context[:project].namespace.root_ancestor
end
......
......@@ -53,7 +53,7 @@ RSpec.describe Banzai::Filter::CustomEmojiFilter do
end
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
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