Use arrays in Pipeline#latest_builds_with_artifacts

This changes Ci::Pipeline#latest_builds_with_artifacts so it returns an
Array instead of a relation. Whenever we use this data we do so in two
steps:

1. Count the number of rows
2. If this number is greater than 0, iterate over the rows

By returning an Array instead we only execute 1 query of which the total
time/work is less than running either just a COUNT(*) or both queries
(in the worst case).

On GitLab.com this change should save us a few milliseconds per request
to ProjectsController#show.
This commit is contained in:
Yorick Peterse 2017-11-21 17:32:12 +01:00
parent 9024875e1f
commit 54f1e406f4
No known key found for this signature in database
GPG Key ID: EDD30D2BEB691AC9
3 changed files with 13 additions and 1 deletions

View File

@ -507,7 +507,10 @@ module Ci
end
def latest_builds_with_artifacts
@latest_builds_with_artifacts ||= builds.latest.with_artifacts
# We purposely cast the builds to an Array here. Because we always use the
# rows if there are more than 0 this prevents us from having to run two
# queries: one to get the count and one to get the rows.
@latest_builds_with_artifacts ||= builds.latest.with_artifacts.to_a
end
private

View File

@ -0,0 +1,5 @@
---
title: Use arrays in Pipeline#latest_builds_with_artifacts
merge_request:
author:
type: performance

View File

@ -1502,6 +1502,10 @@ describe Ci::Pipeline, :mailer do
create(:ci_build, :success, :artifacts, pipeline: pipeline)
end
it 'returns an Array' do
expect(pipeline.latest_builds_with_artifacts).to be_an_instance_of(Array)
end
it 'returns the latest builds' do
expect(pipeline.latest_builds_with_artifacts).to eq([build])
end