• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

actioninja / refpack-rs / 20115958187

10 Dec 2025 10:56PM UTC coverage: 90.291% (-7.7%) from 97.965%
20115958187

push

github

actioninja
chore: Make longrunning CI not run in coverage!

1553 of 1720 relevant lines covered (90.29%)

359994.72 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

98.44
/src/data/compression/fastest.rs
1
////////////////////////////////////////////////////////////////////////////////
2
// This Source Code Form is subject to the terms of the Mozilla Public         /
3
// License, v. 2.0. If a copy of the MPL was not distributed with this         /
4
// file, You can obtain one at https://mozilla.org/MPL/2.0/.                   /
5
//                                                                             /
6
////////////////////////////////////////////////////////////////////////////////
7

8
use std::cmp::max;
9

10
use crate::data::compression::bytes_for_match;
11
use crate::data::compression::match_length::match_length;
12
use crate::data::compression::prefix_search::hash_table::PrefixTable;
13
use crate::data::compression::prefix_search::prefix;
14
use crate::data::control::{
15
    COPY_LITERAL_MAX,
16
    Command,
17
    Control,
18
    LITERAL_MAX,
19
    LONG_LENGTH_MAX,
20
    LONG_OFFSET_MAX,
21
    SHORT_OFFSET_MIN,
22
};
23

24
/// Reads from an incoming `Read` reader and compresses and encodes to
25
/// `Vec<Control>`
26
pub(crate) fn encode(input: &[u8]) -> Vec<Control> {
25,464✔
27
    let mut controls: Vec<Control> = vec![];
25,464✔
28
    let mut prefix_table = PrefixTable::new(input.len());
25,464✔
29

30
    let mut i = 0;
25,464✔
31
    let end = max(3, input.len()) - 3;
25,464✔
32
    let mut literal_block: Vec<u8> = Vec::with_capacity(LITERAL_MAX as usize);
25,464✔
33
    while i < end {
1,368,754✔
34
        let key = prefix(&input[i..]);
1,343,290✔
35

36
        // get the position of the prefix in the table (if it exists)
37
        let matched = prefix_table.insert(key, i as u32);
1,343,290✔
38

39
        let pair = matched.and_then(|matched| {
1,343,290✔
40
            let matched = matched as usize;
23,815✔
41
            let distance = i - matched;
23,815✔
42
            if distance > LONG_OFFSET_MAX as usize || distance < SHORT_OFFSET_MIN as usize {
23,815✔
43
                None
×
44
            } else {
45
                // find the longest common prefix
46
                let max_copy_len = LONG_LENGTH_MAX as usize;
23,815✔
47
                let match_length = match_length(input, i, matched, max_copy_len, 3);
23,815✔
48

49
                bytes_for_match(match_length, distance)
23,815✔
50
                    .and_then(|(bytes, _)| bytes.map(|_| (matched, match_length)))
23,815✔
51
            }
52
        });
23,815✔
53

54
        if let Some((found, match_length)) = pair {
1,343,290✔
55
            let distance = i - found;
23,813✔
56

57
            // If the current literal block is longer than the copy limit we need to split the block
58
            if literal_block.len() > COPY_LITERAL_MAX as usize {
23,813✔
59
                let split_point: usize = literal_block.len() - (literal_block.len() % 4);
130✔
60
                controls.push(Control::new_literal_block(&literal_block[..split_point]));
130✔
61
                let second_block = &literal_block[split_point..];
130✔
62
                controls.push(Control::new(
130✔
63
                    Command::new(
130✔
64
                        distance as u32,
130✔
65
                        match_length as u16,
130✔
66
                        second_block.len() as u8,
130✔
67
                    ),
130✔
68
                    second_block.to_vec(),
130✔
69
                ));
130✔
70
            } else {
23,683✔
71
                // If it's not, just push a new block directly
23,683✔
72
                controls.push(Control::new(
23,683✔
73
                    Command::new(
23,683✔
74
                        distance as u32,
23,683✔
75
                        match_length as u16,
23,683✔
76
                        literal_block.len() as u8,
23,683✔
77
                    ),
23,683✔
78
                    literal_block.clone(),
23,683✔
79
                ));
23,683✔
80
            }
23,683✔
81
            literal_block.clear();
23,813✔
82

83
            // here we would normally insert all intermediate prefixes into the prefix table
84
            // but for the sake of performance we skip this step
85

86
            i += match_length;
23,813✔
87
        } else {
88
            literal_block.push(input[i]);
1,319,477✔
89
            i += 1;
1,319,477✔
90
            // If it's reached the limit, push the block immediately and clear the running
91
            // block
92
            if literal_block.len() >= (LITERAL_MAX as usize) {
1,319,477✔
93
                controls.push(Control::new_literal_block(&literal_block));
1,149✔
94
                literal_block.clear();
1,149✔
95
            }
1,318,328✔
96
        }
97
    }
98
    // Add remaining literals if there are any
99
    if i < input.len() {
25,464✔
100
        literal_block.extend_from_slice(&input[i..]);
25,409✔
101
    }
25,409✔
102
    // Extremely similar to block up above, but with a different control type
103
    if literal_block.len() > 3 {
25,464✔
104
        let split_point: usize = literal_block.len() - (literal_block.len() % 4);
24,551✔
105
        controls.push(Control::new_literal_block(&literal_block[..split_point]));
24,551✔
106
        controls.push(Control::new_stop(&literal_block[split_point..]));
24,551✔
107
    } else {
24,551✔
108
        controls.push(Control::new_stop(&literal_block));
913✔
109
    }
913✔
110

111
    controls
25,464✔
112
}
25,464✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc