zstd-complete 0.1.0

A pure Rust Zstandard compressor and decompressor with bounded streaming I/O
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
//! Canonical Huffman table construction kernel.

use alloc::{vec, vec::Vec};
use core::convert::TryFrom;

use super::HuffmanCode;

const MAX_HUFFMAN_BITS: usize = 11;
const NODE_NONE: u16 = u16::MAX;
const RANK_NONE: usize = usize::MAX;

#[derive(Clone, Default)]
pub struct HuffmanBuildScratch {
    nodes: Vec<Node>,
}

impl HuffmanBuildScratch {
    #[doc(hidden)]
    #[cfg(test)]
    pub fn retained_node_capacity(&self) -> usize {
        self.nodes.capacity()
    }
}

#[cfg(test)]
pub struct BuiltHuffmanTable {
    pub codes: Vec<HuffmanCode>,
    pub max_num_bits: u8,
    pub weights: Vec<u8>,
}

pub struct DescribedHuffmanTable {
    pub codes: Vec<HuffmanCode>,
    pub max_num_bits: u8,
    pub table_description: Vec<u8>,
}

/// Builds C's canonical compression table and consumes it into the weight
/// representation used by `HUF_writeCTable_wksp()`.
///
/// Keeping the sort, tree merge, height redistribution, canonical-code pass,
/// and bits-to-weight pass in one generated-code unit avoids returning an
/// intermediate tree or rebuilding rank metadata in the parent crate.
#[cfg_attr(target_vendor = "apple", link_section = "__TEXT,__rz_hb0")]
#[cfg_attr(target_family = "windows", link_section = ".text$010.rz.hb0")]
#[cfg_attr(
    all(
        not(target_vendor = "apple"),
        not(target_family = "windows"),
        not(target_family = "wasm")
    ),
    link_section = ".text.sorted.010.ruzstd.huffman.build"
)]
#[cfg(test)]
pub fn build_huffman_table(
    counts: &[usize],
    max_bits: usize,
    scratch: &mut HuffmanBuildScratch,
) -> Option<BuiltHuffmanTable> {
    let (codes, max_num_bits) = build_codes(counts, max_bits, scratch)?;
    let weights = with_weights(&codes, max_num_bits, <[u8]>::to_vec);
    Some(BuiltHuffmanTable {
        codes,
        max_num_bits,
        weights,
    })
}

/// Builds the complete canonical table and immediately consumes its bit
/// lengths into the serialized table description. The callback boundary is
/// invoked once per new table and lets the parent reuse its existing exact FSE
/// weight serializer while the hot tree/rank/weight transaction remains in
/// this isolated codegen unit.
#[cfg_attr(target_vendor = "apple", link_section = "__TEXT,__rz_hb1")]
#[cfg_attr(target_family = "windows", link_section = ".text$011.rz.hb1")]
#[cfg_attr(
    all(
        not(target_vendor = "apple"),
        not(target_family = "windows"),
        not(target_family = "wasm")
    ),
    link_section = ".text.sorted.011.ruzstd.huffman.build_described"
)]
pub fn build_described_huffman_table(
    counts: &[usize],
    max_bits: usize,
    scratch: &mut HuffmanBuildScratch,
    describe_weights: fn(&[u8]) -> Vec<u8>,
) -> Option<DescribedHuffmanTable> {
    let (codes, max_num_bits) = build_codes(counts, max_bits, scratch)?;
    let table_description = with_weights(&codes, max_num_bits, describe_weights);
    Some(DescribedHuffmanTable {
        codes,
        max_num_bits,
        table_description,
    })
}

/// Builds the same complete described table while refilling caller-owned
/// output allocations. C alternates current and next entropy tables inside
/// the compression context; this entry point lets the Rust frame model the
/// same lifetime without embedding a fixed 2 KiB table in return values.
#[cfg_attr(target_vendor = "apple", link_section = "__TEXT,__rz_hb2")]
#[cfg_attr(target_family = "windows", link_section = ".text$012.rz.hb2")]
#[cfg_attr(
    all(
        not(target_vendor = "apple"),
        not(target_family = "windows"),
        not(target_family = "wasm")
    ),
    link_section = ".text.sorted.012.ruzstd.huffman.build_described_reusing"
)]
pub fn build_described_huffman_table_reusing(
    counts: &[usize],
    max_bits: usize,
    scratch: &mut HuffmanBuildScratch,
    mut codes: Vec<HuffmanCode>,
    mut table_description: Vec<u8>,
    describe_weights: fn(&[u8], &mut Vec<u8>),
) -> Option<DescribedHuffmanTable> {
    let max_num_bits = build_codes_reusing(counts, max_bits, scratch, &mut codes)?;
    with_weights(&codes, max_num_bits, |weights| {
        describe_weights(weights, &mut table_description)
    });
    Some(DescribedHuffmanTable {
        codes,
        max_num_bits,
        table_description,
    })
}

/// Builds the same caller-owned table while allowing its description callback
/// to borrow a caller-owned workspace. This keeps the generated Huffman
/// transaction independent of the parent's entropy implementation while
/// letting a compression context share C's sequential workspace lifetime.
#[cfg_attr(target_vendor = "apple", link_section = "__TEXT,__rz_hb3")]
#[cfg_attr(target_family = "windows", link_section = ".text$013.rz.hb3")]
#[cfg_attr(
    all(
        not(target_vendor = "apple"),
        not(target_family = "windows"),
        not(target_family = "wasm")
    ),
    link_section = ".text.sorted.013.ruzstd.huffman.build_described_reusing_context"
)]
pub fn build_described_huffman_table_reusing_with_context<Context>(
    counts: &[usize],
    max_bits: usize,
    scratch: &mut HuffmanBuildScratch,
    mut codes: Vec<HuffmanCode>,
    mut table_description: Vec<u8>,
    context: &mut Context,
    describe_weights: fn(&[u8], &mut Vec<u8>, &mut Context),
) -> Option<DescribedHuffmanTable> {
    let max_num_bits = build_codes_reusing(counts, max_bits, scratch, &mut codes)?;
    with_weights(&codes, max_num_bits, |weights| {
        describe_weights(weights, &mut table_description, context)
    });
    Some(DescribedHuffmanTable {
        codes,
        max_num_bits,
        table_description,
    })
}

fn build_codes_reusing(
    counts: &[usize],
    max_bits: usize,
    scratch: &mut HuffmanBuildScratch,
    codes: &mut Vec<HuffmanCode>,
) -> Option<u8> {
    if counts.len() > 256 {
        return None;
    }
    let max_bits = max_bits.clamp(1, MAX_HUFFMAN_BITS);
    if counts.iter().filter(|count| **count != 0).count() > 1usize << max_bits {
        return None;
    }
    let leaf_count = build_tree(counts, &mut scratch.nodes)?;
    let max_num_bits = limit_height(&mut scratch.nodes, leaf_count, max_bits)?;

    codes.clear();
    codes.resize(counts.len(), (0, 0));
    let mut counts_by_length = [0u16; 256];
    for node in &scratch.nodes[1..=leaf_count] {
        counts_by_length[usize::from(node.len)] += 1;
        codes[usize::from(node.symbol)].1 = node.len;
    }

    let mut values_by_length = [0u32; 256];
    let mut min_value = 0u32;
    for len in (1..=max_num_bits).rev() {
        values_by_length[len] = min_value;
        min_value += u32::from(counts_by_length[len]);
        min_value >>= 1;
    }
    for code in codes {
        let len = usize::from(code.1);
        if len != 0 {
            code.0 = values_by_length[len];
            values_by_length[len] += 1;
        }
    }

    u8::try_from(max_num_bits).ok()
}

fn build_codes(
    counts: &[usize],
    max_bits: usize,
    scratch: &mut HuffmanBuildScratch,
) -> Option<(Vec<HuffmanCode>, u8)> {
    if counts.len() > 256 {
        return None;
    }
    let max_bits = max_bits.clamp(1, MAX_HUFFMAN_BITS);
    if counts.iter().filter(|count| **count != 0).count() > 1usize << max_bits {
        return None;
    }
    let leaf_count = build_tree(counts, &mut scratch.nodes)?;
    let max_num_bits = limit_height(&mut scratch.nodes, leaf_count, max_bits)?;

    let mut codes = vec![(0, 0); counts.len()];
    let mut counts_by_length = [0u16; 256];
    for node in &scratch.nodes[1..=leaf_count] {
        counts_by_length[usize::from(node.len)] += 1;
        codes[usize::from(node.symbol)].1 = node.len;
    }

    let mut values_by_length = [0u32; 256];
    let mut min_value = 0u32;
    for len in (1..=max_num_bits).rev() {
        values_by_length[len] = min_value;
        min_value += u32::from(counts_by_length[len]);
        min_value >>= 1;
    }
    for code in &mut codes {
        let len = usize::from(code.1);
        if len != 0 {
            code.0 = values_by_length[len];
            values_by_length[len] += 1;
        }
    }

    let max_num_bits = u8::try_from(max_num_bits).ok()?;
    Some((codes, max_num_bits))
}

fn with_weights<R>(codes: &[HuffmanCode], max_num_bits: u8, consume: impl FnOnce(&[u8]) -> R) -> R {
    // This initialized fixed workspace is consumed before return. Its full
    // byte-indexed lookup mirrors C's bitsToWeight table without unsafe code.
    let mut bits_to_weight = [0u8; 256];
    for num_bits in 1..=max_num_bits {
        bits_to_weight[usize::from(num_bits)] = max_num_bits + 1 - num_bits;
    }
    let mut weights = [0u8; 256];
    for (weight, code) in weights.iter_mut().zip(codes) {
        *weight = bits_to_weight[usize::from(code.1)];
    }
    consume(&weights[..codes.len()])
}

fn build_tree(counts: &[usize], nodes: &mut Vec<Node>) -> Option<usize> {
    sort_leaves(counts, nodes)?;
    let leaf_count = nodes.len().saturating_sub(1);
    if leaf_count <= 1 {
        return None;
    }

    let first_parent = leaf_count + 1;
    let root = 2 * leaf_count - 1;
    nodes.resize(2 * leaf_count, Node::barrier());
    let mut smallest_leaf = leaf_count;
    let mut smallest_parent = first_parent;
    for parent in first_parent..=root {
        let left = pop_smallest(nodes, &mut smallest_leaf, &mut smallest_parent);
        let right = pop_smallest(nodes, &mut smallest_leaf, &mut smallest_parent);
        let parent_index = u16::try_from(parent).ok()?;
        nodes[left].parent = parent_index;
        nodes[right].parent = parent_index;
        nodes[parent].count = nodes[left].count.checked_add(nodes[right].count)?;
    }

    nodes[root].len = 0;
    for index in (first_parent..root).rev() {
        nodes[index].len = nodes[parent(nodes[index])].len + 1;
    }
    for index in 1..=leaf_count {
        nodes[index].len = nodes[parent(nodes[index])].len + 1;
    }
    Some(leaf_count)
}

fn limit_height(nodes: &mut [Node], leaf_count: usize, target_bits: usize) -> Option<usize> {
    let largest_bits = usize::from(nodes[leaf_count].len);
    if largest_bits <= target_bits {
        return Some(largest_bits);
    }

    let shift = largest_bits.checked_sub(target_bits)?;
    let base_cost = 1isize.checked_shl(u32::try_from(shift).ok()?)?;
    let mut total_cost = 0isize;
    let mut last_below_target = leaf_count;
    while usize::from(nodes[last_below_target].len) > target_bits {
        let len = usize::from(nodes[last_below_target].len);
        let rank_cost = 1isize.checked_shl(u32::try_from(largest_bits - len).ok()?)?;
        total_cost += base_cost - rank_cost;
        nodes[last_below_target].len = u8::try_from(target_bits).ok()?;
        last_below_target = last_below_target.checked_sub(1)?;
    }
    while usize::from(nodes[last_below_target].len) == target_bits {
        last_below_target = last_below_target.checked_sub(1)?;
    }

    total_cost >>= shift;
    if total_cost <= 0 {
        return None;
    }

    let mut rank_last = [RANK_NONE; MAX_HUFFMAN_BITS + 2];
    let mut current_bits = target_bits;
    for position in (1..=last_below_target).rev() {
        let len = usize::from(nodes[position].len);
        if len >= current_bits {
            continue;
        }
        current_bits = len;
        rank_last[target_bits - current_bits] = position;
    }

    while total_cost > 0 {
        let mut bits_to_decrease = highest_bit_set(total_cost as usize);
        for candidate_bits in (2..=bits_to_decrease).rev() {
            let high_position = rank_last[candidate_bits];
            let low_position = rank_last[candidate_bits - 1];
            if high_position == RANK_NONE {
                bits_to_decrease -= 1;
                continue;
            }
            if low_position == RANK_NONE {
                break;
            }
            if nodes[high_position].count <= nodes[low_position].count.saturating_mul(2) {
                break;
            }
            bits_to_decrease -= 1;
        }
        while bits_to_decrease <= target_bits && rank_last[bits_to_decrease] == RANK_NONE {
            bits_to_decrease += 1;
        }
        if bits_to_decrease > target_bits {
            return None;
        }

        total_cost -= 1isize << (bits_to_decrease - 1);
        let position = rank_last[bits_to_decrease];
        nodes[position].len += 1;
        if rank_last[bits_to_decrease - 1] == RANK_NONE {
            rank_last[bits_to_decrease - 1] = position;
        }
        rank_last[bits_to_decrease] = if position > 1
            && usize::from(nodes[position - 1].len) == target_bits - bits_to_decrease
        {
            position - 1
        } else {
            RANK_NONE
        };
    }

    while total_cost < 0 {
        if rank_last[1] == RANK_NONE {
            while usize::from(nodes[last_below_target].len) == target_bits {
                last_below_target = last_below_target.checked_sub(1)?;
            }
            let position = last_below_target + 1;
            nodes[position].len = nodes[position].len.checked_sub(1)?;
            rank_last[1] = position;
            total_cost += 1;
            continue;
        }
        let position = rank_last[1] + 1;
        if position > leaf_count {
            return None;
        }
        nodes[position].len = nodes[position].len.checked_sub(1)?;
        rank_last[1] = position;
        total_cost += 1;
    }
    Some(target_bits)
}

fn sort_leaves(counts: &[usize], nodes: &mut Vec<Node>) -> Option<()> {
    const TABLE_SIZE: usize = 192;
    const MAX_COUNT_LOG: usize = 32;
    const LOG_BUCKETS_BEGIN: usize = (TABLE_SIZE - 1) - MAX_COUNT_LOG - 1;
    const DISTINCT_CUTOFF: usize = LOG_BUCKETS_BEGIN + 7;

    #[derive(Clone, Copy, Default)]
    struct RankPosition {
        base: usize,
        current: usize,
    }

    fn rank_index(count: usize) -> usize {
        if count < DISTINCT_CUTOFF {
            count
        } else {
            high_bit(count) + LOG_BUCKETS_BEGIN
        }
    }

    let mut positions = [RankPosition::default(); TABLE_SIZE];
    let mut nonzero_count = 0usize;
    for &count in counts {
        nonzero_count += usize::from(count != 0);
        let rank = rank_index(count);
        if rank >= TABLE_SIZE - 1 {
            return None;
        }
        positions[rank].base += 1;
    }
    for rank in (1..TABLE_SIZE).rev() {
        positions[rank - 1].base += positions[rank].base;
        positions[rank - 1].current = positions[rank - 1].base;
    }

    nodes.clear();
    nodes.resize(nonzero_count + 1, Node::barrier());
    for (symbol, &count) in counts.iter().enumerate() {
        if count == 0 {
            continue;
        }
        let rank = rank_index(count) + 1;
        let position = positions[rank].current;
        positions[rank].current += 1;
        nodes[position + 1] = Node {
            count: u32::try_from(count).ok()?,
            symbol: u8::try_from(symbol).ok()?,
            parent: NODE_NONE,
            len: 0,
        };
    }
    for rank in &positions[DISTINCT_CUTOFF..TABLE_SIZE - 1] {
        let start = rank.base.min(nonzero_count) + 1;
        let end = rank.current.min(nonzero_count) + 1;
        if end.saturating_sub(start) > 1 {
            quick_sort(&mut nodes[start..end]);
        }
    }
    Some(())
}

fn quick_sort(nodes: &mut [Node]) {
    const INSERTION_THRESHOLD: usize = 8;
    if nodes.len() <= 1 {
        return;
    }
    if nodes.len() - 1 < INSERTION_THRESHOLD {
        insertion_sort(nodes);
        return;
    }
    let mut low = 0usize;
    let mut high = nodes.len() - 1;
    while low < high {
        let split = partition(nodes, low, high);
        if split - low < high - split {
            if low < split {
                quick_sort(&mut nodes[low..split]);
            }
            low = split + 1;
        } else {
            if split < high {
                quick_sort(&mut nodes[split + 1..=high]);
            }
            if split == 0 {
                break;
            }
            high = split - 1;
        }
    }
}

fn insertion_sort(nodes: &mut [Node]) {
    for index in 1..nodes.len() {
        let key = nodes[index];
        let mut position = index;
        while position > 0 && nodes[position - 1].count < key.count {
            nodes[position] = nodes[position - 1];
            position -= 1;
        }
        nodes[position] = key;
    }
}

fn partition(nodes: &mut [Node], low: usize, high: usize) -> usize {
    let pivot = nodes[high].count;
    let mut boundary = low;
    for index in low..high {
        if nodes[index].count > pivot {
            nodes.swap(boundary, index);
            boundary += 1;
        }
    }
    nodes.swap(boundary, high);
    boundary
}

fn pop_smallest(nodes: &[Node], leaf: &mut usize, parent: &mut usize) -> usize {
    if nodes[*leaf].count < nodes[*parent].count {
        let index = *leaf;
        *leaf -= 1;
        index
    } else {
        let index = *parent;
        *parent += 1;
        index
    }
}

fn parent(node: Node) -> usize {
    usize::from(node.parent)
}

fn high_bit(value: usize) -> usize {
    usize::BITS as usize - 1 - value.leading_zeros() as usize
}

fn highest_bit_set(value: usize) -> usize {
    usize::BITS as usize - value.leading_zeros() as usize
}

#[derive(Clone, Copy)]
struct Node {
    count: u32,
    parent: u16,
    symbol: u8,
    len: u8,
}

impl Node {
    const fn barrier() -> Self {
        Self {
            count: u32::MAX,
            parent: NODE_NONE,
            symbol: 0,
            len: 0,
        }
    }
}