Skip to content

Decode linked blocks in LZ4FrameInputStream - #198

Open
Cyan4973 wants to merge 1 commit into
yawkat:mainfrom
Cyan4973:linked-blocks
Open

Cyan4973 wants to merge 1 commit into
yawkat:mainfrom
Cyan4973:linked-blocks

Conversation

@Cyan4973

Copy link
Copy Markdown

LZ4FrameInputStream now decodes frames with linked blocks, the default of liblz4's frame API. Decompression only: LZ4FrameOutputStream still writes independent blocks.

Existing decoders are unchanged. Details, tests and measurements are in the commit message.

Closes #197

LZ4FrameInputStream rejected frames with linked blocks (FLG.B.Indep = 0),
although they are the default of liblz4's frame API (LZ4F_blockLinked = 0).
In such frames, a block may reference up to 64 KB of the data decoded
before it in the same frame. LZ4FrameOutputStream still writes independent
blocks only.

The templates generate a separate decompressWithPrefix copy of the safe
decoder, for byte arrays, whose matches may also reach the prefixLen bytes
before destOff in dest. The existing decompress methods keep the same
bytecode: a variant sharing their code made the safe decoder 3 to 5%
slower on independent blocks with JDK 21 and 25, a code generation effect.
JNI gets a new native, LZ4_decompress_safe_usingDict, for which liblz4
detects the prefix in place. The existing natives are unchanged.

decompressWithPrefix is package-private and not abstract on
LZ4SafeDecompressor. Implementations outside this package keep working, and
reject a non-empty prefix.

LZ4FrameInputStream decodes a linked block right after the data decoded
before it in the frame, and moves the last 64 KB of that data to the start
of the buffer only when the block doesn't fit after it. The buffer grows to
at most 64 KB + the maximum block size, still only as data arrives. Blocks
without history (independent blocks, first block of a linked frame) still
go through decompress(), which also keeps them working with an older native
library from java.library.path. FLG.fromByte accepts linked blocks; the FLG
constructor, used by LZ4FrameOutputStream, still rejects them.

LZ4FrameLinkedBlocksTest writes linked frames by hand along with their
content: matches up to offset 65535 across several blocks, matches before
the frame start, no history across frames, random concatenated frames. It
also decodes frames written by the lz4 CLI with -BD (each block size, -BX,
--content-size, -9), checked to depend on the history, with the three
decompressors. New fuzz targets decompress with a prefix twice, with
different data before the prefix and after the output, and compare;
LZ4TestAccess gives them access to the package-private method. The frame
fuzz targets get a seed with linked frames.

Decoding speed on silesia, lz4 CLI frames, JDK 25: unchanged for
independent blocks. Linked vs independent blocks of the same size: JNI
-0.4% with 256 KB and 4 MB blocks, +8% with 64 KB blocks (better ratio);
safe decoder -3% with 256 KB and 4 MB blocks, +8% with 64 KB blocks.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support linked blocks in LZ4FrameInputStream

1 participant