Skip to content

Commit

Permalink
Adjust mmap flash call to improve performance
Browse files Browse the repository at this point in the history
Signed-off-by: kexuan.yang <[email protected]>
  • Loading branch information
yangkx1024 committed Jul 24, 2024
1 parent cf8319b commit 57e9018
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 18 deletions.
4 changes: 2 additions & 2 deletions src/core/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ mod tests {
let _ = fs::remove_file(file_name);
let file = OpenOptions::new()
.create(true)
.truncate(true)
.write(true)
.read(true)
.open(file_name)
Expand All @@ -140,8 +141,7 @@ mod tests {
let test_encoder = &TestEncoderDecoder;
for i in 0..10 {
let buffer = Buffer::from_i32(&i.to_string(), i);
mm.append(test_encoder.encode_to_bytes(&buffer, i as u32).unwrap())
.unwrap();
mm.append(test_encoder.encode_to_bytes(&buffer, i as u32).unwrap());
buffers.push(buffer);
}
let decoder = &TestEncoderDecoder;
Expand Down
20 changes: 12 additions & 8 deletions src/core/memory_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,27 +82,31 @@ unsafe impl Sync for RawMmap {}
#[repr(transparent)]
pub struct MemoryMap(RawMmap);

impl Drop for MemoryMap {
fn drop(&mut self) {
self.0.flush(self.write_offset()).unwrap();
}
}

impl MemoryMap {
pub fn new(file: &File, len: usize) -> Self {
let raw_mmap = RawMmap::new(file.as_raw_fd(), len).unwrap();
MemoryMap(raw_mmap)
}

pub fn append(&mut self, value: Vec<u8>) -> io::Result<()> {
pub fn append(&mut self, value: Vec<u8>) {
let data_len = value.len();
let start = self.write_offset();
let content_len = start - LEN_OFFSET;
let end = data_len + start;
let new_content_len = data_len + content_len;
self.0[0..LEN_OFFSET].copy_from_slice(new_content_len.to_be_bytes().as_slice());
self.0[start..end].copy_from_slice(value.as_slice());
self.0.flush(end)
}

pub fn reset(&mut self) -> io::Result<()> {
pub fn reset(&mut self) {
let len = 0usize;
self.0[0..LEN_OFFSET].copy_from_slice(len.to_be_bytes().as_slice());
self.0.flush(LEN_OFFSET)
}

pub fn content_start_offset(&self) -> usize {
Expand Down Expand Up @@ -144,8 +148,8 @@ mod tests {
file.set_len(1024).unwrap();
let mut mm = MemoryMap::new(&file, 1024);
assert_eq!(mm.write_offset(), LEN_OFFSET);
mm.append(vec![1, 2, 3]).unwrap();
mm.append(vec![4]).unwrap();
mm.append(vec![1, 2, 3]);
mm.append(vec![4]);
assert_eq!(mm.write_offset(), 12);

let read = mm.read(8..10);
Expand All @@ -155,8 +159,8 @@ mod tests {
let read = mm.read(mm.write_offset() - 1..mm.write_offset());
assert_eq!(read[0], 4);

mm.reset().unwrap();
mm.append(vec![5, 4, 3, 2, 1]).unwrap();
mm.reset();
mm.append(vec![5, 4, 3, 2, 1]);
assert_eq!(mm.write_offset(), 13);
let read = mm.read(8..9);
assert_eq!(read[0], 5);
Expand Down
14 changes: 6 additions & 8 deletions src/core/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use crate::core::buffer::{Buffer, Decoder, Encoder};
use crate::core::config::Config;
use crate::core::io_looper::Callback;
use crate::core::memory_map::MemoryMap;
use crate::Error::EncodeFailed;
use std::time::Instant;

const LOG_TAG: &str = "MMKV:IO";
Expand Down Expand Up @@ -48,7 +47,7 @@ impl IOWriter {
self.need_trim = true;
}
if target_end <= max_len {
self.mm.append(data).unwrap();
self.mm.append(data);
self.position += 1;
return;
}
Expand All @@ -60,6 +59,7 @@ impl IOWriter {
"start trim, current len {}",
self.mm.write_offset()
);
info!(LOG_TAG,"start take snapshot");
let (mut snapshot, _) = self
.mm
.iter(|bytes, position| self.decoder.decode_bytes(bytes, position))
Expand All @@ -69,17 +69,15 @@ impl IOWriter {
} else {
snapshot.insert(buffer.key().to_string(), buffer);
}
self.mm
.reset()
.map_err(|e| EncodeFailed(e.to_string()))
.unwrap();
info!(LOG_TAG,"snapshot finished in {:?}", time_start.elapsed());
self.mm.reset();
self.position = 0;
for buffer in snapshot.values() {
let bytes = self.encoder.encode_to_bytes(buffer, self.position).unwrap();
if self.mm.write_offset() + bytes.len() > max_len {
self.expand();
}
self.mm.append(bytes).unwrap();
self.mm.append(bytes);
self.position += 1;
}
self.need_trim = false;
Expand All @@ -93,7 +91,7 @@ impl IOWriter {
} else {
// expand and write
self.expand();
self.mm.append(data).unwrap();
self.mm.append(data);
self.position += 1;
}
}
Expand Down

0 comments on commit 57e9018

Please sign in to comment.