|
9 | 9 | import java.util.ArrayDeque;
|
10 | 10 | import java.util.Deque;
|
11 | 11 | import java.util.zip.DataFormatException;
|
| 12 | +import java.util.zip.Inflater; |
12 | 13 | import java.util.zip.ZipException;
|
13 | 14 |
|
14 | 15 | /**
|
15 | 16 | * Optimized implementation of {@link DeflateDecompressor} with unsafe resetting for more throughput.
|
16 | 17 | *
|
17 | 18 | * @author xDark
|
18 | 19 | */
|
| 20 | +@SuppressWarnings("UnnecessaryLocalVariable") |
19 | 21 | public class UnsafeDeflateDecompressor implements Decompressor {
|
20 |
| - private static final Deque<UnsafeInflater> INFLATERS = new ArrayDeque<>(); |
| 22 | + private static final int DEFLATE_CACHE_LIMIT = 64; |
| 23 | + private static final Deque<DeflateEntry> DEFLATE_ENTRIES = new ArrayDeque<>(); |
| 24 | + private static final byte[] emptyBuf = new byte[0]; |
| 25 | + |
| 26 | + static { |
| 27 | + Deque<DeflateEntry> entries = DEFLATE_ENTRIES; |
| 28 | + for (int i = 0; i < DEFLATE_CACHE_LIMIT; i++) { |
| 29 | + entries.push(new DeflateEntry()); |
| 30 | + } |
| 31 | + } |
21 | 32 |
|
22 | 33 | @Override
|
23 | 34 | public ByteData decompress(LocalFileHeader header, ByteData data) throws IOException {
|
24 | 35 | if (header.getCompressionMethod() != ZipCompressions.DEFLATED)
|
25 | 36 | throw new IOException("LocalFileHeader contents not using 'Deflated'!");
|
26 | 37 | FastWrapOutputStream out = new FastWrapOutputStream();
|
27 |
| - UnsafeInflater inflater; |
28 |
| - Deque<UnsafeInflater> inflaters = INFLATERS; |
| 38 | + DeflateEntry entry; |
| 39 | + Deque<DeflateEntry> inflaters = DEFLATE_ENTRIES; |
29 | 40 | synchronized (inflaters) {
|
30 |
| - inflater = inflaters.poll(); |
31 |
| - if (inflater == null) { |
32 |
| - inflater = new UnsafeInflater(true); |
33 |
| - } |
| 41 | + entry = inflaters.poll(); |
| 42 | + } |
| 43 | + if (entry == null) { |
| 44 | + entry = new DeflateEntry(); |
| 45 | + } else { |
| 46 | + // Normal 'Inflater' reset() is synchronized and bottlenecks us, |
| 47 | + // but we're using 'UnsafeInflater' which bypasses that. |
| 48 | + entry.inflater.reset(); |
34 | 49 | }
|
35 | 50 | try {
|
36 |
| - byte[] output = new byte[1024]; |
37 |
| - byte[] buffer = new byte[1024]; |
| 51 | + byte[] output = entry.decompress; |
| 52 | + byte[] buffer = entry.buffer; |
| 53 | + Inflater inflater = entry.inflater; |
38 | 54 | long position = 0L;
|
39 | 55 | long length = data.length();
|
| 56 | + inflater.setInput(emptyBuf); |
40 | 57 | do {
|
41 | 58 | if (inflater.needsInput()) {
|
42 | 59 | int remaining = (int) Math.min(buffer.length, length);
|
43 |
| - if (remaining == 0) { |
44 |
| - break; |
| 60 | + if (remaining != 0) { |
| 61 | + data.get(position, buffer, 0, remaining); |
| 62 | + length -= remaining; |
| 63 | + position += remaining; |
| 64 | + inflater.setInput(buffer, 0, remaining); |
45 | 65 | }
|
46 |
| - data.get(position, buffer, 0, remaining); |
47 |
| - length -= remaining; |
48 |
| - position += remaining; |
49 |
| - inflater.setInput(buffer, 0, remaining); |
50 | 66 | }
|
51 | 67 | int count = inflater.inflate(output);
|
52 | 68 | if (count != 0) {
|
53 | 69 | out.write(output, 0, count);
|
54 | 70 | }
|
55 |
| - } while (!inflater.finished() && !inflater.needsDictionary()); |
| 71 | + } while (!inflater.finished()); |
56 | 72 | } catch (DataFormatException e) {
|
57 |
| - String s = e.getMessage(); |
58 |
| - throw (ZipException) new ZipException(s != null ? null : "Invalid ZLIB data format").initCause(e); |
| 73 | + String msg = e.getMessage(); |
| 74 | + throw (ZipException) new ZipException(msg != null ? null : "Invalid ZLIB data format").initCause(e); |
59 | 75 | } finally {
|
60 | 76 | end:
|
61 | 77 | {
|
62 |
| - if (inflaters.size() < 32) { |
| 78 | + if (inflaters.size() < DEFLATE_CACHE_LIMIT) { |
63 | 79 | synchronized (inflaters) {
|
64 |
| - if (inflaters.size() < 32) { |
65 |
| - inflater.fastReset(); |
66 |
| - inflaters.push(inflater); |
| 80 | + if (inflaters.size() < DEFLATE_CACHE_LIMIT) { |
| 81 | + inflaters.addFirst(entry); |
67 | 82 | break end;
|
68 | 83 | }
|
69 | 84 | }
|
70 | 85 | }
|
71 |
| - inflater.end(); |
| 86 | + entry.inflater.end(); |
72 | 87 | }
|
73 | 88 | }
|
74 | 89 | return out.wrap();
|
75 | 90 | }
|
| 91 | + |
| 92 | + private static final class DeflateEntry { |
| 93 | + final Inflater inflater = new UnsafeInflater(true); |
| 94 | + final byte[] decompress = new byte[1024]; |
| 95 | + final byte[] buffer = new byte[8192]; |
| 96 | + } |
76 | 97 | }
|
0 commit comments