1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
//! # gdelta
//!
//! A fast delta compression algorithm for similar data chunks.
//!
//! `GDelta` efficiently encodes the differences between similar data chunks using
//! GEAR rolling hash for pattern matching and variable-length integer encoding
//! for space efficiency.
//!
//! ## Quick Start
//!
//! ```
//! use gdelta::{encode, decode};
//!
//! let base = b"Hello, World!";
//! let new = b"Hello, Rust!";
//!
//! // Encode the delta
//! let delta = encode(new, base).unwrap();
//!
//! // Decode to recover the new data
//! let recovered = decode(&delta, base).unwrap();
//! assert_eq!(recovered, new);
//! ```
//!
//! ## Algorithm Details
//!
//! The algorithm works by:
//! 1. Finding common prefix and suffix between base and new data
//! 2. Building a hash table of the base data using GEAR rolling hash
//! 3. Scanning the new data to find matches in the base
//! 4. Encoding the result as copy and literal instructions
//!
//! ## Performance
//!
//! `GDelta` is optimized for:
//! - Speed: Faster than Xdelta, Zdelta, Ddelta, and Edelta
//! - Similar chunks: Best for data chunks 4KB - 64KB in size
//! - Inter-chunk redundancy: Removes redundancy between similar chunks
//!
//! For maximum compression, combine `GDelta` with a general-purpose compressor
//! like ZSTD or LZ4.
pub use ;
/// Encodes the delta between new data and base data.
///
/// This function computes a compact representation of the differences between
/// `new_data` and `base_data`. The resulting delta can be later used with
/// [`decode`] to reconstruct the new data.
///
/// # Arguments
///
/// * `new_data` - The target data to encode
/// * `base_data` - The reference data to encode against
///
/// # Returns
///
/// A `Vec<u8>` containing the encoded delta, or a [`GDeltaError`] if encoding fails.
///
/// # Errors
///
/// Currently, encoding does not fail under normal circumstances. The `Result` type
/// is used for API consistency with `decode` and to allow for future validation
/// or error conditions without breaking the API.
///
/// # Examples
///
/// ```
/// use gdelta::encode;
///
/// let base = b"The quick brown fox jumps over the lazy dog";
/// let new = b"The quick brown cat jumps over the lazy dog";
///
/// let delta = encode(new, base).unwrap();
/// println!("Delta size: {} bytes", delta.len());
/// ```
///
/// # Performance
///
/// The encoding time is roughly proportional to the size of the new data,
/// with additional overhead for building the hash table of the base data.
/// Typical throughput is several hundred MB/s on modern hardware.
/// Decodes delta data using the base data to reconstruct the original.
///
/// This function applies the delta (created by [`encode`]) to the base data
/// to reconstruct the new data.
///
/// # Arguments
///
/// * `delta` - The encoded delta data
/// * `base_data` - The same base data used during encoding
///
/// # Returns
///
/// A `Vec<u8>` containing the reconstructed data, or a [`GDeltaError`] if
/// decoding fails (e.g., corrupted delta data).
///
/// # Errors
///
/// Returns `GDeltaError::InvalidDelta` if:
/// - The delta data is corrupted or malformed
/// - The instruction length exceeds the delta size
/// - A copy instruction references data beyond the base data bounds
///
/// # Examples
///
/// ```
/// use gdelta::{encode, decode};
///
/// let base = b"Hello, World!";
/// let new = b"Hello, Rust!";
///
/// let delta = encode(new, base).unwrap();
/// let recovered = decode(&delta, base).unwrap();
///
/// assert_eq!(recovered, new);
/// ```
///
/// # Performance
///
/// Decoding is typically faster than encoding, as it only needs to follow
/// the instructions in the delta without performing hash table lookups.