From 57e90188b813617dfd9e114698b810663bd648eb Mon Sep 17 00:00:00 2001 From: "kexuan.yang" Date: Thu, 25 Jul 2024 01:14:29 +0800 Subject: [PATCH] Adjust mmap flash call to improve performance Signed-off-by: kexuan.yang --- src/core/iter.rs | 4 ++-- src/core/memory_map.rs | 20 ++++++++++++-------- src/core/writer.rs | 14 ++++++-------- 3 files changed, 20 insertions(+), 18 deletions(-) diff --git a/src/core/iter.rs b/src/core/iter.rs index 18bd580..014a8e0 100644 --- a/src/core/iter.rs +++ b/src/core/iter.rs @@ -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) @@ -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; diff --git a/src/core/memory_map.rs b/src/core/memory_map.rs index 8a2ec40..f779514 100644 --- a/src/core/memory_map.rs +++ b/src/core/memory_map.rs @@ -82,13 +82,19 @@ 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) -> io::Result<()> { + pub fn append(&mut self, value: Vec) { let data_len = value.len(); let start = self.write_offset(); let content_len = start - LEN_OFFSET; @@ -96,13 +102,11 @@ impl MemoryMap { 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 { @@ -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); @@ -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); diff --git a/src/core/writer.rs b/src/core/writer.rs index ff8310a..7267dd1 100644 --- a/src/core/writer.rs +++ b/src/core/writer.rs @@ -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"; @@ -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; } @@ -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)) @@ -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; @@ -93,7 +91,7 @@ impl IOWriter { } else { // expand and write self.expand(); - self.mm.append(data).unwrap(); + self.mm.append(data); self.position += 1; } }