7 * The author dedicates this file to the public domain.
8 * You can do whatever you want with this file.
10 * ----------------------------------------------------------------------------
12 * This is a Binary Trees (bt) based matchfinder.
14 * The data structure is a hash table where each hash bucket contains a binary
15 * tree of sequences whose first 3 bytes share the same hash code. Each
16 * sequence is identified by its starting position in the input buffer. Each
17 * binary tree is always sorted such that each left child represents a sequence
18 * lexicographically lesser than its parent and each right child represents a
19 * sequence lexicographically greater than its parent.
21 * The algorithm processes the input buffer sequentially. At each byte
22 * position, the hash code of the first 3 bytes of the sequence beginning at
23 * that position (the sequence being matched against) is computed. This
24 * identifies the hash bucket to use for that position. Then, a new binary tree
25 * node is created to represent the current sequence. Then, in a single tree
26 * traversal, the hash bucket's binary tree is searched for matches and is
27 * re-rooted at the new node.
29 * Compared to the simpler algorithm that uses linked lists instead of binary
30 * trees (see hc_matchfinder.h), the binary tree version gains more information
31 * at each node visitation. Ideally, the binary tree version will examine only
32 * 'log(n)' nodes to find the same matches that the linked list version will
33 * find by examining 'n' nodes. In addition, the binary tree version can
34 * examine fewer bytes at each node by taking advantage of the common prefixes
35 * that result from the sort order, whereas the linked list version may have to
36 * examine up to the full length of the match at each node.
38 * However, it is not always best to use the binary tree version. It requires
39 * nearly twice as much memory as the linked list version, and it takes time to
40 * keep the binary trees sorted, even at positions where the compressor does not
41 * need matches. Generally, when doing fast compression on small buffers,
42 * binary trees are the wrong approach. They are best suited for thorough
43 * compression and/or large buffers.
45 * ----------------------------------------------------------------------------
51 #include "wimlib/lz_extend.h"
52 #include "wimlib/lz_hash.h"
54 #define BT_MATCHFINDER_HASH_ORDER 16
56 /* TEMPLATED functions and structures have MF_SUFFIX appended to their name. */
58 #define TEMPLATED(name) CONCAT(name, MF_SUFFIX)
60 #ifndef _WIMLIB_BT_MATCHFINDER_H
61 #define _WIMLIB_BT_MATCHFINDER_H
63 /* Non-templated definitions */
65 /* Representation of a match found by the bt_matchfinder */
68 /* The number of bytes matched. */
71 /* The offset back from the current position that was matched. */
76 bt_matchfinder_hash_3_bytes(const u8 *in_next)
78 return lz_hash_3_bytes(in_next, BT_MATCHFINDER_HASH_ORDER);
81 #endif /* _WIMLIB_BT_MATCHFINDER_H */
83 struct TEMPLATED(bt_matchfinder) {
84 pos_t hash_tab[1UL << BT_MATCHFINDER_HASH_ORDER];
88 /* Return the number of bytes that must be allocated for a 'bt_matchfinder' that
89 * can work with buffers up to the specified size. */
91 TEMPLATED(bt_matchfinder_size)(size_t max_bufsize)
93 return sizeof(struct TEMPLATED(bt_matchfinder)) +
94 (2 * max_bufsize * sizeof(pos_t));
97 /* Prepare the matchfinder for a new input buffer. */
99 TEMPLATED(bt_matchfinder_init)(struct TEMPLATED(bt_matchfinder) *mf)
101 memset(mf, 0, sizeof(*mf));
104 static inline pos_t *
105 TEMPLATED(bt_child)(struct TEMPLATED(bt_matchfinder) *mf, pos_t node, int offset)
107 return &mf->child_tab[(node << 1) + offset];
110 static inline pos_t *
111 TEMPLATED(bt_left_child)(struct TEMPLATED(bt_matchfinder) *mf, pos_t node)
113 return TEMPLATED(bt_child)(mf, node, 0);
116 static inline pos_t *
117 TEMPLATED(bt_right_child)(struct TEMPLATED(bt_matchfinder) *mf, pos_t node)
119 return TEMPLATED(bt_child)(mf, node, 1);
123 * Retrieve a list of matches with the current position.
126 * The matchfinder structure.
128 * Pointer to the beginning of the input buffer.
130 * Pointer to the next byte in the input buffer to process. This is the
131 * pointer to the sequence being matched against.
133 * Only record matches that are at least this long.
135 * The maximum permissible match length at this position.
137 * Stop searching if a match of at least this length is found.
138 * Must be <= @max_len.
140 * Limit on the number of potential matches to consider. Must be >= 1.
142 * Pointer to the hash code for the current sequence, which was computed
143 * one position in advance so that the binary tree root could be
144 * prefetched. This is an input/output parameter.
146 * The length of the longest match found is written here. (This is
147 * actually redundant with the 'struct lz_match' array, but this is easier
148 * for the compiler to optimize when inlined and the caller immediately
149 * does a check against 'best_len'.)
151 * An array in which this function will record the matches. The recorded
152 * matches will be sorted by strictly increasing length and strictly
153 * increasing offset. The maximum number of matches that may be found is
154 * 'min(nice_len, max_len) - 3 + 1'.
156 * The return value is a pointer to the next available slot in the @lz_matchptr
157 * array. (If no matches were found, this will be the same as @lz_matchptr.)
159 static inline struct lz_match *
160 TEMPLATED(bt_matchfinder_get_matches)(struct TEMPLATED(bt_matchfinder) * const restrict mf,
161 const u8 * const in_begin,
162 const u8 * const in_next,
163 const unsigned min_len,
164 const unsigned max_len,
165 const unsigned nice_len,
166 const unsigned max_search_depth,
167 u32 * restrict next_hash,
168 unsigned * restrict best_len_ret,
169 struct lz_match * restrict lz_matchptr)
171 unsigned depth_remaining = max_search_depth;
175 pos_t *pending_lt_ptr, *pending_gt_ptr;
176 unsigned best_lt_len, best_gt_len;
178 unsigned best_len = min_len - 1;
180 if (unlikely(max_len < LZ_HASH3_REQUIRED_NBYTES + 1)) {
181 *best_len_ret = best_len;
186 *next_hash = bt_matchfinder_hash_3_bytes(in_next + 1);
187 cur_node = mf->hash_tab[hash];
188 mf->hash_tab[hash] = in_next - in_begin;
189 prefetchw(&mf->hash_tab[*next_hash]);
191 pending_lt_ptr = TEMPLATED(bt_left_child)(mf, in_next - in_begin);
192 pending_gt_ptr = TEMPLATED(bt_right_child)(mf, in_next - in_begin);
200 *best_len_ret = best_len;
205 matchptr = &in_begin[cur_node];
207 if (matchptr[len] == in_next[len]) {
208 len = lz_extend(in_next, matchptr, len + 1, max_len);
209 if (len > best_len) {
211 lz_matchptr->length = len;
212 lz_matchptr->offset = in_next - matchptr;
214 if (len >= nice_len) {
215 *pending_lt_ptr = *TEMPLATED(bt_left_child)(mf, cur_node);
216 *pending_gt_ptr = *TEMPLATED(bt_right_child)(mf, cur_node);
217 *best_len_ret = best_len;
223 if (matchptr[len] < in_next[len]) {
224 *pending_lt_ptr = cur_node;
225 pending_lt_ptr = TEMPLATED(bt_right_child)(mf, cur_node);
226 cur_node = *pending_lt_ptr;
228 if (best_gt_len < len)
231 *pending_gt_ptr = cur_node;
232 pending_gt_ptr = TEMPLATED(bt_left_child)(mf, cur_node);
233 cur_node = *pending_gt_ptr;
235 if (best_lt_len < len)
239 if (!cur_node || !--depth_remaining) {
242 *best_len_ret = best_len;
249 * Advance the matchfinder, but don't record any matches.
252 * The matchfinder structure.
254 * Pointer to the beginning of the input buffer.
256 * Pointer to the next byte in the input buffer to process.
258 * Pointer to the end of the input buffer.
260 * Stop searching if a match of at least this length is found.
262 * Limit on the number of potential matches to consider.
264 * Pointer to the hash code for the current sequence, which was computed
265 * one position in advance so that the binary tree root could be
266 * prefetched. This is an input/output parameter.
268 * Note: this is very similar to bt_matchfinder_get_matches() because both
269 * functions must do hashing and tree re-rooting. This version just doesn't
270 * actually record any matches.
273 TEMPLATED(bt_matchfinder_skip_position)(struct TEMPLATED(bt_matchfinder) * const restrict mf,
274 const u8 * const in_begin,
275 const u8 * const in_next,
276 const u8 * const in_end,
277 const unsigned nice_len,
278 const unsigned max_search_depth,
279 u32 * restrict next_hash)
281 unsigned depth_remaining = max_search_depth;
285 pos_t *pending_lt_ptr, *pending_gt_ptr;
286 unsigned best_lt_len, best_gt_len;
289 if (unlikely(in_end - in_next < LZ_HASH3_REQUIRED_NBYTES + 1))
293 *next_hash = bt_matchfinder_hash_3_bytes(in_next + 1);
294 cur_node = mf->hash_tab[hash];
295 mf->hash_tab[hash] = in_next - in_begin;
296 prefetchw(&mf->hash_tab[*next_hash]);
298 depth_remaining = max_search_depth;
299 pending_lt_ptr = TEMPLATED(bt_left_child)(mf, in_next - in_begin);
300 pending_gt_ptr = TEMPLATED(bt_right_child)(mf, in_next - in_begin);
312 matchptr = &in_begin[cur_node];
314 if (matchptr[len] == in_next[len]) {
315 len = lz_extend(in_next, matchptr, len + 1, nice_len);
316 if (len == nice_len) {
317 *pending_lt_ptr = *TEMPLATED(bt_left_child)(mf, cur_node);
318 *pending_gt_ptr = *TEMPLATED(bt_right_child)(mf, cur_node);
323 if (matchptr[len] < in_next[len]) {
324 *pending_lt_ptr = cur_node;
325 pending_lt_ptr = TEMPLATED(bt_right_child)(mf, cur_node);
326 cur_node = *pending_lt_ptr;
328 if (best_gt_len < len)
331 *pending_gt_ptr = cur_node;
332 pending_gt_ptr = TEMPLATED(bt_left_child)(mf, cur_node);
333 cur_node = *pending_gt_ptr;
335 if (best_lt_len < len)
339 if (!cur_node || !--depth_remaining) {