Commit 16168b5b authored by Jacob Vosmaer's avatar Jacob Vosmaer

Use the iterator inside DiffCollection only once

parent fcdc4279
...@@ -19,22 +19,19 @@ module Gitlab ...@@ -19,22 +19,19 @@ module Gitlab
@line_count = 0 @line_count = 0
@byte_count = 0 @byte_count = 0
@overflow = false @overflow = false
@empty = true
@array = Array.new @array = Array.new
end end
def each(&block) def each(&block)
if @populated Gitlab::GitalyClient.migrate(:commit_raw_diffs) do
# @iterator.each is slower than just iterating the array in place each_patch(&block)
@array.each(&block)
else
Gitlab::GitalyClient.migrate(:commit_raw_diffs) do
each_patch(&block)
end
end end
end end
def empty? def empty?
!@iterator.any? any? # Make sure the iterator has been exercised
@empty
end end
def overflow? def overflow?
...@@ -60,7 +57,6 @@ module Gitlab ...@@ -60,7 +57,6 @@ module Gitlab
collection = each_with_index do |element, i| collection = each_with_index do |element, i|
@array[i] = yield(element) @array[i] = yield(element)
end end
@populated = true
collection collection
end end
...@@ -70,7 +66,6 @@ module Gitlab ...@@ -70,7 +66,6 @@ module Gitlab
return if @populated return if @populated
each { nil } # force a loop through all diffs each { nil } # force a loop through all diffs
@populated = true
nil nil
end end
...@@ -79,15 +74,17 @@ module Gitlab ...@@ -79,15 +74,17 @@ module Gitlab
end end
def each_patch def each_patch
@iterator.each_with_index do |raw, i| i = 0
# First yield cached Diff instances from @array @array.each do |diff|
if @array[i] yield diff
yield @array[i] i += 1
next end
end
return if @overflow
return if @iterator.nil?
# We have exhausted @array, time to create new Diff instances or stop. @iterator.each do |raw|
break if @overflow @empty = false
if !@all_diffs && i >= @max_files if !@all_diffs && i >= @max_files
@overflow = true @overflow = true
...@@ -113,7 +110,13 @@ module Gitlab ...@@ -113,7 +110,13 @@ module Gitlab
end end
yield @array[i] = diff yield @array[i] = diff
i += 1
end end
@populated = true
# Allow iterator to be garbage-collected. It cannot be reused anyway.
@iterator = nil
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