Commit fddb803e authored by Michael Kozono's avatar Michael Kozono

Merge branch '327006-fix-search-commits-n-plus-1' into 'master'

Fix N+1 for searching commits

See merge request gitlab-org/gitlab!58867
parents 8f0ef28e 2d144568
......@@ -17,12 +17,13 @@ module RendersCommits
def set_commits_for_rendering(commits, commits_count: nil)
@total_commit_count = commits_count || commits.size
limited, @hidden_commit_count = limited_commits(commits, @total_commit_count)
commits.each(&:lazy_author) # preload authors
prepare_commits_for_rendering(limited)
end
# rubocop: enable Gitlab/ModuleWithInstanceVariables
def prepare_commits_for_rendering(commits)
commits.each(&:lazy_author) # preload commits' authors
Banzai::CommitRenderer.render(commits, @project, current_user) # rubocop:disable Gitlab/ModuleWithInstanceVariables
commits
......
---
title: Fix N+1 for searching commits
merge_request: 58867
author:
type: performance
......@@ -57,4 +57,16 @@ RSpec.describe RendersCommits do
end.not_to exceed_all_query_limit(control_count)
end
end
describe '.prepare_commits_for_rendering' do
it 'avoids N+1' do
control = ActiveRecord::QueryRecorder.new do
subject.prepare_commits_for_rendering(merge_request.commits.take(1))
end
expect do
subject.prepare_commits_for_rendering(merge_request.commits)
end.not_to exceed_all_query_limit(control.count)
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