src/backup/data_chunk.rs: only use compressed data if it is shorter than uncompressed

This commit is contained in:
Dietmar Maurer 2019-06-13 12:56:50 +02:00
parent 68ab376ae9
commit dba72d5016
1 changed files with 13 additions and 13 deletions

View File

@ -66,7 +66,7 @@ impl DataChunk {
if let Some(config) = config { if let Some(config) = config {
let enc_data = config.encode_chunk(data, compress)?; Let enc_data = config.encode_chunk(data, compress)?;
let chunk = DataChunk { digest, raw_data: enc_data }; let chunk = DataChunk { digest, raw_data: enc_data };
Ok(chunk) Ok(chunk)
@ -78,11 +78,12 @@ impl DataChunk {
comp_data.write_all(&COMPRESSED_CHUNK_MAGIC_1_0)?; comp_data.write_all(&COMPRESSED_CHUNK_MAGIC_1_0)?;
zstd::stream::copy_encode(data, &mut comp_data, 1)?; zstd::stream::copy_encode(data, &mut comp_data, 1)?;
if comp_data.len() < (data.len() + 8) {
let chunk = DataChunk { digest, raw_data: comp_data }; let chunk = DataChunk { digest, raw_data: comp_data };
return Ok(chunk); return Ok(chunk);
} else { }
// TODO: howto avoid data copy here? }
let mut raw_data = Vec::with_capacity(data.len() + 8); let mut raw_data = Vec::with_capacity(data.len() + 8);
raw_data.write_all(&UNCOMPRESSED_CHUNK_MAGIC_1_0)?; raw_data.write_all(&UNCOMPRESSED_CHUNK_MAGIC_1_0)?;
@ -92,7 +93,6 @@ impl DataChunk {
return Ok(chunk); return Ok(chunk);
} }
} }
}
/// Decode chunk data /// Decode chunk data
pub fn decode(self, config: Option<&CryptConfig>) -> Result<Vec<u8>, Error> { pub fn decode(self, config: Option<&CryptConfig>) -> Result<Vec<u8>, Error> {