-
Notifications
You must be signed in to change notification settings - Fork 242
Integrate jlibdeflate for faster DEFLATE compression and decompression #1768
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
116 changes: 116 additions & 0 deletions
116
src/main/java/htsjdk/samtools/util/zip/LibdeflateDeflater.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| /* | ||
| * The MIT License | ||
| * | ||
| * Copyright (c) 2026 Tim Fennell | ||
| * | ||
| * Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| * of this software and associated documentation files (the "Software"), to deal | ||
| * in the Software without restriction, including without limitation the rights | ||
| * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| * copies of the Software, and to permit persons to whom the Software is | ||
| * furnished to do so, subject to the following conditions: | ||
| * | ||
| * The above copyright notice and this permission notice shall be included in | ||
| * all copies or substantial portions of the Software. | ||
| * | ||
| * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
| * THE SOFTWARE. | ||
| */ | ||
| package htsjdk.samtools.util.zip; | ||
|
|
||
| import com.fulcrumgenomics.jlibdeflate.LibdeflateCompressor; | ||
|
|
||
| import java.util.zip.Deflater; | ||
|
|
||
| /** | ||
| * A {@link Deflater} implementation backed by libdeflate via the jlibdeflate library. | ||
| * Provides significantly faster DEFLATE compression than the JDK's built-in zlib. | ||
| * | ||
| * <p>This class supports the subset of the Deflater API used by BGZF block compression: | ||
| * {@link #reset()}, {@link #setInput(byte[], int, int)}, {@link #finish()}, | ||
| * {@link #deflate(byte[], int, int)}, and {@link #finished()}.</p> | ||
| */ | ||
| class LibdeflateDeflater extends Deflater { | ||
|
|
||
| private final LibdeflateCompressor compressor; | ||
| private final boolean nowrap; | ||
|
|
||
| private byte[] inputBuf; | ||
| private int inputOff; | ||
| private int inputLen; | ||
| private boolean finishing; | ||
| private boolean done; | ||
|
|
||
| /** | ||
| * Creates a new LibdeflateDeflater at the specified compression level. | ||
| * | ||
| * @param level compression level (0-12 for libdeflate, 0-9 for standard compatibility) | ||
| * @param nowrap if true, produce raw DEFLATE (no zlib/gzip header); if false, produce zlib format | ||
| */ | ||
| LibdeflateDeflater(final int level, final boolean nowrap) { | ||
| // The super constructor allocates a native zlib stream we won't use. | ||
| // We immediately free it since all compression goes through libdeflate. | ||
| super(level, nowrap); | ||
| super.end(); | ||
|
|
||
| this.nowrap = nowrap; | ||
| this.compressor = new LibdeflateCompressor(level); | ||
| } | ||
|
|
||
| @Override | ||
| public void setInput(final byte[] input, final int off, final int len) { | ||
| this.inputBuf = input; | ||
| this.inputOff = off; | ||
| this.inputLen = len; | ||
| this.done = false; | ||
| } | ||
|
|
||
| @Override | ||
| public void finish() { | ||
| this.finishing = true; | ||
|
tfenne marked this conversation as resolved.
|
||
| } | ||
|
|
||
| @Override | ||
| public int deflate(final byte[] output, final int off, final int len) { | ||
| if (inputBuf == null || inputLen == 0) { | ||
| done = true; | ||
| return 0; | ||
| } | ||
|
|
||
| final int compressed = nowrap | ||
| ? compressor.deflateCompress(inputBuf, inputOff, inputLen, output, off, len) | ||
| : compressor.zlibCompress(inputBuf, inputOff, inputLen, output, off, len); | ||
| if (compressed == -1) { | ||
| // Output buffer too small — caller will handle this (e.g. fall back to no-compression) | ||
| done = false; | ||
| return 0; | ||
| } | ||
|
|
||
| done = true; | ||
| return compressed; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean finished() { | ||
| return finishing && done; | ||
| } | ||
|
|
||
| @Override | ||
| public void reset() { | ||
| inputBuf = null; | ||
| inputOff = 0; | ||
| inputLen = 0; | ||
| finishing = false; | ||
| done = false; | ||
| } | ||
|
|
||
| @Override | ||
| public void end() { | ||
| compressor.close(); | ||
| } | ||
| } | ||
98 changes: 98 additions & 0 deletions
98
src/main/java/htsjdk/samtools/util/zip/LibdeflateInflater.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| /* | ||
| * The MIT License | ||
| * | ||
| * Copyright (c) 2026 Tim Fennell | ||
| * | ||
| * Permission is hereby granted, free of charge, to any person obtaining a copy | ||
| * of this software and associated documentation files (the "Software"), to deal | ||
| * in the Software without restriction, including without limitation the rights | ||
| * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
| * copies of the Software, and to permit persons to whom the Software is | ||
| * furnished to do so, subject to the following conditions: | ||
| * | ||
| * The above copyright notice and this permission notice shall be included in | ||
| * all copies or substantial portions of the Software. | ||
| * | ||
| * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
| * THE SOFTWARE. | ||
| */ | ||
| package htsjdk.samtools.util.zip; | ||
|
|
||
| import com.fulcrumgenomics.jlibdeflate.LibdeflateDecompressor; | ||
|
|
||
| import java.util.zip.DataFormatException; | ||
| import java.util.zip.Inflater; | ||
|
|
||
| /** | ||
| * An {@link Inflater} implementation backed by libdeflate via the jlibdeflate library. | ||
| * Provides significantly faster DEFLATE decompression than the JDK's built-in zlib. | ||
| * | ||
| * <p>This class supports the subset of the Inflater API used by BGZF block decompression: | ||
| * {@link #reset()}, {@link #setInput(byte[], int, int)}, and | ||
| * {@link #inflate(byte[], int, int)}.</p> | ||
| * | ||
| * <p>The libdeflate decompressor requires the exact uncompressed size to be known. In BGZF, | ||
| * this is always the case since the uncompressed size is stored in the block footer and | ||
| * passed as the {@code len} parameter to {@link #inflate(byte[], int, int)}.</p> | ||
| */ | ||
| class LibdeflateInflater extends Inflater { | ||
|
|
||
| private final LibdeflateDecompressor decompressor; | ||
| private final boolean nowrap; | ||
|
|
||
| private byte[] inputBuf; | ||
| private int inputOff; | ||
| private int inputLen; | ||
|
|
||
| LibdeflateInflater(final boolean nowrap) { | ||
| // The super constructor allocates a native zlib stream we won't use. | ||
| // We immediately free it since all decompression goes through libdeflate. | ||
| super(nowrap); | ||
| super.end(); | ||
|
|
||
| this.nowrap = nowrap; | ||
| this.decompressor = new LibdeflateDecompressor(); | ||
| } | ||
|
|
||
| @Override | ||
| public void setInput(final byte[] input, final int off, final int len) { | ||
| this.inputBuf = input; | ||
| this.inputOff = off; | ||
| this.inputLen = len; | ||
| } | ||
|
|
||
| @Override | ||
| public int inflate(final byte[] output, final int off, final int len) throws DataFormatException { | ||
| if (inputBuf == null || inputLen == 0 || len == 0) { | ||
| return 0; | ||
| } | ||
|
|
||
| try { | ||
| if (nowrap) { | ||
| decompressor.deflateDecompress(inputBuf, inputOff, inputLen, output, off, len); | ||
| } else { | ||
| decompressor.zlibDecompress(inputBuf, inputOff, inputLen, output, off, len); | ||
| } | ||
| return len; | ||
| } catch (final Exception e) { | ||
| throw new DataFormatException(e.getMessage()); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void reset() { | ||
| inputBuf = null; | ||
| inputOff = 0; | ||
| inputLen = 0; | ||
| } | ||
|
|
||
| @Override | ||
| public void end() { | ||
| decompressor.close(); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nitpick: it seems odd that
InflaterFactorycalls a method onDeflatorFactory, but putting it into it's own separate class seems odd too. 🤷There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed. Feels like it should just be
CompressionFactory, but consolidating the two would be a bigger breaking change now.