xpatch
A high-performance delta compression library for Rust that automatically selects the optimal compression algorithm based on the type of change detected between data versions.
Features
- Automatic Algorithm Selection: Analyzes changes and chooses the best compression strategy
- Multiple Compression Algorithms:
- Simple character insertion (Chars)
- Token-based compression (Tokens)
- Byte removal (Remove)
- Repetitive pattern detection (RepeatChars, RepeatTokens)
- General-purpose delta compression (GDelta, GDeltaZstd)
- Excellent Compression Ratios: Achieves 99.5% average space savings on real-world code changes
- Fast Performance: 40-55 GB/s throughput for typical changes
- Optional zstd Compression: Additional compression layer for complex changes
- Metadata Support: Embed version tags with zero overhead for values 0-15
Performance
All encoding and decoding operations are single-threaded. The benchmark infrastructure uses multiple threads to process many deltas in parallel, but each individual encoding is sequential.
Typical Performance (simple insertions, deletions, small changes):
- Encoding: 1-5 microseconds (40-55 GB/s)
- Decoding: 1-5 microseconds (40-55 GB/s)
Slower Cases (complex changes requiring GDelta):
- Encoding: 50-200 microseconds (0.5-2 GB/s)
- Decoding: 5-50 microseconds (2-20 GB/s)
Edge Cases (worst-case scenarios, complete rewrites):
- Encoding: up to 200 microseconds for 100KB files (~500 MB/s)
- Decoding: 30-50 microseconds (~2-3 GB/s)
Most real-world changes fall into the "typical" category. See test_results for detailed benchmark data.
Installation
Add xpatch to your Cargo.toml:
[]
= "0.1.0"
License
This project is dual-licensed under:
Option 1: AGPL-3.0-or-later (Free for Open Source)
Free to use in open source projects that comply with the AGPL license. If you modify xpatch and distribute it (including as a web service), you must open-source your modifications under AGPL.
Option 2: Commercial License (For Proprietary Use)
For companies that want to use xpatch in closed-source products, a commercial license is available. Pricing is flexible.
To purchase a commercial license or request a quote: Email: xpatch-commercial@alias.oseifert.ch
Contributor License Agreement
All contributors must sign a CLA that grants us rights to relicense their contributions under both AGPL and commercial terms.
See LICENSE-AGPL.txt for the full AGPL license text. See LICENSE-COMMERCIAL.txt for commercial license terms.
Quick Start
use delta;
Running the Examples
Try the included examples to see xpatch in action:
# Basic compression example
# Tags example demonstrating metadata and version optimization
# Expected output will show compression ratios and delta sizes
Command-Line Tool
xpatch includes a convenient CLI tool for working with deltas:
# Install with CLI support
# Or build from source
Basic Usage
# Create a delta
# Apply a delta
# Show delta info
See src/bin/xpatch/README.md for detailed CLI documentation.
Benchmark Results
Tested on 337,378 real-world Git commit diffs across three repositories (Git, Neovim, Tokio). All measurements are single-threaded performance.
Hardware: AMD Ryzen 7 7800X3D (16 threads), 64GB DDR5 RAM, Fedora Linux
| Algorithm | Avg Compression Ratio | Avg Space Savings | Avg Encode Time | Avg Decode Time |
|---|---|---|---|---|
| xpatch | 0.0043 | 99.6% | 0.69 ms | 0.03 ms |
| xdelta3 | 0.0197 | 98.0% | 0.12 ms | 0.01 ms |
| qbsdiff | 0.0073 | 99.3% | 17.29 ms | 1.66 ms |
Win Rate (best compression in head-to-head comparison): xpatch wins 95.4% of cases.
See the test_results directory for detailed logs and benchmark data.
How It Works
xpatch analyzes the change pattern between two byte sequences and automatically selects the most efficient algorithm:
- Change Analysis: Detects whether the change is a simple insertion, removal, or complex modification
- Pattern Detection: Identifies repetitive patterns that can be compressed efficiently
- Algorithm Selection: Tests multiple specialized algorithms and chooses the smallest output
- Encoding: Creates a compact delta with algorithm metadata in the header
For complex changes, xpatch uses gdelta, a general-purpose delta compression algorithm, with optional zstd compression.
API Documentation
Encoding
Creates a delta that transforms base_data into new_data.
tag: User-defined metadata valuebase_data: The original datanew_data: The target dataenable_zstd: Enable zstd compression for complex changes (slower but better compression)
Returns: Compact delta as a byte vector
Decoding
Applies a delta to reconstruct the new data.
base_data: The original data the delta was created fromdelta: The encoded delta
Returns: Reconstructed data or error message
Metadata Extraction
Extracts the tag value from a delta without decoding it.
Returns: Tag value or error message
Understanding Tags
The tag parameter provides a way to embed metadata directly into your deltas. Tags enable an important optimization in version control systems: you can choose which previous version to use as the base for creating a delta, not just the immediate predecessor.
Efficient Storage
Tags from 0-15 use only a single byte in the delta header alongside the algorithm type, adding zero overhead. Larger tags use variable-length encoding.
Example: Comparing Against Older Versions
Consider this scenario where data reverts to a previous state:
use delta;
Output:
=== Naive Approach ===
v1 -> v2 delta size: 9 bytes
v2 -> v3 delta size: 3 bytes
Naive total: 12 bytes
=== Optimized Approach ===
v1 -> v3 delta size: 2 bytes
Tag=1 indicates base version
Tag extracted: 1
By checking older versions in your history, you can find the optimal base that produces the smallest delta. The tag stores which version was used as the base, allowing your decoder to retrieve the correct version during reconstruction. This is particularly effective when changes are reverted or when data has cyclical patterns.
Running Benchmarks
The repository includes comprehensive benchmark suites:
Synthetic Benchmarks
# Standard compression tests
# Library comparison
Real-World Git Benchmarks
# Install xdelta3 (required for git benchmark comparisons)
# Run benchmark on selected repositories
Results are saved to the benchmark_results directory with timestamped CSV files. Benchmark scripts are included in the
repository.
Related Projects
- gdelta - General-purpose delta compression algorithm used by xpatch
Contributing
Contributions are welcome. Please open an issue or pull request on GitHub.