diff options
author | Shinya Maeda <gitlab.shinyamaeda@gmail.com> | 2017-05-17 14:21:13 +0300 |
---|---|---|
committer | Shinya Maeda <gitlab.shinyamaeda@gmail.com> | 2017-05-24 14:10:14 +0300 |
commit | 8c1b07e0cc3e461939cae0a9566cbab172360cdf (patch) | |
tree | ca343791e21736c7411dc319763e5565bf88d98b /lib | |
parent | 6be609dd6418f1688d027f21cfe6fdb167375bc0 (diff) |
Optimize reverse_line
Diffstat (limited to 'lib')
-rw-r--r-- | lib/gitlab/ci/trace/stream.rb | 25 |
1 files changed, 13 insertions, 12 deletions
diff --git a/lib/gitlab/ci/trace/stream.rb b/lib/gitlab/ci/trace/stream.rb index 5777332b251..e8c34f3da05 100644 --- a/lib/gitlab/ci/trace/stream.rb +++ b/lib/gitlab/ci/trace/stream.rb @@ -96,30 +96,31 @@ module Gitlab end def reverse_line - pos = BUFFER_SIZE + pos = 0 max = stream.size debris = '' - while pos < max + while (read_size = calc_read_size(pos, max)) > 0 + pos += read_size stream.seek(-pos, IO::SEEK_END) - stream.read(BUFFER_SIZE).tap do |buf| + stream.read(read_size).tap do |buf| buf = buf + debris debris, *lines = buf.each_line.to_a lines.reverse_each do |line| yield(line) end end - pos += BUFFER_SIZE end - # Reached the head, read only left - stream.seek(0) - last = (max > BUFFER_SIZE) ? (max % BUFFER_SIZE) : max - stream.read(last).tap do |buf| - buf = buf + debris - buf.each_line.reverse_each do |line| - yield(line) - end + yield(debris) if debris != '' + end + + def calc_read_size(pos, max) + if pos > max + BUFFER_SIZE + (pos - max) + else + remain = max - pos + (remain > BUFFER_SIZE) ? BUFFER_SIZE : remain end end end |