Only serializes diff files found by paths query

This commit is contained in:
Oswaldo Ferreira 2018-07-31 12:47:22 -03:00
parent abae261b86
commit fc4a0cd055
3 changed files with 15 additions and 5 deletions

View File

@ -249,15 +249,13 @@ class MergeRequestDiff < ActiveRecord::Base
end
def load_diffs(options)
raw = merge_request_diff_files.map(&:to_hash)
collection = merge_request_diff_files
if paths = options[:paths]
raw = raw.select do |diff|
paths.include?(diff[:old_path]) || paths.include?(diff[:new_path])
end
collection = collection.where('old_path IN (?) OR new_path IN (?)', paths, paths)
end
Gitlab::Git::DiffCollection.new(raw, options)
Gitlab::Git::DiffCollection.new(collection.map(&:to_hash), options)
end
def load_commits

View File

@ -0,0 +1,5 @@
---
title: Improve performance when fetching collapsed diffs and commenting in merge requests
merge_request: 20940
author:
type: performance

View File

@ -127,6 +127,13 @@ describe MergeRequestDiff do
expect(diffs.map(&:new_path)).to contain_exactly('files/ruby/popen.rb')
end
it 'only serializes diff files found by query' do
expect(diff_with_commits.merge_request_diff_files.count).to be > 10
expect_any_instance_of(MergeRequestDiffFile).to receive(:to_hash).once
diffs
end
it 'uses the diffs from the DB' do
expect(diff_with_commits).to receive(:load_diffs)