Shave some CPU cycles from reading blobs from files.

This shows up in 'perf' profile when running in debug mode. Not so
significant in release mode, but still.
This commit is contained in:
Heikki Linnakangas
2022-03-14 19:53:00 +02:00
parent 89690d7349
commit 60ed6b3710
2 changed files with 12 additions and 5 deletions

View File

@@ -335,11 +335,13 @@ impl InMemoryLayer {
self.start_lsn..inner.end_lsn.unwrap(),
)?;
let mut buf = Vec::new();
let mut do_steps = || -> Result<()> {
for (key, vec_map) in inner.index.iter() {
// Write all page versions
for (lsn, pos) in vec_map.as_slice() {
let val = Value::des(&utils::read_blob(&inner.file, *pos)?)?;
let len = utils::read_blob_buf(&inner.file, *pos, &mut buf)?;
let val = Value::des(&buf[0..len])?;
delta_layer_writer.put_value(*key, *lsn, val)?;
}
}

View File

@@ -4,17 +4,22 @@ use std::os::unix::fs::FileExt;
use bookfile::BoundedReader;
pub fn read_blob<F: FileExt>(file: &F, off: u64) -> Result<Vec<u8>, Error> {
pub fn read_blob_buf<F: FileExt>(file: &F, off: u64, buf: &mut Vec<u8>) -> Result<usize, Error> {
// read length
let mut len_buf = [0u8; 4];
file.read_exact_at(&mut len_buf, off)?;
let len = u32::from_ne_bytes(len_buf);
let len = u32::from_ne_bytes(len_buf) as usize;
let mut buf: Vec<u8> = Vec::new();
buf.resize(len as usize, 0);
buf.resize(len, 0);
file.read_exact_at(&mut buf.as_mut_slice(), off + 4)?;
Ok(len)
}
pub fn read_blob<F: FileExt>(file: &F, off: u64) -> Result<Vec<u8>, Error> {
let mut buf: Vec<u8> = Vec::new();
let _ = read_blob_buf(file, off, &mut buf);
Ok(buf)
}