From 081264f18682d7fa823f75737a919e05ea02fe04 Mon Sep 17 00:00:00 2001 From: Stan Hu Date: Wed, 11 Jul 2018 23:19:20 -0700 Subject: Optimize ProjectWiki#empty? check The `empty?` check was iterating over all Wiki pages to determine whether it was empty. Using the limit parameter allows us to bail out early once we found a page. Note that this currently only improves performance for GitLab 11.0 until https://gitlab.com/gitlab-org/gitaly/issues/1204 is fixed. Relates to #40101 --- app/models/project_wiki.rb | 5 ++++- changelogs/unreleased/sh-optimize-wiki-empty-check.yml | 5 +++++ spec/models/project_wiki_spec.rb | 10 +++++++++- 3 files changed, 18 insertions(+), 2 deletions(-) create mode 100644 changelogs/unreleased/sh-optimize-wiki-empty-check.yml diff --git a/app/models/project_wiki.rb b/app/models/project_wiki.rb index fc868c3ebb7..9ae2fb0013a 100644 --- a/app/models/project_wiki.rb +++ b/app/models/project_wiki.rb @@ -20,7 +20,6 @@ class ProjectWiki @user = user end - delegate :empty?, to: :pages delegate :repository_storage, :hashed_storage?, to: :project def path @@ -74,6 +73,10 @@ class ProjectWiki !!find_page('home') end + def empty? + pages(limit: 1).empty? + end + # Returns an Array of Gitlab WikiPage instances or an # empty Array if this Wiki has no pages. def pages(limit: nil) diff --git a/changelogs/unreleased/sh-optimize-wiki-empty-check.yml b/changelogs/unreleased/sh-optimize-wiki-empty-check.yml new file mode 100644 index 00000000000..31ca7497b5a --- /dev/null +++ b/changelogs/unreleased/sh-optimize-wiki-empty-check.yml @@ -0,0 +1,5 @@ +--- +title: Optimize ProjectWiki#empty? check +merge_request: 20573 +author: +type: performance diff --git a/spec/models/project_wiki_spec.rb b/spec/models/project_wiki_spec.rb index a3c20b3b3c1..a544940800a 100644 --- a/spec/models/project_wiki_spec.rb +++ b/spec/models/project_wiki_spec.rb @@ -1,3 +1,4 @@ +# coding: utf-8 require "spec_helper" describe ProjectWiki do @@ -10,7 +11,6 @@ describe ProjectWiki do subject { project_wiki } - it { is_expected.to delegate_method(:empty?).to :pages } it { is_expected.to delegate_method(:repository_storage).to :project } it { is_expected.to delegate_method(:hashed_storage?).to :project } @@ -92,11 +92,19 @@ describe ProjectWiki do context "when the wiki has pages" do before do project_wiki.create_page("index", "This is an awesome new Gollum Wiki") + project_wiki.create_page("another-page", "This is another page") end describe '#empty?' do subject { super().empty? } it { is_expected.to be_falsey } + + # Re-enable this when https://gitlab.com/gitlab-org/gitaly/issues/1204 is fixed + xit 'only instantiates a Wiki page once' do + expect(WikiPage).to receive(:new).once.and_call_original + + subject + end end end end -- cgit v1.2.1