mbrotli 0.4.1

Fast Brotli (RFC 7932 and RFC 9841) compression and decompression in safe Rust; encoding is byte-identical to Google's reference encoder
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
//! Merging histograms that are similar enough to share a prefix code.
//!
//! Ports `c/enc/cluster.c` and `c/enc/cluster_inc.h` from the pinned reference
//! (`google/brotli` v1.2.0, commit `028fb5a`).
//!
//! A meta-block may gather hundreds of histograms — one per block type and
//! context — but the format allows at most two hundred and fifty-six, and each
//! one costs a whole prefix code to store. Clustering repeatedly merges the
//! pair whose combination saves the most bits, until no merge pays.
//!
//! The candidate pairs are kept in a bounded array with the best one first,
//! which the reference calls a heap and treats as one only at that first
//! position. Reproducing that — including which pair is displaced when the
//! array is full — is what keeps the merge order, and therefore the context
//! map, identical.

use alloc::vec::Vec;

use crate::shared::bit_cost::population_cost;
use crate::shared::fast_log::fast_log2;
use crate::shared::histogram::Histogram;

/// A candidate merge and what it would cost (`HistogramPair`).
#[derive(Copy, Clone, Debug, Default)]
pub(crate) struct HistogramPair {
    /// Lower index of the pair.
    idx1: u32,
    /// Higher index of the pair.
    idx2: u32,
    /// Cost of storing the two histograms combined.
    cost_combo: f64,
    /// Change in total cost from merging: negative means the merge pays.
    cost_diff: f64,
}

/// Returns whether `left` is the worse candidate of the two.
///
/// Mirrors `HistogramPairIsLess`: a larger cost difference is worse, and ties
/// are broken on the span between the indices, which keeps the order total.
fn pair_is_less(left: &HistogramPair, right: &HistogramPair) -> bool {
    if left.cost_diff != right.cost_diff {
        return left.cost_diff > right.cost_diff;
    }
    (left.idx2 - left.idx1) > (right.idx2 - right.idx1)
}

/// Returns how much the context map shrinks when two clusters merge.
///
/// Mirrors `ClusterCostDiff`: the entropy of the cluster-size distribution
/// before the merge, minus after.
fn cluster_cost_diff(size_a: usize, size_b: usize) -> f64 {
    let size_c = size_a + size_b;
    size_a as f64 * fast_log2(size_a) + size_b as f64 * fast_log2(size_b)
        - size_c as f64 * fast_log2(size_c)
}

/// Scratch storage the clusterer reuses across meta-blocks.
pub(crate) struct ClusterArena<const N: usize> {
    pairs: Vec<HistogramPair>,
    cluster_size: Vec<u32>,
    clusters: Vec<u32>,
    new_index: Vec<u32>,
    tmp: Histogram<N>,
    reindexed: Vec<Histogram<N>>,
}

impl<const N: usize> ClusterArena<N> {
    /// Counts all heap-backed scratch storage.
    pub(crate) fn retained_bytes(&self) -> usize {
        (self.pairs.capacity()) * size_of::<HistogramPair>()
            + (self.cluster_size.capacity() + self.clusters.capacity() + self.new_index.capacity())
                * size_of::<u32>()
            + (self.reindexed.capacity()) * size_of::<Histogram<N>>()
    }
}

impl<const N: usize> Default for ClusterArena<N> {
    /// Returns empty scratch storage.
    fn default() -> Self {
        Self {
            pairs: Vec::new(),
            cluster_size: Vec::new(),
            clusters: Vec::new(),
            new_index: Vec::new(),
            tmp: Histogram::default(),
            reindexed: Vec::new(),
        }
    }
}

/// Considers merging `idx1` with `idx2`, queueing it if it looks worthwhile.
///
/// Mirrors `BrotliCompareAndPushToQueue`. The combined cost is only computed
/// when it could beat what is already queued, which is what keeps the quadratic
/// first pass affordable.
#[expect(
    clippy::too_many_arguments,
    reason = "mirrors BrotliCompareAndPushToQueue, whose parameters are all needed"
)]
fn compare_and_push_to_queue<const N: usize>(
    out: &[Histogram<N>],
    tmp: &mut Histogram<N>,
    cluster_size: &[u32],
    data_size: usize,
    idx1: u32,
    idx2: u32,
    max_num_pairs: usize,
    pairs: &mut Vec<HistogramPair>,
) {
    if idx1 == idx2 {
        return;
    }
    let (idx1, idx2) = if idx2 < idx1 {
        (idx2, idx1)
    } else {
        (idx1, idx2)
    };

    let mut p = HistogramPair {
        idx1,
        idx2,
        cost_combo: 0.0,
        cost_diff: 0.0,
    };
    p.cost_diff = 0.5
        * cluster_cost_diff(
            cluster_size[idx1 as usize] as usize,
            cluster_size[idx2 as usize] as usize,
        );
    p.cost_diff -= out[idx1 as usize].bit_cost;
    p.cost_diff -= out[idx2 as usize].bit_cost;

    let is_good_pair = if out[idx1 as usize].total_count == 0 {
        p.cost_combo = out[idx2 as usize].bit_cost;
        true
    } else if out[idx2 as usize].total_count == 0 {
        p.cost_combo = out[idx1 as usize].bit_cost;
        true
    } else {
        let threshold = if pairs.is_empty() {
            1e99
        } else {
            pairs[0].cost_diff.max(0.0)
        };
        *tmp = out[idx1 as usize].clone();
        tmp.add_histogram(&out[idx2 as usize]);
        let cost_combo = population_cost(tmp, data_size);
        if cost_combo < threshold - p.cost_diff {
            p.cost_combo = cost_combo;
            true
        } else {
            false
        }
    };

    if !is_good_pair {
        return;
    }
    p.cost_diff += p.cost_combo;
    if !pairs.is_empty() && pair_is_less(&pairs[0], &p) {
        // The new pair is the best so far; the old best keeps its place in the
        // array if there is room.
        if pairs.len() < max_num_pairs {
            let front = pairs[0];
            pairs.push(front);
        }
        pairs[0] = p;
    } else if pairs.len() < max_num_pairs {
        pairs.push(p);
    }
}

/// Merges clusters until no merge pays or `max_clusters` remain.
///
/// Mirrors `BrotliHistogramCombine`. `symbols` maps every input histogram to
/// the cluster it belongs to and is rewritten as clusters merge; `clusters`
/// lists the surviving cluster indices. Returns how many clusters are left.
#[expect(
    clippy::too_many_arguments,
    reason = "mirrors BrotliHistogramCombine, whose parameters are all needed"
)]
fn histogram_combine<const N: usize>(
    out: &mut [Histogram<N>],
    tmp: &mut Histogram<N>,
    data_size: usize,
    cluster_size: &mut [u32],
    symbols: &mut [u32],
    clusters: &mut [u32],
    pairs: &mut Vec<HistogramPair>,
    mut num_clusters: usize,
    symbols_size: usize,
    max_clusters: usize,
    max_num_pairs: usize,
) -> usize {
    let mut cost_diff_threshold = 0.0f64;
    let mut min_cluster_size = 1usize;
    pairs.clear();

    for idx1 in 0..num_clusters {
        for idx2 in idx1 + 1..num_clusters {
            compare_and_push_to_queue(
                out,
                tmp,
                cluster_size,
                data_size,
                clusters[idx1],
                clusters[idx2],
                max_num_pairs,
                pairs,
            );
        }
    }

    while num_clusters > min_cluster_size {
        if pairs.is_empty() || pairs[0].cost_diff >= cost_diff_threshold {
            // Nothing left that pays: raise the bar so the loop ends.
            cost_diff_threshold = 1e99;
            min_cluster_size = max_clusters;
            continue;
        }
        let best_idx1 = pairs[0].idx1;
        let best_idx2 = pairs[0].idx2;
        let cost_combo = pairs[0].cost_combo;

        let absorbed = out[best_idx2 as usize].clone();
        out[best_idx1 as usize].add_histogram(&absorbed);
        out[best_idx1 as usize].bit_cost = cost_combo;
        cluster_size[best_idx1 as usize] += cluster_size[best_idx2 as usize];
        for symbol in symbols.iter_mut().take(symbols_size) {
            if *symbol == best_idx2 {
                *symbol = best_idx1;
            }
        }
        if let Some(at) = clusters[..num_clusters]
            .iter()
            .position(|&cluster| cluster == best_idx2)
        {
            clusters.copy_within(at + 1..num_clusters, at);
        }
        num_clusters -= 1;

        {
            // Drop every queued pair that touched either merged cluster,
            // keeping the best of what remains in front.
            let mut copy_to_idx = 0usize;
            for index in 0..pairs.len() {
                let p = pairs[index];
                if p.idx1 == best_idx1
                    || p.idx2 == best_idx1
                    || p.idx1 == best_idx2
                    || p.idx2 == best_idx2
                {
                    continue;
                }
                if pair_is_less(&pairs[0], &p) {
                    let front = pairs[0];
                    pairs[0] = p;
                    pairs[copy_to_idx] = front;
                } else {
                    pairs[copy_to_idx] = p;
                }
                copy_to_idx += 1;
            }
            pairs.truncate(copy_to_idx);
        }

        for &cluster in &clusters[..num_clusters] {
            compare_and_push_to_queue(
                out,
                tmp,
                cluster_size,
                data_size,
                best_idx1,
                cluster,
                max_num_pairs,
                pairs,
            );
        }
    }
    num_clusters
}

/// Returns what moving `histogram` into `candidate` would cost.
///
/// Mirrors `BrotliHistogramBitCostDistance`.
fn bit_cost_distance<const N: usize>(
    histogram: &Histogram<N>,
    candidate: &Histogram<N>,
    tmp: &mut Histogram<N>,
    data_size: usize,
) -> f64 {
    if histogram.total_count == 0 {
        return 0.0;
    }
    *tmp = histogram.clone();
    tmp.add_histogram(candidate);
    population_cost(tmp, data_size) - candidate.bit_cost
}

/// Assigns every input histogram to its cheapest cluster.
///
/// Mirrors `BrotliHistogramRemap`, including its preference for the cluster the
/// previous histogram chose when two are equally good — which is what makes the
/// resulting context map compress.
fn histogram_remap<const N: usize>(
    input: &[Histogram<N>],
    clusters: &[u32],
    num_clusters: usize,
    out: &mut [Histogram<N>],
    tmp: &mut Histogram<N>,
    data_size: usize,
    symbols: &mut [u32],
) {
    for index in 0..input.len() {
        let mut best_out = if index == 0 {
            symbols[0]
        } else {
            symbols[index - 1]
        };
        let mut best_bits =
            bit_cost_distance(&input[index], &out[best_out as usize], tmp, data_size);
        for &cluster in &clusters[..num_clusters] {
            let bits = bit_cost_distance(&input[index], &out[cluster as usize], tmp, data_size);
            if bits < best_bits {
                best_bits = bits;
                best_out = cluster;
            }
        }
        symbols[index] = best_out;
    }

    for &cluster in &clusters[..num_clusters] {
        out[cluster as usize].clear();
    }
    for index in 0..input.len() {
        let source = input[index].clone();
        out[symbols[index] as usize].add_histogram(&source);
    }
}

/// Renumbers the surviving histograms into a dense range.
///
/// Mirrors `BrotliHistogramReindex`: after remapping, `symbols` points at
/// scattered indices; this compacts them so the first use of each comes in
/// increasing order, which is what the context-map encoder expects. Returns how
/// many distinct histograms remain.
fn histogram_reindex<const N: usize>(
    out: &mut [Histogram<N>],
    symbols: &mut [u32],
    length: usize,
    new_index: &mut Vec<u32>,
    reindexed: &mut Vec<Histogram<N>>,
) -> usize {
    new_index.clear();
    new_index.resize(length, u32::MAX);
    let mut next_index = 0u32;
    for &symbol in symbols.iter().take(length) {
        let symbol = symbol as usize;
        if new_index[symbol] == u32::MAX {
            new_index[symbol] = next_index;
            next_index += 1;
        }
    }

    reindexed.clear();
    reindexed.resize(next_index as usize, Histogram::default());
    let mut written = 0u32;
    for slot in symbols.iter_mut().take(length) {
        let symbol = *slot as usize;
        if new_index[symbol] == written {
            reindexed[written as usize] = out[symbol].clone();
            written += 1;
        }
        *slot = new_index[symbol];
    }
    out[..reindexed.len()].clone_from_slice(reindexed);
    reindexed.len()
}

/// Histograms clustered in one batch of the first pass (`max_input_histograms`).
const MAX_INPUT_HISTOGRAMS: usize = 64;

/// Clusters `input` into at most `max_histograms` histograms.
///
/// Mirrors `BrotliClusterHistograms`. Returns the number of histograms written
/// to `out`; `symbols` is filled with the histogram each input maps to.
///
/// The first pass clusters in batches of sixty-four, which bounds the quadratic
/// pair search; the second lets everything that survived compete.
pub(crate) fn cluster_histograms<const N: usize>(
    input: &[Histogram<N>],
    data_size: usize,
    max_histograms: usize,
    arena: &mut ClusterArena<N>,
    out: &mut Vec<Histogram<N>>,
    symbols: &mut Vec<u32>,
) -> usize {
    let in_size = input.len();
    arena.cluster_size.clear();
    arena.cluster_size.resize(in_size, 1);
    arena.clusters.clear();
    arena.clusters.resize(in_size, 0);

    out.clear();
    out.reserve(in_size);
    symbols.clear();
    symbols.resize(in_size, 0);
    for (index, histogram) in input.iter().enumerate() {
        let mut copy = histogram.clone();
        copy.bit_cost = population_cost(histogram, data_size);
        out.push(copy);
        symbols[index] = index as u32;
    }

    let mut num_clusters = 0usize;
    let mut index = 0usize;
    while index < in_size {
        let num_to_combine = (in_size - index).min(MAX_INPUT_HISTOGRAMS);
        for j in 0..num_to_combine {
            arena.clusters[num_clusters + j] = (index + j) as u32;
        }
        let pairs_capacity = MAX_INPUT_HISTOGRAMS * MAX_INPUT_HISTOGRAMS / 2;
        let num_new_clusters = histogram_combine(
            out,
            &mut arena.tmp,
            data_size,
            &mut arena.cluster_size,
            &mut symbols[index..],
            &mut arena.clusters[num_clusters..],
            &mut arena.pairs,
            num_to_combine,
            num_to_combine,
            max_histograms,
            pairs_capacity,
        );
        num_clusters += num_new_clusters;
        index += MAX_INPUT_HISTOGRAMS;
    }

    {
        // The second pass caps the number of pairs it will keep: past that it
        // only tracks the single best.
        let max_num_pairs = (64 * num_clusters).min((num_clusters / 2) * num_clusters);
        num_clusters = histogram_combine(
            out,
            &mut arena.tmp,
            data_size,
            &mut arena.cluster_size,
            symbols,
            &mut arena.clusters,
            &mut arena.pairs,
            num_clusters,
            in_size,
            max_histograms,
            max_num_pairs,
        );
    }

    histogram_remap(
        input,
        &arena.clusters,
        num_clusters,
        out,
        &mut arena.tmp,
        data_size,
        symbols,
    );
    let final_size = histogram_reindex(
        out,
        symbols,
        in_size,
        &mut arena.new_index,
        &mut arena.reindexed,
    );
    out.truncate(final_size);
    final_size
}

/// Merges one batch of block histograms, as the block splitter needs it.
///
/// The splitter clusters its blocks itself rather than through
/// [`cluster_histograms`], because it works from block lengths rather than a
/// prepared array; this exposes the same merge step to it.
#[expect(
    clippy::too_many_arguments,
    reason = "mirrors BrotliHistogramCombine, whose parameters are all needed"
)]
pub(crate) fn combine_batch<const N: usize>(
    out: &mut [Histogram<N>],
    tmp: &mut Histogram<N>,
    data_size: usize,
    cluster_size: &mut [u32],
    symbols: &mut [u32],
    clusters: &mut [u32],
    pairs: &mut Vec<HistogramPair>,
    num_clusters: usize,
    symbols_size: usize,
    max_clusters: usize,
    max_num_pairs: usize,
) -> usize {
    histogram_combine(
        out,
        tmp,
        data_size,
        cluster_size,
        symbols,
        clusters,
        pairs,
        num_clusters,
        symbols_size,
        max_clusters,
        max_num_pairs,
    )
}

/// Exposes the move cost to the block splitter, which assigns blocks directly.
pub(crate) fn move_cost<const N: usize>(
    histogram: &Histogram<N>,
    candidate: &Histogram<N>,
    tmp: &mut Histogram<N>,
    data_size: usize,
) -> f64 {
    bit_cost_distance(histogram, candidate, tmp, data_size)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::shared::constants::NUM_LITERAL_SYMBOLS;
    use crate::shared::histogram::HistogramLiteral;

    /// Builds a histogram over the bytes of `data`.
    fn histogram(data: &[u8]) -> HistogramLiteral {
        let mut histogram = HistogramLiteral::default();
        for &byte in data {
            histogram.add(usize::from(byte));
        }
        histogram
    }

    /// Clusters `input`, returning the histograms and the symbol map.
    fn cluster(
        input: &[HistogramLiteral],
        max_histograms: usize,
    ) -> (Vec<HistogramLiteral>, Vec<u32>) {
        let mut arena = ClusterArena::default();
        let mut out = Vec::new();
        let mut symbols = Vec::new();
        cluster_histograms(
            input,
            NUM_LITERAL_SYMBOLS,
            max_histograms,
            &mut arena,
            &mut out,
            &mut symbols,
        );
        (out, symbols)
    }

    #[test]
    fn a_merge_that_pays_has_a_negative_cost_difference() {
        // Two clusters of one merging into one of two: the map gets shorter.
        assert!(cluster_cost_diff(1, 1) < 0.0);
        assert!(cluster_cost_diff(4, 4) < 0.0);
        // A cluster merging with nothing changes nothing.
        assert_eq!(cluster_cost_diff(0, 5), 0.0);
    }

    #[test]
    fn pair_ordering_breaks_ties_on_the_index_span() {
        let near = HistogramPair {
            idx1: 0,
            idx2: 1,
            cost_combo: 0.0,
            cost_diff: -5.0,
        };
        let far = HistogramPair {
            idx1: 0,
            idx2: 9,
            cost_combo: 0.0,
            cost_diff: -5.0,
        };
        // Equal costs: the wider span is the worse pair.
        assert!(pair_is_less(&far, &near));
        assert!(!pair_is_less(&near, &far));

        let dearer = HistogramPair {
            cost_diff: -1.0,
            ..near
        };
        assert!(pair_is_less(&dearer, &near));
    }

    #[test]
    fn identical_histograms_collapse_into_one() {
        let input = vec![histogram(b"aaaaaaaabbbbbbbb"); 6];
        let (out, symbols) = cluster(&input, 256);
        assert_eq!(out.len(), 1);
        assert_eq!(symbols, vec![0; 6]);
        // The survivor holds every count.
        assert_eq!(out[0].total_count, 16 * 6);
    }

    #[test]
    fn unrelated_histograms_stay_apart() {
        let input = vec![
            histogram(&[0u8; 400]),
            histogram(&[128u8; 400]),
            histogram(&[255u8; 400]),
        ];
        let (out, symbols) = cluster(&input, 256);
        assert_eq!(out.len(), 3, "distinct histograms were merged");
        let mut sorted = symbols.clone();
        sorted.sort_unstable();
        assert_eq!(sorted, vec![0, 1, 2]);
    }

    #[test]
    fn the_histogram_limit_is_respected() {
        // Sixteen genuinely different histograms, capped at four.
        let input: Vec<HistogramLiteral> = (0..16u8)
            .map(|index| histogram(&vec![index * 16; 500]))
            .collect();
        let (out, symbols) = cluster(&input, 4);
        assert!(out.len() <= 4, "clustering kept {} histograms", out.len());
        assert!(symbols.iter().all(|&symbol| (symbol as usize) < out.len()));
    }

    #[test]
    fn the_symbol_map_is_dense_and_first_use_ordered() {
        let input: Vec<HistogramLiteral> = (0..40u8)
            .map(|index| histogram(&vec![index * 6; 300]))
            .collect();
        let (out, symbols) = cluster(&input, 8);

        let mut next = 0u32;
        for &symbol in &symbols {
            assert!(symbol <= next, "symbol {symbol} appeared before {next}");
            if symbol == next {
                next += 1;
            }
        }
        assert_eq!(next as usize, out.len());
    }

    #[test]
    fn every_count_survives_clustering() {
        let input: Vec<HistogramLiteral> = (0..30u8)
            .map(|index| histogram(&vec![index; 100 + usize::from(index)]))
            .collect();
        let before: usize = input.iter().map(|histogram| histogram.total_count).sum();
        let (out, _) = cluster(&input, 6);
        let after: usize = out.iter().map(|histogram| histogram.total_count).sum();
        assert_eq!(before, after);
    }

    #[test]
    fn an_empty_histogram_merges_for_free() {
        let input = vec![
            histogram(&[7u8; 500]),
            HistogramLiteral::default(),
            histogram(&[7u8; 500]),
        ];
        let (out, _) = cluster(&input, 256);
        assert_eq!(out.len(), 1, "an empty histogram was kept apart");
    }

    #[test]
    fn clustering_a_single_histogram_is_the_identity() {
        let input = vec![histogram(b"only one")];
        let (out, symbols) = cluster(&input, 256);
        assert_eq!(out.len(), 1);
        assert_eq!(symbols, vec![0]);
        assert_eq!(out[0].total_count, 8);
    }

    #[test]
    fn the_arena_can_be_reused_without_changing_the_result() {
        let input: Vec<HistogramLiteral> = (0..20u8)
            .map(|index| histogram(&vec![index * 11; 400]))
            .collect();
        let other: Vec<HistogramLiteral> =
            (0..7u8).map(|index| histogram(&vec![index; 900])).collect();

        let (expected, expected_symbols) = cluster(&input, 5);

        let mut arena = ClusterArena::default();
        let mut out = Vec::new();
        let mut symbols = Vec::new();
        cluster_histograms(
            &other,
            NUM_LITERAL_SYMBOLS,
            3,
            &mut arena,
            &mut out,
            &mut symbols,
        );
        cluster_histograms(
            &input,
            NUM_LITERAL_SYMBOLS,
            5,
            &mut arena,
            &mut out,
            &mut symbols,
        );
        assert_eq!(symbols, expected_symbols);
        assert_eq!(out.len(), expected.len());
        for (left, right) in out.iter().zip(&expected) {
            assert_eq!(left.data, right.data);
            assert_eq!(left.total_count, right.total_count);
        }
    }

    #[test]
    fn more_than_one_batch_still_clusters() {
        // Past sixty-four histograms the first pass runs in batches, and the
        // second pass has to reconcile them.
        let input: Vec<HistogramLiteral> = (0..200u32)
            .map(|index| histogram(&vec![(index % 5) as u8; 300]))
            .collect();
        let (out, symbols) = cluster(&input, 256);
        assert!(out.len() <= 5, "clustering kept {} histograms", out.len());
        assert!(symbols.iter().all(|&symbol| (symbol as usize) < out.len()));
    }
}