]> wimlib.net Git - wimlib/blob - include/wimlib/bt_matchfinder.h
4fe754c95c3e7f181460ad01bd3b41646bd7a384
[wimlib] / include / wimlib / bt_matchfinder.h
1 /*
2  * bt_matchfinder.h
3  *
4  * Author:      Eric Biggers
5  * Year:        2014, 2015
6  *
7  * The author dedicates this file to the public domain.
8  * You can do whatever you want with this file.
9  *
10  * ----------------------------------------------------------------------------
11  *
12  * This is a Binary Trees (bt) based matchfinder.
13  *
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.
20  *
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.
28  *
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.
37  *
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.
44  *
45  * ----------------------------------------------------------------------------
46  */
47
48 #ifndef _BT_MATCHFINDER_H
49 #define _BT_MATCHFINDER_H
50
51 #include "wimlib/lz_extend.h"
52 #include "wimlib/lz_hash.h"
53 #include "wimlib/matchfinder_common.h"
54
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
59 #else
60 #  define BT_MATCHFINDER_HASH_ORDER 16
61 #endif
62
63 #define BT_MATCHFINDER_HASH_LENGTH      (1UL << BT_MATCHFINDER_HASH_ORDER)
64
65 struct bt_matchfinder {
66         pos_t hash_tab[BT_MATCHFINDER_HASH_LENGTH];
67         pos_t child_tab[];
68 } _aligned_attribute(MATCHFINDER_ALIGNMENT);
69
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.  */
72 static inline size_t
73 bt_matchfinder_size(size_t max_bufsize)
74 {
75         return sizeof(pos_t) * (BT_MATCHFINDER_HASH_LENGTH + (2 * max_bufsize));
76 }
77
78 /* Prepare the matchfinder for a new input buffer.  */
79 static inline void
80 bt_matchfinder_init(struct bt_matchfinder *mf)
81 {
82         matchfinder_init(mf->hash_tab, BT_MATCHFINDER_HASH_LENGTH);
83 }
84
85 static inline u32
86 bt_matchfinder_hash_3_bytes(const u8 *in_next)
87 {
88         return lz_hash_3_bytes(in_next, BT_MATCHFINDER_HASH_ORDER);
89 }
90
91 static inline pos_t *
92 bt_child(struct bt_matchfinder *mf, pos_t node, int offset)
93 {
94         if (MATCHFINDER_MAX_WINDOW_ORDER < sizeof(pos_t) * 8) {
95                 /* no cast needed */
96                 return &mf->child_tab[(node << 1) + offset];
97         } else {
98                 return &mf->child_tab[((size_t)node << 1) + offset];
99         }
100 }
101
102 static inline pos_t *
103 bt_left_child(struct bt_matchfinder *mf, pos_t node)
104 {
105         return bt_child(mf, node, 0);
106 }
107
108 static inline pos_t *
109 bt_right_child(struct bt_matchfinder *mf, pos_t node)
110 {
111         return bt_child(mf, node, 1);
112 }
113
114 /*
115  * Retrieve a list of matches with the current position.
116  *
117  * @mf
118  *      The matchfinder structure.
119  * @in_begin
120  *      Pointer to the beginning of the input buffer.
121  * @in_next
122  *      Pointer to the next byte in the input buffer to process.  This is the
123  *      pointer to the sequence being matched against.
124  * @min_len
125  *      Only record matches that are at least this long.
126  * @max_len
127  *      The maximum permissible match length at this position.
128  * @nice_len
129  *      Stop searching if a match of at least this length is found.
130  *      Must be <= @max_len.
131  * @max_search_depth
132  *      Limit on the number of potential matches to consider.  Must be >= 1.
133  * @next_hash
134  *      Pointer to the hash code for the current sequence, which was computed
135  *      one position in advance so that the binary tree root could be
136  *      prefetched.  This is an input/output parameter.
137  * @best_len_ret
138  *      The length of the longest match found is written here.  (This is
139  *      actually redundant with the 'struct lz_match' array, but this is easier
140  *      for the compiler to optimize when inlined and the caller immediately
141  *      does a check against 'best_len'.)
142  * @lz_matchptr
143  *      An array in which this function will record the matches.  The recorded
144  *      matches will be sorted by strictly increasing length and strictly
145  *      increasing offset.  The maximum number of matches that may be found is
146  *      'min(nice_len, max_len) - 3 + 1'.
147  *
148  * The return value is a pointer to the next available slot in the @lz_matchptr
149  * array.  (If no matches were found, this will be the same as @lz_matchptr.)
150  */
151 static inline struct lz_match *
152 bt_matchfinder_get_matches(struct bt_matchfinder * const restrict mf,
153                            const u8 * const in_begin,
154                            const u8 * const in_next,
155                            const unsigned min_len,
156                            const unsigned max_len,
157                            const unsigned nice_len,
158                            const unsigned max_search_depth,
159                            u32 * restrict next_hash,
160                            unsigned * restrict best_len_ret,
161                            struct lz_match * restrict lz_matchptr)
162 {
163         unsigned depth_remaining = max_search_depth;
164         u32 hash;
165         pos_t cur_node;
166         const u8 *matchptr;
167         pos_t *pending_lt_ptr, *pending_gt_ptr;
168         unsigned best_lt_len, best_gt_len;
169         unsigned len;
170         unsigned best_len = min_len - 1;
171
172         if (unlikely(max_len < LZ_HASH3_REQUIRED_NBYTES + 1)) {
173                 *best_len_ret = best_len;
174                 return lz_matchptr;
175         }
176
177         hash = *next_hash;
178         *next_hash = bt_matchfinder_hash_3_bytes(in_next + 1);
179         cur_node = mf->hash_tab[hash];
180         mf->hash_tab[hash] = in_next - in_begin;
181         prefetchw(&mf->hash_tab[*next_hash]);
182
183         pending_lt_ptr = bt_left_child(mf, in_next - in_begin);
184         pending_gt_ptr = bt_right_child(mf, in_next - in_begin);
185         best_lt_len = 0;
186         best_gt_len = 0;
187         len = 0;
188
189         if (!matchfinder_node_valid(cur_node)) {
190                 *pending_lt_ptr = MATCHFINDER_NULL;
191                 *pending_gt_ptr = MATCHFINDER_NULL;
192                 *best_len_ret = best_len;
193                 return lz_matchptr;
194         }
195
196         for (;;) {
197                 matchptr = &in_begin[cur_node];
198
199                 if (matchptr[len] == in_next[len]) {
200                         len = lz_extend(in_next, matchptr, len + 1, max_len);
201                         if (len > best_len) {
202                                 best_len = len;
203                                 lz_matchptr->length = len;
204                                 lz_matchptr->offset = in_next - matchptr;
205                                 lz_matchptr++;
206                                 if (len >= nice_len) {
207                                         *pending_lt_ptr = *bt_left_child(mf, cur_node);
208                                         *pending_gt_ptr = *bt_right_child(mf, cur_node);
209                                         *best_len_ret = best_len;
210                                         return lz_matchptr;
211                                 }
212                         }
213                 }
214
215                 if (matchptr[len] < in_next[len]) {
216                         *pending_lt_ptr = cur_node;
217                         pending_lt_ptr = bt_right_child(mf, cur_node);
218                         cur_node = *pending_lt_ptr;
219                         best_lt_len = len;
220                         if (best_gt_len < len)
221                                 len = best_gt_len;
222                 } else {
223                         *pending_gt_ptr = cur_node;
224                         pending_gt_ptr = bt_left_child(mf, cur_node);
225                         cur_node = *pending_gt_ptr;
226                         best_gt_len = len;
227                         if (best_lt_len < len)
228                                 len = best_lt_len;
229                 }
230
231                 if (!matchfinder_node_valid(cur_node) || !--depth_remaining) {
232                         *pending_lt_ptr = MATCHFINDER_NULL;
233                         *pending_gt_ptr = MATCHFINDER_NULL;
234                         *best_len_ret = best_len;
235                         return lz_matchptr;
236                 }
237         }
238 }
239
240 /*
241  * Advance the matchfinder, but don't record any matches.
242  *
243  * @mf
244  *      The matchfinder structure.
245  * @in_begin
246  *      Pointer to the beginning of the input buffer.
247  * @in_next
248  *      Pointer to the next byte in the input buffer to process.
249  * @in_end
250  *      Pointer to the end of the input buffer.
251  * @nice_len
252  *      Stop searching if a match of at least this length is found.
253  * @max_search_depth
254  *      Limit on the number of potential matches to consider.
255  * @next_hash
256  *      Pointer to the hash code for the current sequence, which was computed
257  *      one position in advance so that the binary tree root could be
258  *      prefetched.  This is an input/output parameter.
259  *
260  * Note: this is very similar to bt_matchfinder_get_matches() because both
261  * functions must do hashing and tree re-rooting.  This version just doesn't
262  * actually record any matches.
263  */
264 static inline void
265 bt_matchfinder_skip_position(struct bt_matchfinder * const restrict mf,
266                              const u8 * const in_begin,
267                              const u8 * const in_next,
268                              const u8 * const in_end,
269                              const unsigned nice_len,
270                              const unsigned max_search_depth,
271                              u32 * restrict next_hash)
272 {
273         unsigned depth_remaining = max_search_depth;
274         u32 hash;
275         pos_t cur_node;
276         const u8 *matchptr;
277         pos_t *pending_lt_ptr, *pending_gt_ptr;
278         unsigned best_lt_len, best_gt_len;
279         unsigned len;
280
281         if (unlikely(in_end - in_next < LZ_HASH3_REQUIRED_NBYTES + 1))
282                 return;
283
284         hash = *next_hash;
285         *next_hash = bt_matchfinder_hash_3_bytes(in_next + 1);
286         cur_node = mf->hash_tab[hash];
287         mf->hash_tab[hash] = in_next - in_begin;
288         prefetchw(&mf->hash_tab[*next_hash]);
289
290         depth_remaining = max_search_depth;
291         pending_lt_ptr = bt_left_child(mf, in_next - in_begin);
292         pending_gt_ptr = bt_right_child(mf, in_next - in_begin);
293         best_lt_len = 0;
294         best_gt_len = 0;
295         len = 0;
296
297         if (!matchfinder_node_valid(cur_node)) {
298                 *pending_lt_ptr = MATCHFINDER_NULL;
299                 *pending_gt_ptr = MATCHFINDER_NULL;
300                 return;
301         }
302
303         for (;;) {
304                 matchptr = &in_begin[cur_node];
305
306                 if (matchptr[len] == in_next[len]) {
307                         len = lz_extend(in_next, matchptr, len + 1, nice_len);
308                         if (len == nice_len) {
309                                 *pending_lt_ptr = *bt_left_child(mf, cur_node);
310                                 *pending_gt_ptr = *bt_right_child(mf, cur_node);
311                                 return;
312                         }
313                 }
314
315                 if (matchptr[len] < in_next[len]) {
316                         *pending_lt_ptr = cur_node;
317                         pending_lt_ptr = bt_right_child(mf, cur_node);
318                         cur_node = *pending_lt_ptr;
319                         best_lt_len = len;
320                         if (best_gt_len < len)
321                                 len = best_gt_len;
322                 } else {
323                         *pending_gt_ptr = cur_node;
324                         pending_gt_ptr = bt_left_child(mf, cur_node);
325                         cur_node = *pending_gt_ptr;
326                         best_gt_len = len;
327                         if (best_lt_len < len)
328                                 len = best_lt_len;
329                 }
330
331                 if (!matchfinder_node_valid(cur_node) || !--depth_remaining) {
332                         *pending_lt_ptr = MATCHFINDER_NULL;
333                         *pending_gt_ptr = MATCHFINDER_NULL;
334                         return;
335                 }
336         }
337 }
338
339 #endif /* _BT_MATCHFINDER_H */