Repository: commons-compress Updated Branches: refs/heads/master 49f654703 -> 5b004bfc1
10% speed improvements and decreased memory allocation rate in deflate64 Project: http://git-wip-us.apache.org/repos/asf/commons-compress/repo Commit: http://git-wip-us.apache.org/repos/asf/commons-compress/commit/90d775ce Tree: http://git-wip-us.apache.org/repos/asf/commons-compress/tree/90d775ce Diff: http://git-wip-us.apache.org/repos/asf/commons-compress/diff/90d775ce Branch: refs/heads/master Commit: 90d775ce2270c35a7c514f5638b8a3b23d5a4f3c Parents: 49f6547 Author: Dawid Weiss <dawid.we...@carrotsearch.com> Authored: Thu Jan 11 18:11:11 2018 +0100 Committer: Stefan Bodewig <bode...@apache.org> Committed: Fri Jan 12 17:22:13 2018 +0100 ---------------------------------------------------------------------- .../compressors/deflate64/HuffmanDecoder.java | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/commons-compress/blob/90d775ce/src/main/java/org/apache/commons/compress/compressors/deflate64/HuffmanDecoder.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/compress/compressors/deflate64/HuffmanDecoder.java b/src/main/java/org/apache/commons/compress/compressors/deflate64/HuffmanDecoder.java index f42e162..267fa68 100644 --- a/src/main/java/org/apache/commons/compress/compressors/deflate64/HuffmanDecoder.java +++ b/src/main/java/org/apache/commons/compress/compressors/deflate64/HuffmanDecoder.java @@ -128,7 +128,7 @@ class HuffmanDecoder implements Closeable { public int decode(byte[] b) throws IOException { return decode(b, 0, b.length); } - + public int decode(byte[] b, int off, int len) throws IOException { while (!finalBlock || state.hasData()) { switch (state.state()) { @@ -204,7 +204,7 @@ class HuffmanDecoder implements Closeable { int max = (int) Math.min(blockLength - read, len); for (int i = 0; i < max; i++) { if (reader.bitsCached() > 0) { - byte next = (byte) (readBits(Byte.SIZE) & 0xFF); + byte next = (byte) readBits(Byte.SIZE); b[off + i] = memory.add(next); read++; } else { @@ -261,6 +261,7 @@ class HuffmanDecoder implements Closeable { private int runBufferPos = 0; private byte[] runBuffer = new byte[0]; + private int runBufferLength = 0; HuffmanCodes(HuffmanState state, int[] lengths, int[] distance) { this.state = state; @@ -287,7 +288,7 @@ class HuffmanDecoder implements Closeable { while (result < len) { int symbol = nextSymbol(reader, lengthTree); if (symbol < 256) { - b[off + result++] = memory.add((byte) (symbol & 0xFF)); + b[off + result++] = memory.add((byte) symbol); } else if (symbol > 256) { int runMask = RUN_LENGTH_TABLE[symbol - 257]; int run = runMask >>> 5; @@ -301,7 +302,10 @@ class HuffmanDecoder implements Closeable { int distXtra = distMask & 0xF; dist += readBits(distXtra); - runBuffer = new byte[run]; + if (runBuffer.length < run) { + runBuffer = new byte[run]; + } + runBufferLength = run; runBufferPos = 0; memory.recordToBuffer(dist, run, runBuffer); @@ -316,7 +320,7 @@ class HuffmanDecoder implements Closeable { } private int copyFromRunBuffer(byte[] b, int off, int len) { - int bytesInBuffer = runBuffer.length - runBufferPos; + int bytesInBuffer = runBufferLength - runBufferPos; int copiedBytes = 0; if (bytesInBuffer > 0) { copiedBytes = Math.min(len, bytesInBuffer); @@ -333,7 +337,7 @@ class HuffmanDecoder implements Closeable { @Override int available() { - return runBuffer.length - runBufferPos; + return runBufferLength - runBufferPos; } }