Skip to content

Commit

Permalink
Merge pull request #2527 from ganmacs/use-slice-to-speed-up
Browse files Browse the repository at this point in the history
Replace in_tail with a faster implementation.
Signed-off-by: Masahiro Nakagawa <[email protected]>
  • Loading branch information
repeatedly committed Jul 29, 2019
1 parent 1b77e82 commit 61d9fa5
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions lib/fluent/plugin/in_tail.rb
Original file line number Diff line number Diff line change
Expand Up @@ -698,9 +698,18 @@ def convert(s)
s.encode!(@encoding, @from_encoding, :invalid => :replace, :undef => :replace)
end

def next_line
def read_lines(lines)
idx = @buffer.index(@eol)
convert(@buffer.slice!(0, idx + 1)) unless idx.nil?

until idx.nil?
# Using freeze and slice is faster than slice!
# See https://github.com/fluent/fluentd/pull/2527
@buffer.freeze
rbuf = @buffer.slice(0, idx + 1)
@buffer = @buffer.slice(idx + 1, @buffer.size)
idx = @buffer.index(@eol)
lines << convert(rbuf)
end
end

def bytesize
Expand Down Expand Up @@ -733,9 +742,7 @@ def handle_notify
begin
while true
@fifo << io.readpartial(8192, @iobuf)
while (line = @fifo.next_line)
@lines << line
end
@fifo.read_lines(@lines)
if @lines.size >= @watcher.read_lines_limit
# not to use too much memory in case the file is very large
read_more = true
Expand Down

0 comments on commit 61d9fa5

Please sign in to comment.