Skip to content

Commit

Permalink
Merge #22
Browse files Browse the repository at this point in the history
22: Do not allocate buffer size before necessary r=gendx a=dragly

This changes the way the LZCircularBuffer is allocated. Instead of allocating the full dict size immediately, the buffer is grown on demand. This helps when parsing smaller files with large dict sizes, especially when compiling to WebAssembly and running in browser where `alloc_zeroed` is slow.

Co-authored-by: Svenn-Arne Dragly <dragly@cognite.com>
  • Loading branch information
bors[bot] and dragly authored Mar 4, 2020
2 parents 9fff01b + 2b2e27f commit 2052b4c
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions src/decode/lzbuffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,12 +139,23 @@ where
info!("Dict size in LZ buffer: {}", dict_size);
Self {
stream,
buf: vec![0; dict_size],
buf: Vec::new(),
dict_size,
cursor: 0,
len: 0,
}
}

fn get(&self, index: usize) -> u8 {
*self.buf.get(index).unwrap_or(&0)
}

fn get_mut(&mut self, index: usize) -> &mut u8 {
if self.buf.len() < index + 1 {
self.buf.resize(index + 1, 0);
}
self.buf.get_mut(index).unwrap()
}
}

impl<'a, W> LZBuffer for LZCircularBuffer<'a, W>
Expand All @@ -160,7 +171,7 @@ where
if self.len == 0 {
lit
} else {
self.buf[(self.dict_size + self.cursor - 1) % self.dict_size]
self.get((self.dict_size + self.cursor - 1) % self.dict_size)
}
}

Expand All @@ -180,12 +191,12 @@ where
}

let offset = (self.dict_size + self.cursor - dist) % self.dict_size;
Ok(self.buf[offset])
Ok(self.get(offset))
}

// Append a literal
fn append_literal(&mut self, lit: u8) -> io::Result<()> {
self.buf[self.cursor] = lit;
*self.get_mut(self.cursor) = lit;
self.cursor += 1;
self.len += 1;

Expand Down Expand Up @@ -216,7 +227,7 @@ where

let mut offset = (self.dict_size + self.cursor - dist) % self.dict_size;
for _ in 0..len {
let x = self.buf[offset];
let x = self.get(offset);
self.append_literal(x)?;
offset += 1;
if offset == self.dict_size {
Expand Down

0 comments on commit 2052b4c

Please sign in to comment.