Commit 2c7652a9 authored by Dylan Griffith's avatar Dylan Griffith

Merge branch 'mc/backstage/optimize-branch-loading' into 'master'

Optimize branch commit resolution

See merge request gitlab-org/gitlab!56204
parents 12b22df9 6d044520
...@@ -7,12 +7,16 @@ module Resolvers ...@@ -7,12 +7,16 @@ module Resolvers
alias_method :branch, :object alias_method :branch, :object
def resolve(**args) def resolve(**args)
return unless branch commit = branch&.dereferenced_target
return unless commit
commit = branch.dereferenced_target lazy_project = BatchLoader::GraphQL.for(commit.repository.gl_project_path).batch do |paths, loader|
project = Project.find_by_full_path(commit.repository.gl_project_path) paths.each { |path| loader.call(path, Project.find_by_full_path(path)) }
end
::Commit.new(commit, project) if commit ::Gitlab::Graphql::Lazy.with_value(lazy_project) do |project|
::Commit.new(commit, project) if project
end
end end
end end
end end
---
title: Optimize branch commit resolution.
merge_request: 56204
author:
type: performance
...@@ -12,11 +12,11 @@ RSpec.describe Resolvers::BranchCommitResolver do ...@@ -12,11 +12,11 @@ RSpec.describe Resolvers::BranchCommitResolver do
describe '#resolve' do describe '#resolve' do
it 'resolves commit' do it 'resolves commit' do
is_expected.to eq(repository.commits('master', limit: 1).last) expect(sync(commit)).to eq(repository.commits('master', limit: 1).last)
end end
it 'sets project container' do it 'sets project container' do
expect(commit.container).to eq(repository.project) expect(sync(commit).container).to eq(repository.project)
end end
context 'when branch does not exist' do context 'when branch does not exist' do
...@@ -26,5 +26,19 @@ RSpec.describe Resolvers::BranchCommitResolver do ...@@ -26,5 +26,19 @@ RSpec.describe Resolvers::BranchCommitResolver do
is_expected.to be_nil is_expected.to be_nil
end end
end end
it 'is N+1 safe' do
commit_a = repository.commits('master', limit: 1).last
commit_b = repository.commits('spooky-stuff', limit: 1).last
commits = batch_sync(max_queries: 1) do
[
resolve(described_class, obj: branch),
resolve(described_class, obj: repository.find_branch('spooky-stuff'))
]
end
expect(commits).to contain_exactly(commit_a, commit_b)
end
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