From 2e679997b07ae42214d65d8a5a6911739f018317 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20du=20Garreau?= Date: Fri, 12 Jul 2024 10:05:42 +0200 Subject: [PATCH] Avoid an intermediary buffer in LZMA decoder --- src/read/lzma.rs | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/src/read/lzma.rs b/src/read/lzma.rs index 0b29b761..e4720914 100644 --- a/src/read/lzma.rs +++ b/src/read/lzma.rs @@ -1,8 +1,6 @@ use lzma_rs::decompress::{Options, Stream, UnpackedSize}; use std::collections::VecDeque; -use std::io::{Read, Result, Write}; - -const COMPRESSED_BYTES_TO_BUFFER: usize = 4096; +use std::io::{BufRead, Read, Result, Write}; const OPTIONS: Options = Options { unpacked_size: UnpackedSize::ReadFromHeader, @@ -29,17 +27,15 @@ impl LzmaDecoder { } } -impl Read for LzmaDecoder { +impl Read for LzmaDecoder { fn read(&mut self, buf: &mut [u8]) -> Result { let mut bytes_read = self.stream.get_output_mut().unwrap().read(buf)?; while bytes_read < buf.len() { - let mut next_compressed = [0u8; COMPRESSED_BYTES_TO_BUFFER]; - let compressed_bytes_read = self.compressed_reader.read(&mut next_compressed)?; - if compressed_bytes_read == 0 { + let compressed_bytes = self.compressed_reader.fill_buf()?; + if compressed_bytes.is_empty() { break; } - self.stream - .write_all(&next_compressed[..compressed_bytes_read])?; + self.stream.write_all(compressed_bytes)?; bytes_read += self .stream .get_output_mut()