Remove DunHuffman16 pattern Box allocation

main
kirbylife 2022-05-04 23:57:31 -05:00
parent ec8dd2ddee
commit 6e83cda4ad
1 changed files with 61 additions and 27 deletions

View File

@ -140,8 +140,8 @@ impl CodeLengthReader {
fn to_lit_and_dist(&self) -> Result<(DynHuffman16, DynHuffman16), &'static str> {
let num_lit = self.num_lit as usize;
let lit = DynHuffman16::new(&self.result[..num_lit])?;
let dist = DynHuffman16::new(&self.result[num_lit..])?;
let lit = DynHuffman16::new(&self.result[..num_lit], DynHuffmanType::LIT)?;
let dist = DynHuffman16::new(&self.result[num_lit..], DynHuffmanType::DIST)?;
Ok((lit, dist))
}
}
@ -152,15 +152,29 @@ struct Trie8bit {
children: [Option<Box<[u16; 16]>>; 16],
}
static mut DYNLITBUFFER: [u16; 256] = [0; 256];
static mut DYNDISTBUFFER: [u16; 256] = [0; 256];
enum DynHuffmanType {
LIT,
DIST,
}
#[derive(Debug)]
struct DynHuffman16 {
patterns: Box<[u16; 256]>,
patterns: &'static mut [u16; 256],
rest: Vec<Trie8bit>,
}
impl DynHuffman16 {
fn new(clens: &[u8]) -> Result<DynHuffman16, &'static str> {
let mut patterns = Box::new([0xffffu16; 256]);
fn new(clens: &[u8], ty: DynHuffmanType) -> Result<DynHuffman16, &'static str> {
let patterns = unsafe {
match ty {
DynHuffmanType::LIT => &mut DYNLITBUFFER,
DynHuffmanType::DIST => &mut DYNDISTBUFFER,
}
};
patterns.fill(0xffffu16);
let mut rest = Vec::new();
with_codes!(clens, 15 => u16, |i: u16, code: u16, bits: u8| -> _ {
let entry = i | (u16::from(bits) << 12);
@ -524,27 +538,47 @@ impl InflateStream {
}
1 => {
// Unwrap is safe because the data is valid.
let lit = DynHuffman16::new(&[
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, // 0-15
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, // 16-31
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, // 32-47
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, // 48-63
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, // 64-79
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, // 80-95
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, // 96-101
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, // 112-127
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, // 128-143
9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, // 144-159
9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, // 160-175
9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, // 176-191
9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, // 192-207
9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, // 208-223
9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, // 224-239
9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, // 240-255
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, // 256-271
7, 7, 7, 7, 7, 7, 7, 7, // 272-279
8, 8, 8, 8, 8, 8, 8, 8, // 280-287
])
let lit = DynHuffman16::new(
&[
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
8, // 0-15
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
8, // 16-31
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
8, // 32-47
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
8, // 48-63
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
8, // 64-79
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
8, // 80-95
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
8, // 96-101
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
8, // 112-127
8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8,
8, // 128-143
9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
9, // 144-159
9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
9, // 160-175
9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
9, // 176-191
9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
9, // 192-207
9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
9, // 208-223
9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
9, // 224-239
9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9,
9, // 240-255
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,
7, // 256-271
7, 7, 7, 7, 7, 7, 7, 7, // 272-279
8, 8, 8, 8, 8, 8, 8, 8, // 280-287
],
DynHuffmanType::LIT,
)
.unwrap();
#[rustfmt::skip]
@ -553,7 +587,7 @@ impl InflateStream {
5, 5, 5, 5, 5, 5, 5, 5,
5, 5, 5, 5, 5, 5, 5, 5,
5, 5, 5, 5, 5, 5, 5, 5
]).unwrap();
], DynHuffmanType::DIST).unwrap();
ok!(BlockDyn(lit, dist, 0))
}
2 => ok!(BlockDynHlit),