Commit 20fb8ed9 authored by Nick Thomas's avatar Nick Thomas

Merge branch '28211-check-if-mapping-is-empty-before-caching' into 'master'

Optimize Gitlab::DiscussionsDiff::FileCollection#load_highlight when no discussions are present

Closes #28211

See merge request gitlab-org/gitlab!18290
parents 1044e5d4 65998716
---
title: Check if mapping is empty before caching in File Collections
merge_request: 18290
author: briankabiro
type: performance
...@@ -27,12 +27,14 @@ module Gitlab ...@@ -27,12 +27,14 @@ module Gitlab
# - The cache content is not updated (there's no need to do so) # - The cache content is not updated (there's no need to do so)
def load_highlight def load_highlight
ids = highlightable_collection_ids ids = highlightable_collection_ids
return if ids.empty?
cached_content = read_cache(ids) cached_content = read_cache(ids)
uncached_ids = ids.select.each_with_index { |_, i| cached_content[i].nil? } uncached_ids = ids.select.each_with_index { |_, i| cached_content[i].nil? }
mapping = highlighted_lines_by_ids(uncached_ids) mapping = highlighted_lines_by_ids(uncached_ids)
HighlightCache.write_multiple(mapping) HighlightCache.write_multiple(mapping) if mapping.any?
diffs = diff_files_indexed_by_id.values_at(*ids) diffs = diff_files_indexed_by_id.values_at(*ids)
......
...@@ -40,6 +40,14 @@ describe Gitlab::DiscussionsDiff::FileCollection do ...@@ -40,6 +40,14 @@ describe Gitlab::DiscussionsDiff::FileCollection do
subject.load_highlight subject.load_highlight
end end
it 'does not write cache for empty mapping' do
allow(subject).to receive(:highlighted_lines_by_ids).and_return([])
expect(Gitlab::DiscussionsDiff::HighlightCache).not_to receive(:write_multiple)
subject.load_highlight
end
it 'does not write cache for resolved notes' do it 'does not write cache for resolved notes' do
diff_note_a.update_column(:resolved_at, Time.now) diff_note_a.update_column(:resolved_at, Time.now)
......
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