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 main data structure is a hash table where each hash bucket contains a
15 * binary tree of sequences whose first 4 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 4 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_HASH3_ORDER 15
55 #define BT_MATCHFINDER_HASH4_ORDER 16
57 /* TEMPLATED functions and structures have MF_SUFFIX appended to their name. */
59 #define TEMPLATED(name) CONCAT(name, MF_SUFFIX)
61 #ifndef _WIMLIB_BT_MATCHFINDER_H
62 #define _WIMLIB_BT_MATCHFINDER_H
64 /* Non-templated definitions */
66 /* Representation of a match found by the bt_matchfinder */
69 /* The number of bytes matched. */
72 /* The offset back from the current position that was matched. */
76 #endif /* _WIMLIB_BT_MATCHFINDER_H */
78 struct TEMPLATED(bt_matchfinder) {
80 /* The hash table for finding length 2 matches, if enabled */
81 #ifdef BT_MATCHFINDER_HASH2_ORDER
82 mf_pos_t hash2_tab[1UL << BT_MATCHFINDER_HASH2_ORDER];
85 /* The hash table for finding length 3 matches */
86 mf_pos_t hash3_tab[1UL << BT_MATCHFINDER_HASH3_ORDER];
88 /* The hash table which contains the roots of the binary trees for
89 * finding length 4+ matches */
90 mf_pos_t hash4_tab[1UL << BT_MATCHFINDER_HASH4_ORDER];
92 /* The child node references for the binary trees. The left and right
93 * children of the node for the sequence with position 'pos' are
94 * 'child_tab[pos * 2]' and 'child_tab[pos * 2 + 1]', respectively. */
98 /* Return the number of bytes that must be allocated for a 'bt_matchfinder' that
99 * can work with buffers up to the specified size. */
101 TEMPLATED(bt_matchfinder_size)(size_t max_bufsize)
103 return sizeof(struct TEMPLATED(bt_matchfinder)) +
104 (2 * max_bufsize * sizeof(mf_pos_t));
107 /* Prepare the matchfinder for a new input buffer. */
109 TEMPLATED(bt_matchfinder_init)(struct TEMPLATED(bt_matchfinder) *mf)
111 memset(mf, 0, sizeof(*mf));
114 static inline mf_pos_t *
115 TEMPLATED(bt_left_child)(struct TEMPLATED(bt_matchfinder) *mf, u32 node)
117 return &mf->child_tab[(node << 1) + 0];
120 static inline mf_pos_t *
121 TEMPLATED(bt_right_child)(struct TEMPLATED(bt_matchfinder) *mf, u32 node)
123 return &mf->child_tab[(node << 1) + 1];
126 /* Advance the binary tree matchfinder by one byte, optionally recording
127 * matches. @record_matches should be a compile-time constant. */
128 static inline struct lz_match *
129 TEMPLATED(bt_matchfinder_advance_one_byte)(struct TEMPLATED(bt_matchfinder) * const restrict mf,
130 const u8 * const restrict in_begin,
131 const ptrdiff_t cur_pos,
134 const u32 max_search_depth,
135 u32 next_hashes[const restrict static 2],
136 u32 * const restrict best_len_ret,
137 struct lz_match * restrict lz_matchptr,
138 const bool record_matches)
140 const u8 *in_next = in_begin + cur_pos;
141 u32 depth_remaining = max_search_depth;
146 #ifdef BT_MATCHFINDER_HASH2_ORDER
152 mf_pos_t *pending_lt_ptr, *pending_gt_ptr;
153 u32 best_lt_len, best_gt_len;
157 next_seq4 = load_u32_unaligned(in_next + 1);
158 next_seq3 = loaded_u32_to_u24(next_seq4);
160 hash3 = next_hashes[0];
161 hash4 = next_hashes[1];
163 next_hashes[0] = lz_hash(next_seq3, BT_MATCHFINDER_HASH3_ORDER);
164 next_hashes[1] = lz_hash(next_seq4, BT_MATCHFINDER_HASH4_ORDER);
165 prefetchw(&mf->hash3_tab[next_hashes[0]]);
166 prefetchw(&mf->hash4_tab[next_hashes[1]]);
168 #ifdef BT_MATCHFINDER_HASH2_ORDER
169 seq2 = load_u16_unaligned(in_next);
170 hash2 = lz_hash(seq2, BT_MATCHFINDER_HASH2_ORDER);
171 cur_node = mf->hash2_tab[hash2];
172 mf->hash2_tab[hash2] = cur_pos;
173 if (record_matches &&
174 seq2 == load_u16_unaligned(&in_begin[cur_node]) &&
175 likely(in_next != in_begin))
177 lz_matchptr->length = 2;
178 lz_matchptr->offset = in_next - &in_begin[cur_node];
183 cur_node = mf->hash3_tab[hash3];
184 mf->hash3_tab[hash3] = cur_pos;
185 if (record_matches &&
186 load_u24_unaligned(in_next) == load_u24_unaligned(&in_begin[cur_node]) &&
187 likely(in_next != in_begin))
189 lz_matchptr->length = 3;
190 lz_matchptr->offset = in_next - &in_begin[cur_node];
194 cur_node = mf->hash4_tab[hash4];
195 mf->hash4_tab[hash4] = cur_pos;
197 pending_lt_ptr = TEMPLATED(bt_left_child)(mf, cur_pos);
198 pending_gt_ptr = TEMPLATED(bt_right_child)(mf, cur_pos);
203 *best_len_ret = best_len;
212 matchptr = &in_begin[cur_node];
214 if (matchptr[len] == in_next[len]) {
215 len = lz_extend(in_next, matchptr, len + 1,
216 (record_matches ? max_len : nice_len));
217 if (!record_matches || len > best_len) {
218 if (record_matches) {
220 lz_matchptr->length = len;
221 lz_matchptr->offset = in_next - matchptr;
224 if (len >= nice_len) {
225 *pending_lt_ptr = *TEMPLATED(bt_left_child)(mf, cur_node);
226 *pending_gt_ptr = *TEMPLATED(bt_right_child)(mf, cur_node);
227 *best_len_ret = best_len;
233 if (matchptr[len] < in_next[len]) {
234 *pending_lt_ptr = cur_node;
235 pending_lt_ptr = TEMPLATED(bt_right_child)(mf, cur_node);
236 cur_node = *pending_lt_ptr;
238 if (best_gt_len < len)
241 *pending_gt_ptr = cur_node;
242 pending_gt_ptr = TEMPLATED(bt_left_child)(mf, cur_node);
243 cur_node = *pending_gt_ptr;
245 if (best_lt_len < len)
249 if (!cur_node || !--depth_remaining) {
252 *best_len_ret = best_len;
259 * Retrieve a list of matches with the current position.
262 * The matchfinder structure.
264 * Pointer to the beginning of the input buffer.
266 * The current position in the input buffer (the position of the sequence
267 * being matched against).
269 * The maximum permissible match length at this position. Must be >= 5.
271 * Stop searching if a match of at least this length is found.
272 * Must be <= @max_len.
274 * Limit on the number of potential matches to consider. Must be >= 1.
276 * The precomputed hash codes for the sequence beginning at @in_next.
277 * These will be used and then updated with the precomputed hashcodes for
278 * the sequence beginning at @in_next + 1.
280 * If a match of length >= 4 was found, then the length of the longest such
281 * match is written here; otherwise 3 is written here. (Note: this is
282 * redundant with the 'struct lz_match' array, but this is easier for the
283 * compiler to optimize when inlined and the caller immediately does a
284 * check against 'best_len'.)
286 * An array in which this function will record the matches. The recorded
287 * matches will be sorted by strictly increasing length and (non-strictly)
288 * increasing offset. The maximum number of matches that may be found is
289 * 'nice_len - 1', or one less if length 2 matches are disabled.
291 * The return value is a pointer to the next available slot in the @lz_matchptr
292 * array. (If no matches were found, this will be the same as @lz_matchptr.)
294 static inline struct lz_match *
295 TEMPLATED(bt_matchfinder_get_matches)(struct TEMPLATED(bt_matchfinder) *mf,
300 u32 max_search_depth,
301 u32 next_hashes[static 2],
303 struct lz_match *lz_matchptr)
305 return TEMPLATED(bt_matchfinder_advance_one_byte)(mf,
318 * Advance the matchfinder, but don't record any matches.
320 * This is very similar to bt_matchfinder_get_matches() because both functions
321 * must do hashing and tree re-rooting.
324 TEMPLATED(bt_matchfinder_skip_position)(struct TEMPLATED(bt_matchfinder) *mf,
329 u32 max_search_depth,
330 u32 next_hashes[static 2])
333 TEMPLATED(bt_matchfinder_advance_one_byte)(mf,