Skip to content

Commit

Permalink
Reset Deflater/Inflater after Use in DeflateCompressor (#65617) (#65646)
Browse files Browse the repository at this point in the history
We should reset after use, not before reuse. Otherwise we keep the input buffers
on these objects around for a long time and they can grow to O(MB).
  • Loading branch information
original-brownbear authored Dec 1, 2020
1 parent bb0fcb1 commit 6bbeedc
Showing 1 changed file with 5 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -229,9 +229,10 @@ public void close() throws IOException {
public BytesReference uncompress(BytesReference bytesReference) throws IOException {
final BytesStreamOutput buffer = baos.get();
final Inflater inflater = inflaterRef.get();
inflater.reset();
try (InflaterOutputStream ios = new InflaterOutputStream(buffer, inflater)) {
bytesReference.slice(HEADER.length, bytesReference.length() - HEADER.length).writeTo(ios);
} finally {
inflater.reset();
}
final BytesReference res = buffer.copyBytes();
buffer.reset();
Expand All @@ -245,11 +246,12 @@ public BytesReference uncompress(BytesReference bytesReference) throws IOExcepti
@Override
public BytesReference compress(BytesReference bytesReference) throws IOException {
final BytesStreamOutput buffer = baos.get();
final Deflater deflater = deflaterRef.get();
deflater.reset();
buffer.write(HEADER);
final Deflater deflater = deflaterRef.get();
try (DeflaterOutputStream dos = new DeflaterOutputStream(buffer, deflater, true)) {
bytesReference.writeTo(dos);
} finally {
deflater.reset();
}
final BytesReference res = buffer.copyBytes();
buffer.reset();
Expand Down

0 comments on commit 6bbeedc

Please sign in to comment.