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 * ----------------------------------------------------------------------------
48 #ifndef _BT_MATCHFINDER_H
49 #define _BT_MATCHFINDER_H
51 #include "wimlib/lz_extend.h"
52 #include "wimlib/lz_hash.h"
53 #include "wimlib/matchfinder_common.h"
55 #if MATCHFINDER_MAX_WINDOW_ORDER < 13
56 # define BT_MATCHFINDER_HASH_ORDER 14
57 #elif MATCHFINDER_MAX_WINDOW_ORDER < 15
58 # define BT_MATCHFINDER_HASH_ORDER 15
60 # define BT_MATCHFINDER_HASH_ORDER 16
63 #define BT_MATCHFINDER_HASH_LENGTH (1UL << BT_MATCHFINDER_HASH_ORDER)
65 struct bt_matchfinder {
66 pos_t hash_tab[BT_MATCHFINDER_HASH_LENGTH];
68 } _aligned_attribute(MATCHFINDER_ALIGNMENT);
70 /* Return the number of bytes that must be allocated for a 'bt_matchfinder' that
71 * can work with buffers up to the specified size. */
73 bt_matchfinder_size(size_t max_bufsize)
75 return sizeof(pos_t) * (BT_MATCHFINDER_HASH_LENGTH + (2 * max_bufsize));
78 /* Prepare the matchfinder for a new input buffer. */
80 bt_matchfinder_init(struct bt_matchfinder *mf)
82 matchfinder_init(mf->hash_tab, BT_MATCHFINDER_HASH_LENGTH);
86 bt_matchfinder_hash_3_bytes(const u8 *in_next)
88 return lz_hash_3_bytes(in_next, BT_MATCHFINDER_HASH_ORDER);
92 bt_child(struct bt_matchfinder *mf, pos_t node, int offset)
94 if (MATCHFINDER_MAX_WINDOW_ORDER < sizeof(pos_t) * 8) {
96 return &mf->child_tab[(node << 1) + offset];
98 return &mf->child_tab[((size_t)node << 1) + offset];
102 static inline pos_t *
103 bt_left_child(struct bt_matchfinder *mf, pos_t node)
105 return bt_child(mf, node, 0);
108 static inline pos_t *
109 bt_right_child(struct bt_matchfinder *mf, pos_t node)
111 return bt_child(mf, node, 1);
115 * Retrieve a list of matches with the current position.
118 * The matchfinder structure.
120 * Pointer to the beginning of the input buffer.
122 * Pointer to the next byte in the input buffer to process. This is the
123 * pointer to the sequence being matched against.
125 * Only record matches that are at least this long.
127 * The maximum permissible match length at this position.
129 * Stop searching if a match of at least this length is found.
131 * Limit on the number of potential matches to consider.
133 * Pointer to the hash code for the current sequence, which was computed
134 * one position in advance so that the binary tree root could be
135 * prefetched. This is an input/output parameter.
137 * The length of the longest match found is written here. (This is
138 * actually redundant with the 'struct lz_match' array, but this is easier
139 * for the compiler to optimize when inlined and the caller immediately
140 * does a check against 'best_len'.)
142 * An array in which this function will record the matches. The recorded
143 * matches will be sorted by strictly increasing length and strictly
144 * increasing offset. The maximum number of matches that may be found is
145 * 'min(nice_len, max_len) - 3 + 1'.
147 * The return value is a pointer to the next available slot in the @lz_matchptr
148 * array. (If no matches were found, this will be the same as @lz_matchptr.)
150 static inline struct lz_match *
151 bt_matchfinder_get_matches(struct bt_matchfinder * const restrict mf,
152 const u8 * const in_begin,
153 const u8 * const in_next,
154 const unsigned min_len,
155 const unsigned max_len,
156 const unsigned nice_len,
157 const unsigned max_search_depth,
158 u32 * restrict next_hash,
159 unsigned * restrict best_len_ret,
160 struct lz_match * restrict lz_matchptr)
162 unsigned depth_remaining = max_search_depth;
166 pos_t *pending_lt_ptr, *pending_gt_ptr;
167 unsigned best_lt_len, best_gt_len;
169 unsigned best_len = min_len - 1;
171 if (unlikely(max_len < LZ_HASH_REQUIRED_NBYTES + 1)) {
172 *best_len_ret = best_len;
177 *next_hash = bt_matchfinder_hash_3_bytes(in_next + 1);
178 cur_node = mf->hash_tab[hash];
179 mf->hash_tab[hash] = in_next - in_begin;
180 prefetch(&mf->hash_tab[*next_hash]);
182 pending_lt_ptr = bt_left_child(mf, in_next - in_begin);
183 pending_gt_ptr = bt_right_child(mf, in_next - in_begin);
188 if (!matchfinder_node_valid(cur_node)) {
189 *pending_lt_ptr = MATCHFINDER_NULL;
190 *pending_gt_ptr = MATCHFINDER_NULL;
191 *best_len_ret = best_len;
196 matchptr = &in_begin[cur_node];
198 if (matchptr[len] == in_next[len]) {
199 len = lz_extend(in_next, matchptr, len + 1, max_len);
200 if (len > best_len) {
202 lz_matchptr->length = len;
203 lz_matchptr->offset = in_next - matchptr;
205 if (len >= nice_len) {
206 *pending_lt_ptr = *bt_left_child(mf, cur_node);
207 *pending_gt_ptr = *bt_right_child(mf, cur_node);
208 *best_len_ret = best_len;
214 if (matchptr[len] < in_next[len]) {
215 *pending_lt_ptr = cur_node;
216 pending_lt_ptr = bt_right_child(mf, cur_node);
217 cur_node = *pending_lt_ptr;
219 if (best_gt_len < len)
222 *pending_gt_ptr = cur_node;
223 pending_gt_ptr = bt_left_child(mf, cur_node);
224 cur_node = *pending_gt_ptr;
226 if (best_lt_len < len)
230 if (!matchfinder_node_valid(cur_node) || !--depth_remaining) {
231 *pending_lt_ptr = MATCHFINDER_NULL;
232 *pending_gt_ptr = MATCHFINDER_NULL;
233 *best_len_ret = best_len;
240 * Advance the matchfinder, but don't record any matches.
243 * The matchfinder structure.
245 * Pointer to the beginning of the input buffer.
247 * Pointer to the next byte in the input buffer to process.
249 * Pointer to the end of the input buffer.
251 * Stop searching if a match of at least this length is found.
253 * Limit on the number of potential matches to consider.
255 * Pointer to the hash code for the current sequence, which was computed
256 * one position in advance so that the binary tree root could be
257 * prefetched. This is an input/output parameter.
259 * Note: this is very similar to bt_matchfinder_get_matches() because both
260 * functions must do hashing and tree re-rooting. This version just doesn't
261 * actually record any matches.
264 bt_matchfinder_skip_position(struct bt_matchfinder * const restrict mf,
265 const u8 * const in_begin,
266 const u8 * const in_next,
267 const u8 * const in_end,
268 const unsigned nice_len,
269 const unsigned max_search_depth,
270 u32 * restrict next_hash)
272 unsigned depth_remaining = max_search_depth;
276 pos_t *pending_lt_ptr, *pending_gt_ptr;
277 unsigned best_lt_len, best_gt_len;
280 if (unlikely(in_end - in_next < LZ_HASH_REQUIRED_NBYTES + 1))
284 *next_hash = bt_matchfinder_hash_3_bytes(in_next + 1);
285 cur_node = mf->hash_tab[hash];
286 mf->hash_tab[hash] = in_next - in_begin;
287 prefetch(&mf->hash_tab[*next_hash]);
289 depth_remaining = max_search_depth;
290 pending_lt_ptr = bt_left_child(mf, in_next - in_begin);
291 pending_gt_ptr = bt_right_child(mf, in_next - in_begin);
296 if (!matchfinder_node_valid(cur_node)) {
297 *pending_lt_ptr = MATCHFINDER_NULL;
298 *pending_gt_ptr = MATCHFINDER_NULL;
303 matchptr = &in_begin[cur_node];
305 if (matchptr[len] == in_next[len]) {
306 len = lz_extend(in_next, matchptr, len + 1, nice_len);
307 if (len == nice_len) {
308 *pending_lt_ptr = *bt_left_child(mf, cur_node);
309 *pending_gt_ptr = *bt_right_child(mf, cur_node);
314 if (matchptr[len] < in_next[len]) {
315 *pending_lt_ptr = cur_node;
316 pending_lt_ptr = bt_right_child(mf, cur_node);
317 cur_node = *pending_lt_ptr;
319 if (best_gt_len < len)
322 *pending_gt_ptr = cur_node;
323 pending_gt_ptr = bt_left_child(mf, cur_node);
324 cur_node = *pending_gt_ptr;
326 if (best_lt_len < len)
330 if (!matchfinder_node_valid(cur_node) || !--depth_remaining) {
331 *pending_lt_ptr = MATCHFINDER_NULL;
332 *pending_gt_ptr = MATCHFINDER_NULL;
338 #endif /* _BT_MATCHFINDER_H */