summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStan Hu <stanhu@gmail.com>2018-04-14 14:34:39 -0700
committerStan Hu <stanhu@gmail.com>2018-04-14 14:37:28 -0700
commitb1f15dfa42fb7b8f74a439806f004eaa5ed598a8 (patch)
tree8a8794cf8a991fee55f7e55ddbd3bcdb091edd3e
parent160f1fd225944b4cedd9d5223cdc55b2f3ed0ea7 (diff)
downloadgitlab-ce-b1f15dfa42fb7b8f74a439806f004eaa5ed598a8.tar.gz
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.
-rw-r--r--changelogs/unreleased/sh-memoize-repository-empty.yml5
-rw-r--r--lib/gitlab/git/repository.rb7
-rw-r--r--spec/lib/gitlab/git/repository_spec.rb12
3 files changed, 23 insertions, 1 deletions
diff --git a/changelogs/unreleased/sh-memoize-repository-empty.yml b/changelogs/unreleased/sh-memoize-repository-empty.yml
new file mode 100644
index 00000000000..64db3ca0371
--- /dev/null
+++ b/changelogs/unreleased/sh-memoize-repository-empty.yml
@@ -0,0 +1,5 @@
+---
+title: Memoize Git::Repository#has_visible_content?
+merge_request:
+author:
+type: performance
diff --git a/lib/gitlab/git/repository.rb b/lib/gitlab/git/repository.rb
index 11a421cb430..61e92a78ab9 100644
--- a/lib/gitlab/git/repository.rb
+++ b/lib/gitlab/git/repository.rb
@@ -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?
diff --git a/spec/lib/gitlab/git/repository_spec.rb b/spec/lib/gitlab/git/repository_spec.rb
index 1e00e8d2739..f6bf7a162f6 100644
--- a/spec/lib/gitlab/git/repository_spec.rb
+++ b/spec/lib/gitlab/git/repository_spec.rb
@@ -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