Commit b1f15dfa authored by Stan Hu's avatar Stan Hu

Memoize Git::Repository#has_visible_content?

This is called repeatedly when viewing a merge request, and this should
improve performance significantly by avoiding shelling out to git every time.

This should help https://gitlab.com/gitlab-com/infrastructure/issues/4027.
parent 160f1fd2
---
title: Memoize Git::Repository#has_visible_content?
merge_request:
author:
type: performance
......@@ -9,6 +9,7 @@ module Gitlab
include Gitlab::Git::RepositoryMirroring
include Gitlab::Git::Popen
include Gitlab::EncodingHelper
include Gitlab::Utils::StrongMemoize
ALLOWED_OBJECT_DIRECTORIES_VARIABLES = %w[
GIT_OBJECT_DIRECTORY
......@@ -232,6 +233,12 @@ module Gitlab
end
def has_local_branches?
strong_memoize(:has_local_branches) do
uncached_has_local_branches?
end
end
def uncached_has_local_branches?
gitaly_migrate(:has_local_branches, status: Gitlab::GitalyClient::MigrationStatus::OPT_OUT) do |is_enabled|
if is_enabled
gitaly_repository_client.has_local_branches?
......
......@@ -463,7 +463,7 @@ describe Gitlab::Git::Repository, seed_helper: true do
it 'returns false when there are no branches' do
# Sanity check
expect(repository.has_local_branches?).to eq(true)
expect(repository.uncached_has_local_branches?).to eq(true)
FileUtils.rm_rf(File.join(repository.path, 'packed-refs'))
heads_dir = File.join(repository.path, 'refs/heads')
......@@ -473,6 +473,16 @@ describe Gitlab::Git::Repository, seed_helper: true do
expect(repository.has_local_branches?).to eq(false)
end
end
context 'memoizes the value' do
it 'returns true' do
expect(repository).to receive(:uncached_has_local_branches?).once.and_call_original
2.times do
expect(repository.has_local_branches?).to eq(true)
end
end
end
end
context 'with gitaly' do
......
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