Skip to content

Commit 700af88

Browse files
committed
Ensure that we don't flush too often when deflating.
When fixing rubyzip#322 we initially flushed on every write, but this is bad for compression perfomance (size-wise). So this commit ensures we only flush when we're running under JRuby. Fixes rubyzip#322
1 parent 58d4d97 commit 700af88

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

lib/zip.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ module Zip
6060
restore_times: true
6161
}.freeze # :nodoc:
6262

63+
# :nodoc:
64+
# Remove this when JRuby#3962 is fixed.
65+
ZLIB_FLUSHING_STRATEGY = defined?(JRUBY_VERSION) ? Zlib::SYNC_FLUSH : Zlib::NO_FLUSH
66+
6367
def reset! # :nodoc:
6468
@_ran_once = false
6569
@unicode_names = false

lib/zip/deflater.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ def <<(data)
1515
val = data.to_s
1616
@crc = Zlib.crc32(val, @crc)
1717
@size += val.bytesize
18-
buffer = @zlib_deflater.deflate(data, Zlib::SYNC_FLUSH)
18+
19+
# When JRuby#3962 is fixed, we can remove the flushing parameter here.
20+
buffer = @zlib_deflater.deflate(data, Zip::ZLIB_FLUSHING_STRATEGY)
1921
return @output_stream if buffer.empty?
2022

2123
@output_stream << @encrypter.encrypt(buffer)

test/deflater_test.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,15 @@ def teardown
1414
Zip.reset!
1515
end
1616

17+
# Remove this test when JRuby#3962 is fixed.
18+
def test_deflate_strategy
19+
if defined?(JRUBY_VERSION)
20+
assert_equal(Zlib::SYNC_FLUSH, Zip::ZLIB_FLUSHING_STRATEGY)
21+
else
22+
assert_equal(Zlib::NO_FLUSH, Zip::ZLIB_FLUSHING_STRATEGY)
23+
end
24+
end
25+
1726
def test_output_operator
1827
txt = load_file('test/data/file2.txt')
1928
deflate(txt, DEFLATER_TEST_FILE)

0 commit comments

Comments
 (0)