Skip to content

Commit

Permalink
Removed needless allocation, split serialize into serialize and clear.
Browse files Browse the repository at this point in the history
  • Loading branch information
fulmicoton committed Dec 22, 2022
1 parent ca0d361 commit 2232000
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 8 deletions.
10 changes: 7 additions & 3 deletions sstable/src/delta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ where W: io::Write
block: Vec<u8>,
write: CountingWriter<BufWriter<W>>,
value_writer: TValueWriter,
// Only here to avoid allocations.
stateless_buffer: Vec<u8>,
}

impl<W, TValueWriter> DeltaWriter<W, TValueWriter>
Expand All @@ -28,6 +30,7 @@ where
block: Vec::with_capacity(BLOCK_LEN * 2),
write: CountingWriter::wrap(BufWriter::new(wrt)),
value_writer: TValueWriter::default(),
stateless_buffer: Vec::new(),
}
}
}
Expand All @@ -42,15 +45,16 @@ where
return Ok(None);
}
let start_offset = self.write.written_bytes() as usize;
// TODO avoid buffer allocation
let mut buffer = Vec::new();
self.value_writer.serialize_block(&mut buffer);
let buffer: &mut Vec<u8> = &mut self.stateless_buffer;
self.value_writer.serialize(buffer);
self.value_writer.clear();
let block_len = buffer.len() + self.block.len();
self.write.write_all(&(block_len as u32).to_le_bytes())?;
self.write.write_all(&buffer[..])?;
self.write.write_all(&self.block[..])?;
let end_offset = self.write.written_bytes() as usize;
self.block.clear();
buffer.clear();
Ok(Some(start_offset..end_offset))
}

Expand Down
11 changes: 9 additions & 2 deletions sstable/src/value/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ pub trait ValueReader: Default {
fn load(&mut self, data: &[u8]) -> io::Result<usize>;
}

/// `ValueWriter` is a trait to make it possible to write blocks
/// of value.
pub trait ValueWriter: Default {
/// Type of the value being written.
type Value;
Expand All @@ -30,7 +32,11 @@ pub trait ValueWriter: Default {
fn write(&mut self, val: &Self::Value);

/// Serializes the accumulated values into the output buffer.
fn serialize_block(&mut self, output: &mut Vec<u8>);
fn serialize(&self, output: &mut Vec<u8>);

/// Clears the `ValueWriter`. After a call to clear, the `ValueWriter`
/// should behave like a fresh `ValueWriter::default()`.
fn clear(&mut self);
}

pub use range::{RangeReader, RangeWriter};
Expand Down Expand Up @@ -62,7 +68,8 @@ pub(crate) mod tests {
for value in value_block {
writer.write(value);
}
writer.serialize_block(&mut buffer);
writer.serialize(&mut buffer);
writer.clear();
}
let data_len = buffer.len();
buffer.extend_from_slice(&b"extradata"[..]);
Expand Down
5 changes: 4 additions & 1 deletion sstable/src/value/range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,17 @@ impl ValueWriter for RangeWriter {
}
}

fn serialize_block(&mut self, writer: &mut Vec<u8>) {
fn serialize(&self, writer: &mut Vec<u8>) {
let mut prev_val = 0u64;
crate::vint::serialize_into_vec(self.vals.len() as u64, writer);
for &val in &self.vals {
let delta = val - prev_val;
crate::vint::serialize_into_vec(delta, writer);
prev_val = val;
}
}

fn clear(&mut self) {
self.vals.clear();
}
}
Expand Down
5 changes: 4 additions & 1 deletion sstable/src/value/u64_monotonic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,17 @@ impl ValueWriter for U64MonotonicWriter {
self.vals.push(*val);
}

fn serialize_block(&mut self, output: &mut Vec<u8>) {
fn serialize(&self, output: &mut Vec<u8>) {
let mut prev_val = 0u64;
vint::serialize_into_vec(self.vals.len() as u64, output);
for &val in &self.vals {
let delta = val - prev_val;
vint::serialize_into_vec(delta, output);
prev_val = val;
}
}

fn clear(&mut self) {
self.vals.clear();
}
}
Expand Down
4 changes: 3 additions & 1 deletion sstable/src/value/void.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ impl ValueWriter for VoidWriter {

fn write(&mut self, _val: &()) {}

fn serialize_block(&mut self, _output: &mut Vec<u8>) {}
fn serialize(&self, _output: &mut Vec<u8>) {}

fn clear(&mut self) {}
}

#[cfg(test)]
Expand Down

0 comments on commit 2232000

Please sign in to comment.