zenflate 0.3.0

Pure Rust DEFLATE/zlib/gzip compression and decompression
Documentation
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
//! Binary Trees (bt) matchfinder for near-optimal compression.
//!
//! Ported from libdeflate's `bt_matchfinder.h`.
//!
//! Uses a hash table of binary trees where each bucket contains sequences
//! whose first 4 bytes share the same hash code. Each tree is sorted
//! lexicographically: left children are lesser, right children are greater.
//!
//! At each byte position, a new node is created, the tree is traversed to
//! find matches and re-rooted at the new node. Compared to hash chains,
//! the binary tree finds more matches per step (ideally log(n) vs n)
//! but requires nearly twice as much memory.

#[cfg(not(feature = "std"))]
use alloc::vec::Vec;

#[cfg(not(feature = "unchecked"))]
use super::lz_extend;
use super::{
    MATCHFINDER_INITVAL, MATCHFINDER_WINDOW_SIZE, lz_hash, matchfinder_init, matchfinder_rebase,
};

/// Hash order for length 3 matches.
const BT_MATCHFINDER_HASH3_ORDER: u32 = 16;

/// Number of ways per hash3 bucket.
const BT_MATCHFINDER_HASH3_WAYS: usize = 2;

/// Hash order for length 4+ matches (binary tree roots).
const BT_MATCHFINDER_HASH4_ORDER: u32 = 16;

/// Number of hash3 buckets.
const BT_HASH3_SIZE: usize = 1 << BT_MATCHFINDER_HASH3_ORDER;

/// Number of hash4 buckets.
const BT_HASH4_SIZE: usize = 1 << BT_MATCHFINDER_HASH4_ORDER;

/// Minimum bytes remaining required for get_matches() / skip_byte().
pub(crate) const BT_MATCHFINDER_REQUIRED_NBYTES: u32 = 5;

/// Window mask for child table indexing.
const WINDOW_MASK: usize = MATCHFINDER_WINDOW_SIZE as usize - 1;

// Storage types: fixed arrays when `unchecked` (single allocation via Box),
// Vec when safe (separate heap allocations per field).
#[cfg(feature = "unchecked")]
type BtHash3Tab = [i16; BT_HASH3_SIZE * BT_MATCHFINDER_HASH3_WAYS];
#[cfg(not(feature = "unchecked"))]
type BtHash3Tab = Vec<i16>;

#[cfg(feature = "unchecked")]
type BtHash4Tab = [i16; BT_HASH4_SIZE];
#[cfg(not(feature = "unchecked"))]
type BtHash4Tab = Vec<i16>;

#[cfg(feature = "unchecked")]
type BtChildTab = [i16; 2 * MATCHFINDER_WINDOW_SIZE as usize];
#[cfg(not(feature = "unchecked"))]
type BtChildTab = Vec<i16>;

/// A match found by the bt_matchfinder.
#[derive(Clone, Copy, Default)]
pub(crate) struct LzMatch {
    /// Number of bytes matched.
    pub length: u16,
    /// Offset back from the current position.
    pub offset: u16,
}

/// Binary tree matchfinder for near-optimal compression (levels 10-12).
///
/// With `unchecked`, uses fixed arrays so the whole NearOptimalState
/// becomes a single allocation via `Box::new_uninit()`.
/// Without `unchecked`, uses Vec (separate heap allocations per field).
#[derive(Clone)]
pub(crate) struct BtMatchfinder {
    /// Hash table for length 3 matches (2-way). Flat: [BT_HASH3_SIZE * 2].
    hash3_tab: BtHash3Tab,
    /// Hash table containing roots of binary trees for length 4+ matches.
    hash4_tab: BtHash4Tab,
    /// Child node references: left child at [2*(pos & WINDOW_MASK)],
    /// right child at [2*(pos & WINDOW_MASK) + 1].
    child_tab: BtChildTab,
}

impl BtMatchfinder {
    /// Create and initialize a new BtMatchfinder (safe path, uses Vec).
    #[cfg(not(feature = "unchecked"))]
    pub fn new() -> Self {
        Self {
            hash3_tab: alloc::vec![MATCHFINDER_INITVAL; BT_HASH3_SIZE * BT_MATCHFINDER_HASH3_WAYS],
            hash4_tab: alloc::vec![MATCHFINDER_INITVAL; BT_HASH4_SIZE],
            child_tab: alloc::vec![MATCHFINDER_INITVAL; 2 * MATCHFINDER_WINDOW_SIZE as usize],
        }
    }

    /// Initialize a BtMatchfinder in-place through a raw pointer.
    ///
    /// Used by `NearOptimalState::new()` to initialize fields within a
    /// `Box::new_uninit()` allocation without touching the stack.
    ///
    /// # Safety
    /// `ptr` must point to a valid, writable allocation of `BtMatchfinder`.
    #[cfg(feature = "unchecked")]
    pub(crate) unsafe fn init_at(ptr: *mut Self) {
        unsafe {
            let h3 = core::ptr::addr_of_mut!((*ptr).hash3_tab) as *mut i16;
            core::slice::from_raw_parts_mut(h3, BT_HASH3_SIZE * BT_MATCHFINDER_HASH3_WAYS)
                .fill(MATCHFINDER_INITVAL);
            let h4 = core::ptr::addr_of_mut!((*ptr).hash4_tab) as *mut i16;
            core::slice::from_raw_parts_mut(h4, BT_HASH4_SIZE).fill(MATCHFINDER_INITVAL);
            let ct = core::ptr::addr_of_mut!((*ptr).child_tab) as *mut i16;
            core::slice::from_raw_parts_mut(ct, 2 * MATCHFINDER_WINDOW_SIZE as usize)
                .fill(MATCHFINDER_INITVAL);
        }
    }

    /// Initialize (reset) the matchfinder for a new input buffer.
    /// Only hash tables are reset; child_tab entries are written before read.
    pub fn init(&mut self) {
        matchfinder_init(&mut self.hash3_tab);
        matchfinder_init(&mut self.hash4_tab);
    }

    /// Slide the window by MATCHFINDER_WINDOW_SIZE.
    /// Rebases all tables including child_tab.
    pub fn slide_window(&mut self) {
        matchfinder_rebase(&mut self.hash3_tab);
        matchfinder_rebase(&mut self.hash4_tab);
        matchfinder_rebase(&mut self.child_tab);
    }

    #[inline(always)]
    fn left_child_idx(node: i32) -> usize {
        2 * (node as usize & WINDOW_MASK)
    }

    #[inline(always)]
    fn right_child_idx(node: i32) -> usize {
        2 * (node as usize & WINDOW_MASK) + 1
    }

    /// Get matches at the current position.
    ///
    /// `input` is the full input buffer.
    /// `in_base_offset` is the absolute offset of the window base in input.
    /// `cur_pos` is the position relative to the window base.
    /// `max_len` must be >= BT_MATCHFINDER_REQUIRED_NBYTES.
    /// `nice_len` is the "nice" length: stop if we find a match this long.
    ///
    /// Returns the number of matches written to `matches`.
    /// Matches are sorted by strictly increasing length.
    #[cfg_attr(feature = "unchecked", allow(dead_code))]
    #[inline(always)]
    #[allow(clippy::too_many_arguments)]
    pub fn get_matches(
        &mut self,
        input: &[u8],
        in_base_offset: usize,
        cur_pos: i32,
        max_len: u32,
        nice_len: u32,
        max_search_depth: u32,
        next_hashes: &mut [u32; 2],
        matches: &mut [LzMatch],
    ) -> usize {
        self.advance_one_byte::<true>(
            input,
            in_base_offset,
            cur_pos,
            max_len,
            nice_len,
            max_search_depth,
            next_hashes,
            matches,
        )
    }

    /// Skip one byte: maintain the tree structure without recording matches.
    #[cfg_attr(feature = "unchecked", allow(dead_code))]
    #[inline(always)]
    pub fn skip_byte(
        &mut self,
        input: &[u8],
        in_base_offset: usize,
        cur_pos: i32,
        nice_len: u32,
        max_search_depth: u32,
        next_hashes: &mut [u32; 2],
    ) {
        self.advance_one_byte::<false>(
            input,
            in_base_offset,
            cur_pos,
            nice_len, // max_len = nice_len for skip (matches C behavior)
            nice_len,
            max_search_depth,
            next_hashes,
            &mut [],
        );
    }

    /// Core method: advance one byte, optionally recording matches.
    ///
    /// When RECORD_MATCHES=true, finds all matches and writes them to `matches`.
    /// When RECORD_MATCHES=false, only maintains tree structure (skip).
    #[cfg_attr(feature = "unchecked", allow(dead_code))]
    #[inline(always)]
    #[allow(clippy::too_many_arguments)]
    fn advance_one_byte<const RECORD_MATCHES: bool>(
        &mut self,
        input: &[u8],
        in_base_offset: usize,
        cur_pos: i32,
        max_len: u32,
        nice_len: u32,
        max_search_depth: u32,
        next_hashes: &mut [u32; 2],
        matches: &mut [LzMatch],
    ) -> usize {
        #[cfg(not(feature = "unchecked"))]
        use crate::fast_bytes::get_byte;
        use crate::fast_bytes::{load_u32_le, prefetch};

        let in_next = (in_base_offset as isize + cur_pos as isize) as usize;
        let mut depth_remaining = max_search_depth;
        let cutoff = cur_pos - MATCHFINDER_WINDOW_SIZE as i32;
        let mut match_count = 0usize;
        let mut best_len = 3u32;

        // Precompute next position's hashes
        let next_hashseq = load_u32_le(input, in_next + 1);
        let hash3 = next_hashes[0] as usize;
        let hash4 = next_hashes[1] as usize;
        next_hashes[0] = lz_hash(next_hashseq & 0xFFFFFF, BT_MATCHFINDER_HASH3_ORDER);
        next_hashes[1] = lz_hash(next_hashseq, BT_MATCHFINDER_HASH4_ORDER);
        prefetch(&self.hash3_tab[next_hashes[0] as usize * BT_MATCHFINDER_HASH3_WAYS]);
        prefetch(&self.hash4_tab[next_hashes[1] as usize]);

        // Hash3: 2-way check for length 3 matches
        let h3 = hash3 * BT_MATCHFINDER_HASH3_WAYS;
        let cur_node_0 = self.hash3_tab[h3] as i32;
        self.hash3_tab[h3] = cur_pos as i16;
        let cur_node_1 = self.hash3_tab[h3 + 1] as i32;
        self.hash3_tab[h3 + 1] = cur_node_0 as i16;

        if RECORD_MATCHES && cur_node_0 > cutoff {
            // Load 4 bytes and mask to 3 — matches C pattern and requires only one load
            let seq3 = load_u32_le(input, in_next) & 0xFFFFFF;
            let match0_pos = (in_base_offset as isize + cur_node_0 as isize) as usize;
            if seq3 == load_u32_le(input, match0_pos) & 0xFFFFFF {
                matches[match_count] = LzMatch {
                    length: 3,
                    offset: (in_next - match0_pos) as u16,
                };
                match_count += 1;
            } else if cur_node_1 > cutoff {
                let match1_pos = (in_base_offset as isize + cur_node_1 as isize) as usize;
                if seq3 == load_u32_le(input, match1_pos) & 0xFFFFFF {
                    matches[match_count] = LzMatch {
                        length: 3,
                        offset: (in_next - match1_pos) as u16,
                    };
                    match_count += 1;
                }
            }
        }

        // Hash4: binary tree for length 4+ matches
        let mut cur_node = self.hash4_tab[hash4] as i32;
        self.hash4_tab[hash4] = cur_pos as i16;

        let mut pending_lt_idx = Self::left_child_idx(cur_pos);
        let mut pending_gt_idx = Self::right_child_idx(cur_pos);

        if cur_node <= cutoff {
            self.child_tab[pending_lt_idx] = MATCHFINDER_INITVAL;
            self.child_tab[pending_gt_idx] = MATCHFINDER_INITVAL;
            return match_count;
        }

        let mut best_lt_len = 0u32;
        let mut best_gt_len = 0u32;
        let mut len = 0u32;

        // Raw-pointer inner loop: eliminates fat-pointer register pressure.
        // input (&[u8] = 2 regs) → input_ptr (*const u8 = 1 reg), freeing
        // a register in this spill-heavy loop (~16 live values, 14 GPRs).
        #[cfg(feature = "unchecked")]
        {
            use super::raw::lz_extend_raw;

            let input_ptr = input.as_ptr();
            let child_ptr = self.child_tab.as_mut_ptr();

            // SAFETY: All offsets are bounded by in_end <= input.len() (checked
            // by callers) and child_tab indices are masked to WINDOW_MASK.
            unsafe {
                loop {
                    let match_pos = (in_base_offset as isize + cur_node as isize) as usize;

                    if *input_ptr.add(match_pos + len as usize)
                        == *input_ptr.add(in_next + len as usize)
                    {
                        len = lz_extend_raw(
                            input_ptr.add(in_next),
                            input_ptr.add(match_pos),
                            len + 1,
                            max_len,
                        );
                        if !RECORD_MATCHES || len > best_len {
                            if RECORD_MATCHES {
                                best_len = len;
                                matches[match_count] = LzMatch {
                                    length: len as u16,
                                    offset: (in_next - match_pos) as u16,
                                };
                                match_count += 1;
                            }
                            if len >= nice_len {
                                *child_ptr.add(pending_lt_idx) =
                                    *child_ptr.add(Self::left_child_idx(cur_node));
                                *child_ptr.add(pending_gt_idx) =
                                    *child_ptr.add(Self::right_child_idx(cur_node));
                                return match_count;
                            }
                        }
                    }

                    if *input_ptr.add(match_pos + len as usize)
                        < *input_ptr.add(in_next + len as usize)
                    {
                        *child_ptr.add(pending_lt_idx) = cur_node as i16;
                        pending_lt_idx = Self::right_child_idx(cur_node);
                        cur_node = *child_ptr.add(pending_lt_idx) as i32;
                        best_lt_len = len;
                        if best_gt_len < len {
                            len = best_gt_len;
                        }
                    } else {
                        *child_ptr.add(pending_gt_idx) = cur_node as i16;
                        pending_gt_idx = Self::left_child_idx(cur_node);
                        cur_node = *child_ptr.add(pending_gt_idx) as i32;
                        best_gt_len = len;
                        if best_lt_len < len {
                            len = best_lt_len;
                        }
                    }

                    depth_remaining -= 1;
                    if cur_node <= cutoff || depth_remaining == 0 {
                        *child_ptr.add(pending_lt_idx) = MATCHFINDER_INITVAL;
                        *child_ptr.add(pending_gt_idx) = MATCHFINDER_INITVAL;
                        return match_count;
                    }
                }
            }
        }

        #[cfg(not(feature = "unchecked"))]
        loop {
            let match_pos = (in_base_offset as isize + cur_node as isize) as usize;

            if get_byte(input, match_pos + len as usize) == get_byte(input, in_next + len as usize)
            {
                len = lz_extend(&input[in_next..], &input[match_pos..], len + 1, max_len);
                if !RECORD_MATCHES || len > best_len {
                    if RECORD_MATCHES {
                        best_len = len;
                        matches[match_count] = LzMatch {
                            length: len as u16,
                            offset: (in_next - match_pos) as u16,
                        };
                        match_count += 1;
                    }
                    if len >= nice_len {
                        self.child_tab[pending_lt_idx] =
                            self.child_tab[Self::left_child_idx(cur_node)];
                        self.child_tab[pending_gt_idx] =
                            self.child_tab[Self::right_child_idx(cur_node)];
                        return match_count;
                    }
                }
            }

            if get_byte(input, match_pos + len as usize) < get_byte(input, in_next + len as usize) {
                self.child_tab[pending_lt_idx] = cur_node as i16;
                pending_lt_idx = Self::right_child_idx(cur_node);
                cur_node = self.child_tab[pending_lt_idx] as i32;
                best_lt_len = len;
                if best_gt_len < len {
                    len = best_gt_len;
                }
            } else {
                self.child_tab[pending_gt_idx] = cur_node as i16;
                pending_gt_idx = Self::left_child_idx(cur_node);
                cur_node = self.child_tab[pending_gt_idx] as i32;
                best_gt_len = len;
                if best_lt_len < len {
                    len = best_lt_len;
                }
            }

            depth_remaining -= 1;
            if cur_node <= cutoff || depth_remaining == 0 {
                self.child_tab[pending_lt_idx] = MATCHFINDER_INITVAL;
                self.child_tab[pending_gt_idx] = MATCHFINDER_INITVAL;
                return match_count;
            }
        }
    }

    /// Raw-pointer get_matches: eliminates fat-pointer register pressure.
    ///
    /// Takes `*const u8` instead of `&[u8]` and `*mut LzMatch` instead of
    /// `&mut [LzMatch]`, freeing 2 registers in the caller's inlined hot loop.
    ///
    /// # Safety
    ///
    /// `input_ptr` must be valid for reads of at least `in_base_offset + cur_pos + max_len` bytes.
    /// `matches_ptr` must be valid for writes of at least `MAX_MATCHES_PER_POS` entries.
    #[cfg(feature = "unchecked")]
    #[inline(always)]
    #[allow(clippy::too_many_arguments)]
    pub unsafe fn get_matches_raw(
        &mut self,
        input_ptr: *const u8,
        in_base_offset: usize,
        cur_pos: i32,
        max_len: u32,
        nice_len: u32,
        max_search_depth: u32,
        next_hashes: &mut [u32; 2],
        matches_ptr: *mut LzMatch,
    ) -> usize {
        unsafe {
            self.advance_one_byte_raw::<true>(
                input_ptr,
                in_base_offset,
                cur_pos,
                max_len,
                nice_len,
                max_search_depth,
                next_hashes,
                matches_ptr,
            )
        }
    }

    /// Raw-pointer skip_byte: maintains tree structure without recording matches.
    ///
    /// # Safety
    ///
    /// `input_ptr` must be valid for reads of at least `in_base_offset + cur_pos + nice_len` bytes.
    #[cfg(feature = "unchecked")]
    #[inline(always)]
    pub unsafe fn skip_byte_raw(
        &mut self,
        input_ptr: *const u8,
        in_base_offset: usize,
        cur_pos: i32,
        nice_len: u32,
        max_search_depth: u32,
        next_hashes: &mut [u32; 2],
    ) {
        unsafe {
            self.advance_one_byte_raw::<false>(
                input_ptr,
                in_base_offset,
                cur_pos,
                nice_len,
                nice_len,
                max_search_depth,
                next_hashes,
                core::ptr::null_mut(),
            );
        }
    }

    /// Core raw-pointer method: advance one byte with all raw pointer access.
    ///
    /// Eliminates all fat pointers and bounds checks. Hash table access uses
    /// `get_unchecked`, input access uses raw pointer arithmetic.
    #[cfg(feature = "unchecked")]
    #[inline(always)]
    #[allow(clippy::too_many_arguments)]
    unsafe fn advance_one_byte_raw<const RECORD_MATCHES: bool>(
        &mut self,
        input_ptr: *const u8,
        in_base_offset: usize,
        cur_pos: i32,
        max_len: u32,
        nice_len: u32,
        max_search_depth: u32,
        next_hashes: &mut [u32; 2],
        matches_ptr: *mut LzMatch,
    ) -> usize {
        use super::raw::lz_extend_raw;
        use crate::fast_bytes::{load_u32_le_ptr, prefetch_ptr};

        unsafe {
            let in_next = (in_base_offset as isize + cur_pos as isize) as usize;
            let mut depth_remaining = max_search_depth;
            let cutoff = cur_pos - MATCHFINDER_WINDOW_SIZE as i32;
            let mut match_count = 0usize;
            let mut best_len = 3u32;

            // Precompute next position's hashes
            let next_hashseq = load_u32_le_ptr(input_ptr, in_next + 1);
            let hash3 = next_hashes[0] as usize;
            let hash4 = next_hashes[1] as usize;
            next_hashes[0] = lz_hash(next_hashseq & 0xFFFFFF, BT_MATCHFINDER_HASH3_ORDER);
            next_hashes[1] = lz_hash(next_hashseq, BT_MATCHFINDER_HASH4_ORDER);
            prefetch_ptr(
                self.hash3_tab
                    .as_ptr()
                    .add(next_hashes[0] as usize * BT_MATCHFINDER_HASH3_WAYS)
                    as *const u8,
            );
            prefetch_ptr(self.hash4_tab.as_ptr().add(next_hashes[1] as usize) as *const u8);

            // Hash3: 2-way check for length 3 matches
            let h3 = hash3 * BT_MATCHFINDER_HASH3_WAYS;
            let cur_node_0 = *self.hash3_tab.get_unchecked(h3) as i32;
            *self.hash3_tab.get_unchecked_mut(h3) = cur_pos as i16;
            let cur_node_1 = *self.hash3_tab.get_unchecked(h3 + 1) as i32;
            *self.hash3_tab.get_unchecked_mut(h3 + 1) = cur_node_0 as i16;

            if RECORD_MATCHES && cur_node_0 > cutoff {
                let seq3 = load_u32_le_ptr(input_ptr, in_next) & 0xFFFFFF;
                let match0_pos = (in_base_offset as isize + cur_node_0 as isize) as usize;
                if seq3 == load_u32_le_ptr(input_ptr, match0_pos) & 0xFFFFFF {
                    *matches_ptr.add(match_count) = LzMatch {
                        length: 3,
                        offset: (in_next - match0_pos) as u16,
                    };
                    match_count += 1;
                } else if cur_node_1 > cutoff {
                    let match1_pos = (in_base_offset as isize + cur_node_1 as isize) as usize;
                    if seq3 == load_u32_le_ptr(input_ptr, match1_pos) & 0xFFFFFF {
                        *matches_ptr.add(match_count) = LzMatch {
                            length: 3,
                            offset: (in_next - match1_pos) as u16,
                        };
                        match_count += 1;
                    }
                }
            }

            // Hash4: binary tree for length 4+ matches
            let mut cur_node = *self.hash4_tab.get_unchecked(hash4) as i32;
            *self.hash4_tab.get_unchecked_mut(hash4) = cur_pos as i16;

            let mut pending_lt_idx = Self::left_child_idx(cur_pos);
            let mut pending_gt_idx = Self::right_child_idx(cur_pos);

            let child_ptr = self.child_tab.as_mut_ptr();

            if cur_node <= cutoff {
                *child_ptr.add(pending_lt_idx) = MATCHFINDER_INITVAL;
                *child_ptr.add(pending_gt_idx) = MATCHFINDER_INITVAL;
                return match_count;
            }

            let mut best_lt_len = 0u32;
            let mut best_gt_len = 0u32;
            let mut len = 0u32;

            loop {
                let match_pos = (in_base_offset as isize + cur_node as isize) as usize;

                if *input_ptr.add(match_pos + len as usize)
                    == *input_ptr.add(in_next + len as usize)
                {
                    len = lz_extend_raw(
                        input_ptr.add(in_next),
                        input_ptr.add(match_pos),
                        len + 1,
                        max_len,
                    );
                    if !RECORD_MATCHES || len > best_len {
                        if RECORD_MATCHES {
                            best_len = len;
                            *matches_ptr.add(match_count) = LzMatch {
                                length: len as u16,
                                offset: (in_next - match_pos) as u16,
                            };
                            match_count += 1;
                        }
                        if len >= nice_len {
                            *child_ptr.add(pending_lt_idx) =
                                *child_ptr.add(Self::left_child_idx(cur_node));
                            *child_ptr.add(pending_gt_idx) =
                                *child_ptr.add(Self::right_child_idx(cur_node));
                            return match_count;
                        }
                    }
                }

                if *input_ptr.add(match_pos + len as usize) < *input_ptr.add(in_next + len as usize)
                {
                    *child_ptr.add(pending_lt_idx) = cur_node as i16;
                    pending_lt_idx = Self::right_child_idx(cur_node);
                    cur_node = *child_ptr.add(pending_lt_idx) as i32;
                    best_lt_len = len;
                    if best_gt_len < len {
                        len = best_gt_len;
                    }
                } else {
                    *child_ptr.add(pending_gt_idx) = cur_node as i16;
                    pending_gt_idx = Self::left_child_idx(cur_node);
                    cur_node = *child_ptr.add(pending_gt_idx) as i32;
                    best_gt_len = len;
                    if best_lt_len < len {
                        len = best_lt_len;
                    }
                }

                depth_remaining -= 1;
                if cur_node <= cutoff || depth_remaining == 0 {
                    *child_ptr.add(pending_lt_idx) = MATCHFINDER_INITVAL;
                    *child_ptr.add(pending_gt_idx) = MATCHFINDER_INITVAL;
                    return match_count;
                }
            }
        } // unsafe
    }
}