compcol 0.6.5

A no_std collection of compression algorithms behind a uniform streaming trait, gated per-algorithm by Cargo features.
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
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
//! Iterative, statistics-driven optimal LZ77 parse for the brotli
//! encoder (a zopfli-style forward dynamic program).
//!
//! The greedy parse in `mod.rs` picks one match per position. This module
//! instead computes a least-cost command sequence for a whole meta-block
//! via forward DP, where the per-command bit cost comes from a
//! [`CostModel`]. The model is rebuilt from the *actual* command / literal
//! / distance histograms of the previous pass and the DP is re-run, so the
//! second pass optimises against the data's real statistics — the same
//! distance-reuse feedback loop that gives reference brotli q10/q11 its
//! edge over a single greedy pass.
//!
//! The output is a `Vec<Command>` in exactly the same shape the greedy
//! `lz77_to_commands` produces, so `plan_commands`, the ring-buffer
//! tracking, and the meta-block emitter are all unchanged and the stream
//! stays spec-valid. The chosen commands are re-planned by `plan_commands`
//! against the real distance ring afterwards, so an imperfect ring
//! approximation in the DP costs only a little ratio, never correctness.

use alloc::vec;
use alloc::vec::Vec;

use super::encoder_dict::{self, DictIndex, IdTransform};
use super::encoder_iac::{
    COPY_EXTRA, INS_EXTRA, copy_to_code, distance_to_normal_code, ic_command_sym, insert_to_code,
};
use super::encoder_lz77::{FinderParams, MAX_MATCH, MIN_MATCH, MatchFinder, match_len_at};
use super::{Command, CopyKind, DistRing, dictionary};

/// Per-symbol bit-cost model derived from a histogram. `cost[s]` is the
/// estimated code length (in bits) of symbol `s`.
struct SymCost {
    cost: Vec<f32>,
    /// Cost charged to a symbol that never appeared in the histogram.
    missing: f32,
}

impl SymCost {
    /// Shannon-style cost: `log2(total) - log2(count[s])`, floored at 1
    /// bit (a present symbol cannot be coded in less). Absent symbols get
    /// `log2(total) + 2` so the parse still considers them but pays a
    /// realistic penalty.
    fn from_hist(hist: &[u32]) -> Self {
        let total: u64 = hist.iter().map(|&h| h as u64).sum();
        let len = hist.len();
        if total == 0 {
            let flat = (len as f32).max(2.0).log2();
            return Self {
                cost: vec![flat; len],
                missing: flat,
            };
        }
        let log2_total = (total as f32).log2();
        let mut cost = vec![0.0f32; len];
        for (i, &h) in hist.iter().enumerate() {
            cost[i] = if h == 0 {
                log2_total + 2.0
            } else {
                let c = log2_total - (h as f32).log2();
                if c < 1.0 { 1.0 } else { c }
            };
        }
        Self {
            cost,
            missing: log2_total + 2.0,
        }
    }

    #[inline]
    fn get(&self, sym: usize) -> f32 {
        self.cost.get(sym).copied().unwrap_or(self.missing)
    }
}

/// Bit-cost model for one DP pass.
struct CostModel {
    lit: SymCost,
    cmd: SymCost,
    dist: SymCost,
}

impl CostModel {
    /// Seed an estimate before any commands have been chosen: literal
    /// costs from the raw byte histogram, command / distance costs flat
    /// (≈ reference brotli's `FastLog2(11)` / `FastLog2(20)` seeds).
    fn estimate(payload: &[u8]) -> Self {
        let mut lit_hist = [0u32; 256];
        for &b in payload {
            lit_hist[b as usize] += 1;
        }
        Self {
            lit: SymCost::from_hist(&lit_hist),
            cmd: SymCost {
                cost: vec![(11.0f32).log2(); 704],
                missing: (11.0f32).log2(),
            },
            dist: SymCost {
                cost: vec![(20.0f32).log2(); 64],
                missing: (20.0f32).log2(),
            },
        }
    }

    /// Build a model from the histograms of a previous pass's commands.
    fn from_hist(lit_hist: &[u32; 256], cmd_hist: &[u32; 704], dist_hist: &[u32; 64]) -> Self {
        Self {
            lit: SymCost::from_hist(lit_hist),
            cmd: SymCost::from_hist(cmd_hist),
            dist: SymCost::from_hist(dist_hist),
        }
    }
}

/// A DP node: the optimal way to have encoded `payload[0..pos]`, where
/// `pos` is the node's index. Literal-only progress sets `copy_len == 0`
/// and accumulates `insert_len`; a copy resets the run.
#[derive(Clone, Copy)]
struct Node {
    /// Minimal cost in bits to reach this byte position.
    cost: f32,
    /// Insert-run length of the command that ends here (literals consumed
    /// immediately before the copy, or the running literal count).
    insert_len: u32,
    /// Bytes consumed by the copy (0 for a literal step / stream start).
    copy_len: u32,
    /// Back-distance for a normal copy. For a dictionary ref this is the
    /// synthesised dict distance (used only for the histogram/plan).
    dist: u32,
    dict_word_idx: u32,
    dict_tr_id: u8,
    /// Dictionary word length placed in the copy field.
    dict_word_len: u8,
    is_dict: bool,
}

impl Node {
    const INF: Node = Node {
        cost: f32::INFINITY,
        insert_len: 0,
        copy_len: 0,
        dist: 0,
        dict_word_idx: 0,
        dict_tr_id: 0,
        dict_word_len: 0,
        is_dict: false,
    };
}

/// Command bit cost (everything except the literal run, already priced
/// incrementally): IC symbol + insert/copy extra bits + distance symbol +
/// distance extra bits.
#[inline]
fn command_cost(
    model: &CostModel,
    insert_len: u32,
    copy_len: u32,
    dcode: u32,
    dextra_bits: u32,
) -> f32 {
    let (ins_code, _, _) = insert_to_code(insert_len);
    let (copy_code, _, _) = copy_to_code(copy_len);
    let ic_sym = ic_command_sym(ins_code, copy_code, false);
    model.cmd.get(ic_sym as usize)
        + INS_EXTRA[ins_code as usize] as f32
        + COPY_EXTRA[copy_code as usize] as f32
        + model.dist.get(dcode as usize)
        + dextra_bits as f32
}

/// Command bit cost for a copy reusing a recent distance (short code, no
/// extra bits). `short_code` is the 0..=15 ring code.
#[inline]
fn repeat_command_cost(model: &CostModel, insert_len: u32, copy_len: u32, short_code: u32) -> f32 {
    let (ins_code, _, _) = insert_to_code(insert_len);
    let (copy_code, _, _) = copy_to_code(copy_len);
    let ic_sym = ic_command_sym(ins_code, copy_code, false);
    model.cmd.get(ic_sym as usize)
        + INS_EXTRA[ins_code as usize] as f32
        + COPY_EXTRA[copy_code as usize] as f32
        + model.dist.get(short_code as usize)
}

/// Recover the four most-recent distances reaching `pos` (decoder ring
/// order; `out[0]` most recent) by following the chosen back-pointers.
/// Dictionary refs and literal-only steps do not push the ring.
fn dist_ring_at(nodes: &[Node], mut pos: usize, out: &mut [u32; 4]) {
    *out = [0; 4];
    let mut filled = 0usize;
    let mut guard = 0;
    while pos > 0 && filled < 4 && guard < 256 {
        let n = nodes[pos];
        if n.copy_len == 0 && n.insert_len == 0 {
            break;
        }
        let back = n.copy_len as usize + n.insert_len as usize;
        if !n.is_dict && n.copy_len != 0 && n.dist != 0 {
            // Skip duplicates of the most recent (code-0 reuse does not
            // push a fresh ring entry).
            if filled == 0 || out[filled - 1] != n.dist {
                out[filled] = n.dist;
                filled += 1;
            }
        }
        if back == 0 || back > pos {
            break;
        }
        pos -= back;
        guard += 1;
    }
}

/// Run the iterative optimal parse and append the chosen commands to
/// `cmds`. On any internal inconsistency `cmds` is cleared so the caller
/// can fall back to the greedy parse.
#[allow(clippy::too_many_arguments)]
pub(crate) fn optimal_parse(
    payload: &[u8],
    mf: &mut MatchFinder,
    dict_index: Option<&DictIndex>,
    id_transforms: Option<&[IdTransform]>,
    prev_total_out: u64,
    ring_start: DistRing,
    iterations: u32,
    finder: FinderParams,
    cmds: &mut Vec<Command>,
    insert_pool: &mut Vec<Vec<u8>>,
) {
    let n = payload.len();
    if n == 0 {
        return;
    }

    let use_dict = dict_index.is_some() && id_transforms.is_some();

    // Populate the hash chains, then precompute every position's match
    // candidates *once*. The candidates don't change between passes (only
    // the cost model does), so caching them makes the extra iterations
    // nearly free — the per-pass work is just the DP relaxation.
    mf.reset();
    for pos in 0..n {
        mf.insert(payload, pos);
    }
    let cache = MatchCache::build(
        payload,
        mf,
        dict_index,
        id_transforms,
        prev_total_out,
        use_dict,
        finder,
    );

    let mut model = CostModel::estimate(payload);
    let mut nodes: Vec<Node> = vec![Node::INF; n + 1];

    let passes = iterations.max(1);
    for iter in 0..passes {
        forward_dp(payload, &cache, ring_start, &model, &mut nodes);
        if !nodes[n].cost.is_finite() {
            cmds.clear();
            return;
        }
        if iter + 1 == passes {
            break;
        }
        let (lit_hist, cmd_hist, dist_hist) = histogram_path(payload, &nodes, ring_start);
        model = CostModel::from_hist(&lit_hist, &cmd_hist, &dist_hist);
    }

    emit_path(payload, &nodes, cmds, insert_pool);
}

/// A dictionary-match candidate at some position.
#[derive(Clone, Copy)]
struct DictCand {
    distance: u32,
    dcode: u32,
    dextra_bits: u32,
    emit_len: u32,
    word_idx: u32,
    tr_id: u8,
    word_len: u8,
}

/// Per-position precomputed match candidates, shared across DP passes.
struct MatchCache {
    /// Flat list of `(len, dist)` explicit-match candidates.
    cand: Vec<(u32, u32)>,
    /// `cand_off[p]..cand_off[p+1]` is position `p`'s candidate slice.
    cand_off: Vec<u32>,
    /// Precomputed `(dcode, dextra_bits)` for each explicit candidate,
    /// aligned with `cand`.
    cand_dcode: Vec<(u32, u32)>,
    /// One optional dictionary candidate per position.
    dict: Vec<Option<DictCand>>,
}

impl MatchCache {
    fn build(
        payload: &[u8],
        mf: &MatchFinder,
        dict_index: Option<&DictIndex>,
        id_transforms: Option<&[IdTransform]>,
        prev_total_out: u64,
        use_dict: bool,
        finder: FinderParams,
    ) -> Self {
        let n = payload.len();
        let mut cand: Vec<(u32, u32)> = Vec::with_capacity(n);
        let mut cand_dcode: Vec<(u32, u32)> = Vec::with_capacity(n);
        let mut cand_off: Vec<u32> = Vec::with_capacity(n + 1);
        let mut dict: Vec<Option<DictCand>> = Vec::with_capacity(n);

        let mut buf = [(0u32, 0u32); 8];
        for p in 0..n {
            cand_off.push(cand.len() as u32);
            if p + MIN_MATCH <= n {
                let cnt = mf.find_matches(payload, p, finder, &mut buf);
                for &(clen, cdist) in &buf[..cnt] {
                    if let Some((dcode, ndb, _)) = distance_to_normal_code(cdist) {
                        cand.push((clen, cdist));
                        cand_dcode.push((dcode, ndb));
                    }
                }
            }
            dict.push(dict_candidate(
                payload,
                dict_index,
                id_transforms,
                prev_total_out,
                use_dict,
                p,
            ));
        }
        cand_off.push(cand.len() as u32);
        Self {
            cand,
            cand_off,
            cand_dcode,
            dict,
        }
    }

    #[inline]
    fn explicit(&self, p: usize) -> ExplicitCands<'_> {
        let lo = self.cand_off[p] as usize;
        let hi = self.cand_off[p + 1] as usize;
        (&self.cand[lo..hi], &self.cand_dcode[lo..hi])
    }
}

/// `(len, dist)` candidates paired with their precomputed
/// `(dcode, dist_extra_bits)`.
type ExplicitCands<'a> = (&'a [(u32, u32)], &'a [(u32, u32)]);

/// Compute the single dictionary-match candidate at position `p`, if any.
fn dict_candidate(
    payload: &[u8],
    dict_index: Option<&DictIndex>,
    id_transforms: Option<&[IdTransform]>,
    prev_total_out: u64,
    use_dict: bool,
    p: usize,
) -> Option<DictCand> {
    let n = payload.len();
    if !use_dict || p + 4 > n {
        return None;
    }
    let dm = encoder_dict::find_dict_match(dict_index?, id_transforms?, payload, p, 4)?;
    let total_out_at_pos: u64 = prev_total_out + p as u64;
    let max_dist: u64 = core::cmp::min((1u64 << 16) - 16, total_out_at_pos);
    let wl = dm.word_len as usize;
    let nwords_bits = dictionary::SIZE_BITS_BY_LENGTH[wl] as u32;
    let off = (dm.word_idx as u64) | ((dm.transform_id as u64) << nwords_bits);
    let distance = max_dist + 1 + off;
    if distance == 0 || distance > u32::MAX as u64 {
        return None;
    }
    let (dcode, ndb, _) = distance_to_normal_code(distance as u32)?;
    let emit_len = dm.emit_len as usize;
    if p + emit_len > n {
        return None;
    }
    Some(DictCand {
        distance: distance as u32,
        dcode,
        dextra_bits: ndb,
        emit_len: emit_len as u32,
        word_idx: dm.word_idx,
        tr_id: dm.transform_id,
        word_len: dm.word_len,
    })
}

/// One forward DP pass: fill `nodes[1..=n]` with least-cost arrivals,
/// using the precomputed match cache.
fn forward_dp(
    payload: &[u8],
    cache: &MatchCache,
    ring_start: DistRing,
    model: &CostModel,
    nodes: &mut [Node],
) {
    let n = payload.len();
    for node in nodes.iter_mut() {
        *node = Node::INF;
    }
    nodes[0] = Node {
        cost: 0.0,
        ..Node::INF
    };

    let mut ring4 = [0u32; 4];
    for p in 0..n {
        let here = nodes[p];
        if !here.cost.is_finite() {
            continue;
        }

        // The running literal-run length entering position `p` is the
        // accumulated insert for the *next* command. A copy-end node
        // restarts the run at 0; a literal-step node carries its count.
        let run_in = if here.copy_len == 0 {
            here.insert_len
        } else {
            0
        };

        // (a) Literal step: extend the running insert run by one byte. The
        //     per-literal cost is added incrementally so a copy starting
        //     later pays only its command overhead.
        {
            let lit_c = here.cost + model.lit.get(payload[p] as usize);
            let nx = &mut nodes[p + 1];
            if lit_c < nx.cost {
                *nx = Node {
                    cost: lit_c,
                    insert_len: run_in.wrapping_add(1),
                    copy_len: 0,
                    dist: 0,
                    dict_word_idx: 0,
                    dict_tr_id: 0,
                    dict_word_len: 0,
                    is_dict: false,
                };
            }
        }

        // The insert run for a copy starting here is whatever literals we
        // have accumulated; its bits are already in `here.cost`.
        let insert_len = run_in;
        let copy_start_cost = here.cost;

        // (b) Copy transitions.
        if p + MIN_MATCH <= n {
            dist_ring_at(nodes, p, &mut ring4);
            let seed = ring_start;
            let ring_get = |k: usize| -> u32 {
                if ring4[k] != 0 {
                    ring4[k]
                } else {
                    let v = seed.nth_last((k + 1) as u32);
                    if v > 0 { v as u32 } else { 0 }
                }
            };

            // Repeat-distance candidates: cheap short codes 0..=3. We try
            // every length from MIN_MATCH up to the match length so the DP
            // can break a long copy when a better-aligned match starts a
            // few bytes in (the key zopfli win over greedy).
            for k in 0..4usize {
                let d = ring_get(k);
                if d == 0 || d as usize > p {
                    continue;
                }
                let rl = match_len_at(payload, p, d as usize);
                if rl < MIN_MATCH {
                    continue;
                }
                let maxl = rl.min(MAX_MATCH).min(n - p);
                let mut len = MIN_MATCH;
                while len <= maxl {
                    let cost = copy_start_cost
                        + repeat_command_cost(model, insert_len, len as u32, k as u32);
                    relax(nodes, p + len, cost, insert_len, len as u32, d, None);
                    len += 1;
                }
            }

            // Explicit chain matches (precomputed). Candidates have
            // strictly increasing length, each at the closest distance
            // achieving that length. For candidate `j` spanning lengths
            // `(prev_len, len_j]`, that distance is the cheapest available
            // for every length in the band, so we relax all of them.
            //
            // If an explicit distance coincides with a recent ring
            // distance it can be coded as a cheap short code instead of a
            // full distance symbol + extra bits — price it as the cheaper
            // of the two so the DP prefers ring-reusing matches.
            let (cands, dcodes) = cache.explicit(p);
            let mut prev_len = MIN_MATCH - 1;
            for (&(clen, cdist), &(dcode, ndb)) in cands.iter().zip(dcodes.iter()) {
                let maxl = (clen as usize).min(MAX_MATCH).min(n - p);
                if maxl <= prev_len {
                    continue;
                }
                let short = ring_short_code(&ring4, &seed, cdist);
                let mut l = prev_len + 1;
                while l <= maxl {
                    let full = command_cost(model, insert_len, l as u32, dcode, ndb);
                    let cost = match short {
                        Some(sc) => {
                            let rep = repeat_command_cost(model, insert_len, l as u32, sc);
                            copy_start_cost + full.min(rep)
                        }
                        None => copy_start_cost + full,
                    };
                    relax(nodes, p + l, cost, insert_len, l as u32, cdist, None);
                    l += 1;
                }
                prev_len = maxl;
            }
        }

        // (c) Dictionary reference (precomputed).
        if let Some(dc) = cache.dict[p] {
            let dest = p + dc.emit_len as usize;
            let cost = copy_start_cost
                + command_cost(
                    model,
                    insert_len,
                    dc.word_len as u32,
                    dc.dcode,
                    dc.dextra_bits,
                );
            relax(
                nodes,
                dest,
                cost,
                insert_len,
                dc.emit_len,
                dc.distance,
                Some((dc.word_idx, dc.tr_id, dc.word_len)),
            );
        }
    }
}

/// Return the short distance code (0..=15) for `dist` given the recent
/// ring (reconstructed `ring4`, falling back to the block-start `seed`),
/// or `None` if `dist` needs a full distance symbol. Mirrors the subset
/// of `DistRing::try_short_code` the DP relies on.
#[inline]
fn ring_short_code(ring4: &[u32; 4], seed: &DistRing, dist: u32) -> Option<u32> {
    let slot = |k: usize| -> i32 {
        if ring4[k] != 0 {
            ring4[k] as i32
        } else {
            seed.nth_last((k + 1) as u32)
        }
    };
    let d = dist as i32;
    let last = slot(0);
    let last2 = slot(1);
    if d == last {
        return Some(0);
    }
    if d == last2 {
        return Some(1);
    }
    if d == slot(2) {
        return Some(2);
    }
    if d == slot(3) {
        return Some(3);
    }
    if d > 0 {
        if d == last - 1 {
            return Some(4);
        }
        if d == last + 1 {
            return Some(5);
        }
        if d == last - 2 {
            return Some(6);
        }
        if d == last + 2 {
            return Some(7);
        }
        if d == last - 3 {
            return Some(8);
        }
        if d == last + 3 {
            return Some(9);
        }
        if d == last2 - 1 {
            return Some(10);
        }
        if d == last2 + 1 {
            return Some(11);
        }
        if d == last2 - 2 {
            return Some(12);
        }
        if d == last2 + 2 {
            return Some(13);
        }
        if d == last2 - 3 {
            return Some(14);
        }
        if d == last2 + 3 {
            return Some(15);
        }
    }
    None
}

/// Relax a copy transition landing at `dest`.
#[inline]
fn relax(
    nodes: &mut [Node],
    dest: usize,
    cost: f32,
    insert_len: u32,
    copy_len: u32,
    dist: u32,
    dict: Option<(u32, u8, u8)>,
) {
    let nd = &mut nodes[dest];
    if cost < nd.cost {
        match dict {
            Some((word_idx, tr_id, word_len)) => {
                *nd = Node {
                    cost,
                    insert_len,
                    copy_len,
                    dist,
                    dict_word_idx: word_idx,
                    dict_tr_id: tr_id,
                    dict_word_len: word_len,
                    is_dict: true,
                };
            }
            None => {
                *nd = Node {
                    cost,
                    insert_len,
                    copy_len,
                    dist,
                    dict_word_idx: 0,
                    dict_tr_id: 0,
                    dict_word_len: 0,
                    is_dict: false,
                };
            }
        }
    }
}

/// Walk the DP back-pointers to recover command boundaries: a list of
/// `(start, end, node)` in forward order.
fn collect_path(payload: &[u8], nodes: &[Node]) -> Option<Vec<(usize, usize, Node)>> {
    let n = payload.len();
    let mut bounds: Vec<(usize, usize, Node)> = Vec::new();
    let mut pos = n;
    let mut guard = 0usize;
    while pos > 0 {
        guard += 1;
        if guard > n + 4 {
            return None;
        }
        let node = nodes[pos];
        if !node.cost.is_finite() {
            return None;
        }
        let span = node.copy_len as usize + node.insert_len as usize;
        if span == 0 || span > pos {
            return None;
        }
        bounds.push((pos - span, pos, node));
        pos -= span;
    }
    bounds.reverse();
    Some(bounds)
}

/// Tally literal / command / distance histograms for the chosen path so
/// the next iteration can reprice. Resolves distances against the real
/// ring (mirroring `plan_commands`) to get accurate short-code stats.
fn histogram_path(
    payload: &[u8],
    nodes: &[Node],
    ring_start: DistRing,
) -> ([u32; 256], [u32; 704], [u32; 64]) {
    let mut lit_hist = [0u32; 256];
    let mut cmd_hist = [0u32; 704];
    let mut dist_hist = [0u32; 64];

    let bounds = match collect_path(payload, nodes) {
        Some(b) => b,
        None => return (lit_hist, cmd_hist, dist_hist),
    };

    let mut ring = ring_start;
    for (start, _end, node) in bounds {
        let ins = node.insert_len as usize;
        let copy_start = start + ins;
        for &b in &payload[start..copy_start] {
            lit_hist[b as usize] += 1;
        }
        if node.copy_len == 0 {
            let (ins_code, _, _) = insert_to_code(ins as u32);
            cmd_hist[ic_command_sym(ins_code, 0, false) as usize] += 1;
            continue;
        }
        let copy_field = if node.is_dict {
            node.dict_word_len as u32
        } else {
            node.copy_len
        };
        let (ins_code, _, _) = insert_to_code(ins as u32);
        let (copy_code, _, _) = copy_to_code(copy_field);
        cmd_hist[ic_command_sym(ins_code, copy_code, false) as usize] += 1;

        if node.is_dict {
            if let Some((dcode, _, _)) = distance_to_normal_code(node.dist) {
                dist_hist[dcode as usize] += 1;
            }
        } else {
            let d = node.dist;
            if let Some(short) = ring.try_short_code(d) {
                dist_hist[short as usize] += 1;
                if short != 0 {
                    ring.push(d as i32);
                }
            } else if let Some((dcode, _, _)) = distance_to_normal_code(d) {
                dist_hist[dcode as usize] += 1;
                ring.push(d as i32);
            }
        }
    }
    (lit_hist, cmd_hist, dist_hist)
}

/// Materialise the chosen path into `Command`s appended to `cmds`.
fn emit_path(
    payload: &[u8],
    nodes: &[Node],
    cmds: &mut Vec<Command>,
    insert_pool: &mut Vec<Vec<u8>>,
) {
    let bounds = match collect_path(payload, nodes) {
        Some(b) => b,
        None => {
            cmds.clear();
            return;
        }
    };

    for (start, _end, node) in bounds {
        let ins = node.insert_len as usize;
        let copy_start = start + ins;
        let mut buf = insert_pool.pop().unwrap_or_default();
        buf.clear();
        buf.extend_from_slice(&payload[start..copy_start]);
        if node.copy_len == 0 {
            cmds.push(Command {
                insert: buf,
                copy_len: 0,
                kind: CopyKind::None,
            });
        } else if node.is_dict {
            cmds.push(Command {
                insert: buf,
                copy_len: node.dict_word_len as u32,
                kind: CopyKind::Dict {
                    word_idx: node.dict_word_idx,
                    transform_id: node.dict_tr_id,
                    emit_len: node.copy_len,
                },
            });
        } else {
            cmds.push(Command {
                insert: buf,
                copy_len: node.copy_len,
                kind: CopyKind::Backref {
                    distance: node.dist,
                },
            });
        }
    }
}