Skip to content

Commit

Permalink
Buffer 64k in ZipTricks IO to improve performance (#21)
Browse files Browse the repository at this point in the history
The zip_tricks gem uses less internal buffering than RubyZip, so wee
need to do some buffering of our own to get equal or better performance
than with the RubyZip IO for small rows.
  • Loading branch information
felixbuenemann authored Oct 31, 2017
1 parent 1752e2c commit 17f964a
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions lib/xlsxtream/io/zip_tricks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,42 @@
module Xlsxtream
module IO
class ZipTricks
BUFFER_SIZE = 64 * 1024

def initialize(body)
@streamer = ::ZipTricks::Streamer.new(body)
@wf = nil
@buffer = ''
end

def <<(data)
@wf << data
@buffer << data
flush_buffer if @buffer.size >= BUFFER_SIZE
self
end

def add_file(path)
@wf.close if @wf
flush_file
@wf = @streamer.write_deflated_file(path)
end

def close
@wf.close if @wf
flush_file
@streamer.close
end

private

def flush_buffer
@wf << @buffer
@buffer.clear
end

def flush_file
return unless @wf
flush_buffer if @buffer.size > 0
@wf.close
end
end
end
end

0 comments on commit 17f964a

Please sign in to comment.