From c1e85d77dc80547ce814b42bc589e74d6a7fb11f Mon Sep 17 00:00:00 2001 From: Lol3rrr Date: Sun, 22 Sep 2024 18:12:31 +0200 Subject: [PATCH] Further perf improv attempts Reuse the buffer when decompressing the frames --- src/frame.rs | 19 +++++++++++++++++++ src/parser.rs | 8 +++----- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/src/frame.rs b/src/frame.rs index be35e93..9895aa9 100644 --- a/src/frame.rs +++ b/src/frame.rs @@ -39,6 +39,25 @@ impl<'b> Frame<'b> { Some(self.inner.as_ref()) } + pub fn decompress_with_buf<'s, 'buf>(&'s self, buf: &'b mut Vec) -> Result<&'buf [u8], ()> where 's: 'buf { + if !self.compressed { + return Ok(&self.inner); + } + + let uncompressed_len = snap::raw::decompress_len(&self.inner).map_err(|e| { + println!("Getting decompress len"); + () + })?; + buf.resize(uncompressed_len, 0); + + snap::raw::Decoder::new().decompress(&self.inner, buf.as_mut_slice()).map_err(|e| { + println!("Decompressing"); + () + })?; + + Ok(buf.as_slice()) + } + pub fn decompress(&mut self) -> Result<(), ()> { if !self.compressed { return Ok(()); diff --git a/src/parser.rs b/src/parser.rs index 4be5abb..43b7a08 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -107,11 +107,9 @@ where let mut entity_states = Vec::new(); - for mut frame in frames.into_iter() { - frame - .decompress() - .map_err(|e| FirstPassError::DecompressFrame)?; - let data = frame.data().ok_or(FirstPassError::NoDataFrame)?; + let mut buffer = Vec::new(); + for frame in frames.into_iter() { + let data = frame.decompress_with_buf(&mut buffer).map_err(|e| FirstPassError::DecompressFrame)?; match frame.cmd { DemoCommand::FileHeader => {