Skip to content

Commit

Permalink
Avoid per-access overhead in WritablePage using unsafe code.
Browse files Browse the repository at this point in the history
  • Loading branch information
adamreichold committed May 8, 2024
1 parent 7aa2d43 commit 0a63e55
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions src/tree_store/page_store/cached_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::tree_store::LEAF;
use crate::{DatabaseError, Result, StorageBackend, StorageError};
use std::collections::BTreeMap;
use std::io;
use std::mem::ManuallyDrop;
use std::ops::{Index, IndexMut};
use std::slice::SliceIndex;
#[cfg(feature = "cache_metrics")]
Expand Down Expand Up @@ -30,23 +31,27 @@ impl CachePriority {
pub(super) struct WritablePage {
buffer: Arc<Mutex<PrioritizedWriteCache>>,
offset: u64,
data: Option<Arc<[u8]>>,
data: ManuallyDrop<Arc<[u8]>>,
priority: CachePriority,
}

impl WritablePage {
pub(super) fn mem(&self) -> &[u8] {
self.data.as_ref().unwrap()
&self.data
}

pub(super) fn mem_mut(&mut self) -> &mut [u8] {
Arc::get_mut(self.data.as_mut().unwrap()).unwrap()
let data = Arc::get_mut(&mut self.data);
debug_assert!(data.is_some());
// SAFETY: `self` has the sole reference to `data`.
unsafe { data.unwrap_unchecked() }
}
}

impl Drop for WritablePage {
fn drop(&mut self) {
let data = self.data.take().unwrap();
// SAFETY: `drop` is called at most once
let data = unsafe { ManuallyDrop::take(&mut self.data) };
self.buffer
.lock()
.unwrap()
Expand Down Expand Up @@ -487,7 +492,7 @@ impl PagedCachedFile {
Ok(WritablePage {
buffer: self.write_buffer.clone(),
offset,
data: Some(data),
data: ManuallyDrop::new(data),
priority,
})
}
Expand Down

0 comments on commit 0a63e55

Please sign in to comment.