Skip to content

Commit

Permalink
compression: avoid copy on none algo
Browse files Browse the repository at this point in the history
This commit fixes the redundant copy that was done when calling `decompress`
with the `None` compression. We should see less memcpys now.
  • Loading branch information
Johannes Wünsche committed Jan 26, 2024
1 parent 4b29cd4 commit 59c1d9b
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 12 deletions.
2 changes: 1 addition & 1 deletion betree/src/compression/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ pub trait CompressionState: Write {
}

pub trait DecompressionState {
fn decompress(&mut self, data: &[u8]) -> Result<Box<[u8]>>;
fn decompress(&mut self, data: Buf) -> Result<Buf>;
}

mod none;
Expand Down
4 changes: 2 additions & 2 deletions betree/src/compression/none.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ impl CompressionState for NoneCompression {
}

impl DecompressionState for NoneDecompression {
fn decompress(&mut self, data: &[u8]) -> Result<Box<[u8]>> {
fn decompress(&mut self, data: Buf) -> Result<Buf> {
// FIXME: pass-through Buf, reusing alloc
Ok(data.to_vec().into_boxed_slice())
Ok(data)
}
}
8 changes: 4 additions & 4 deletions betree/src/compression/zstd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ impl CompressionState for ZstdCompression {
}

impl DecompressionState for ZstdDecompression {
fn decompress(&mut self, data: &[u8]) -> Result<Box<[u8]>> {
let size = u32::read_from_buffer(data).unwrap();
fn decompress(&mut self, data: Buf) -> Result<Buf> {
let size = u32::read_from_buffer(data.as_ref()).unwrap();
let mut buf = BufWrite::with_capacity(Block::round_up_from_bytes(size));

let mut input = zstd::stream::raw::InBuffer::around(&data[DATA_OFF..]);
Expand All @@ -150,7 +150,7 @@ impl DecompressionState for ZstdDecompression {
while self.writer.flush(&mut output)? > 0 {}
self.writer.finish(&mut output, finished_frame)?;

Ok(buf.into_buf().as_ref().into())
Ok(buf.into_buf())
}
}

Expand All @@ -170,7 +170,7 @@ mod tests {
let mut comp = zstd.new_compression().unwrap();
let c_buf = comp.finish(buf.clone()).unwrap();
let mut decomp = zstd.decompression_tag().new_decompression().unwrap();
let d_buf = decomp.decompress(c_buf.as_ref()).unwrap();
let d_buf = decomp.decompress(c_buf).unwrap();
assert_eq!(buf.as_ref().len(), d_buf.as_ref().len());
}

Expand Down
9 changes: 4 additions & 5 deletions betree/src/data_management/dmu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,8 @@ where
.read(op.size(), op.offset(), op.checksum().clone())?;

let object: Node<ObjRef<ObjectPointer<SPL::Checksum>>> = {
let data = decompression_state.decompress(&compressed_data)?;
Object::unpack_at(op.offset(), op.info(), data)?
let data = decompression_state.decompress(compressed_data)?;
Object::unpack_at(op.offset(), op.info(), data.into_boxed_slice())?
};
let key = ObjectKey::Unmodified { offset, generation };
self.insert_object_into_cache(key, TaggedCacheValue::new(RwLock::new(object), pivot_key));
Expand Down Expand Up @@ -413,7 +413,6 @@ where
state.finish()
};

// FIXME: COMPRESSION
self.pool.begin_write(compressed_data, offset)?;

let obj_ptr = ObjectPointer {
Expand Down Expand Up @@ -937,8 +936,8 @@ where
let data = ptr
.decompression_tag()
.new_decompression()?
.decompress(&compressed_data)?;
Object::unpack_at(ptr.offset(), ptr.info(), data)?
.decompress(compressed_data)?;
Object::unpack_at(ptr.offset(), ptr.info(), data.into_boxed_slice())?
};
let key = ObjectKey::Unmodified {
offset: ptr.offset(),
Expand Down

0 comments on commit 59c1d9b

Please sign in to comment.