lzma-sdk-sys
Raw FFI bindings for the LZMA encoder/decoder components of LZMA-SDK (7-zip).
This crate provides low-level access to LZMA compression functionality, with support for the new optimized assembly routines on supported platforms.
Motivation
The implementation of LZMA in LZMA-SDK (7z) generally tends to be slightly better than the one in xz-utils, at equivalent settings.
// Meshes from Interesting NPCs SE - Loose-29194-4-3-2-157834454
Tool Size Ratio Flags
----------------------------------------------------
lzma-sdk 42.24 MiB 27.71% 7z a -txz -mx=9
xz 42.73 MiB 28.03% xz -k -e -z -9
In all, compression speed, size and decompression speed.
However, for library usage, historically xz-utils has been much more common; be it a more familiar API, ease of integration, or any other possible reason.
In recent years however, Igor Pavlov started adding hand-crafted assembly routines for LZMA (de)compression in 7-Zip. These, notably increase decompression speed, by as much as 30%, on supported platforms.
However LZMA-SDK with these optimizations may be non-trivial to build. This Rust crate provides bindings for the LZMA Encoding/Decoding routines of LZMA-SDK, with the possibility of using the hand-optimized assembly routines.
Usage
Add this to your Cargo.toml:
[]
= "0.1.0"
Basic example of compression and decompression:
use *;
use ptr;
Features
The crate provides several configuration options through Cargo features:
Core Features
std(default): Currently a no-op.enable-asm(default): Use hand-optimized assembly routines for improved performance (enabled by default)generate-bindings: Generates the bindings as part of build. You shouldn't usually need to run this. (Requires clang)prefer-clang: Usesclangas the compiler for the 7z library if available on $PATH
Threading Options
- Default: Multi-threaded operation
st: Single-threaded mode
Additional Options
debug-build-logs: Enable detailed build configuration loggingdebug-build-script: Enable debugging of the build script (via CodeLLDB on Linux/macOS)
Future Features
These features of the crate are set up correctly for future LZMA-SDK bindings, but are not currently used by any of the code we create bindings for.
Core Features
external-codecs(default): Support for external codecs in 7z archive format (enabled by default)
Additional Options
large-pages: Enable large pages support for potential performance improvementslong-paths: Support for long file paths
Performance
When using optimized assembly routines (enable-asm feature), significant
performance improvements are achieved.
Here are benchmark results using a Ryzen 5900X, compressing and
decompressing the COPYING file from the 7z distribution:
| Operation | Size (bytes) | Time | Throughput |
|---|---|---|---|
| lzma/decompress/26530 | 26530 (0.03 MB) | 268.29 µs | 94.304 MiB/s |
| lzma/decompress/26530 (x64 ASM Optimization) | 26530 (0.03 MB) | 196.77 µs | 128.58 MiB/s |
Tested on a Ryzen 5900X
Platform Support
The optimized assembly routines have been tested on hardware:
- i686 Linux (Clang)
- x86_64 Linux (Clang)
- i686 Linux (GCC)
- x86_64 Linux (GCC)
- i686 Windows (MSVC)
- x86_64 Windows (MSVC)
- i686 Windows (GNU)
- x86_64 Windows (GNU)
And on emulator or hypervisor:
- aarch64 Linux (Clang)
- aarch64 Linux (GCC)
- aarch64 macOS
The following fail to compile with the optimized assembly routines and are disabled in build.rs:
- x86_64 macOS (hard to diagnose, as I don't have the hardware.)
And a few more platforms are also tested in CI. These however have not been hand checked; all I know is 'they compile, and pass a basic sanity check'.
Building
Build should hopefully 'just work' with whatever toolchain you have installed. Bindings are pre-generated, and the assembly routines are pre-compiled on the relevant platforms.
If you have clang installed and available in your $PATH, the crate will attempt to build the
7z library code with it if the prefer-clang feature is enabled.
No additional dependencies are required. Normally 7z requires you to bring your own assembler, but this crate provides precompiled assembly code, to make it easy to use just like any other Rust crate.
Current Status
This crate currently provides bindings sufficient for LZMA Encoding/Decoding functionality.
While most of the groundwork has been laid for supporting remaining 7z features, a small amount of extra work is needed in terms of writing sanity tests around functionality using optimized assembly routines.
So for now, this crate only outputs bindings for functionality I personally am using.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request. If you're adding new APIs, please add some basic sanity tests first to verify they work.
If you're updating the version of LZMA-SDK, please also update the precompiled assembly code.
Credits & License
This crate is maintained by Sewer56 for usage in sewer56-archives-nx. Licensed under the MIT License.
LZMA-SDK was written by Igor Pavlov. The LZMA encoder/decoder exposed via their bindings are public domain (see file headers).
For any other future code exposed, please see 7zip's own licensing terms. Chances are it's LGPL-V3.