Commit 081264f1 authored by Stan Hu's avatar Stan Hu

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
parent ba38931d
...@@ -20,7 +20,6 @@ class ProjectWiki ...@@ -20,7 +20,6 @@ class ProjectWiki
@user = user @user = user
end end
delegate :empty?, to: :pages
delegate :repository_storage, :hashed_storage?, to: :project delegate :repository_storage, :hashed_storage?, to: :project
def path def path
...@@ -74,6 +73,10 @@ class ProjectWiki ...@@ -74,6 +73,10 @@ class ProjectWiki
!!find_page('home') !!find_page('home')
end end
def empty?
pages(limit: 1).empty?
end
# Returns an Array of Gitlab WikiPage instances or an # Returns an Array of Gitlab WikiPage instances or an
# empty Array if this Wiki has no pages. # empty Array if this Wiki has no pages.
def pages(limit: nil) def pages(limit: nil)
......
---
title: Optimize ProjectWiki#empty? check
merge_request: 20573
author:
type: performance
# coding: utf-8
require "spec_helper" require "spec_helper"
describe ProjectWiki do describe ProjectWiki do
...@@ -10,7 +11,6 @@ describe ProjectWiki do ...@@ -10,7 +11,6 @@ describe ProjectWiki do
subject { project_wiki } 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(:repository_storage).to :project }
it { is_expected.to delegate_method(:hashed_storage?).to :project } it { is_expected.to delegate_method(:hashed_storage?).to :project }
...@@ -92,11 +92,19 @@ describe ProjectWiki do ...@@ -92,11 +92,19 @@ describe ProjectWiki do
context "when the wiki has pages" do context "when the wiki has pages" do
before do before do
project_wiki.create_page("index", "This is an awesome new Gollum Wiki") project_wiki.create_page("index", "This is an awesome new Gollum Wiki")
project_wiki.create_page("another-page", "This is another page")
end end
describe '#empty?' do describe '#empty?' do
subject { super().empty? } subject { super().empty? }
it { is_expected.to be_falsey } 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 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