From 0daf436bfa8d0bcdfbb026ba15f808b691d1a9eb Mon Sep 17 00:00:00 2001 From: jeff washington Date: Thu, 30 Mar 2023 07:40:21 -0500 Subject: [PATCH] disk index: slice/range cleanup --- bucket_map/src/bucket_storage.rs | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/bucket_map/src/bucket_storage.rs b/bucket_map/src/bucket_storage.rs index b03990f3c99930..e15dabfb831cb4 100644 --- a/bucket_map/src/bucket_storage.rs +++ b/bucket_map/src/bucket_storage.rs @@ -188,11 +188,16 @@ impl BucketStorage { self.get_start_offset_with_header(ix) + O::offset_to_first_data() } - pub fn get_cell_slice(&self, ix: u64, len: u64) -> &[T] { - let start = self.get_start_offset_no_header(ix); + pub(crate) fn get_range_no_header(&self, ix: u64, len: u64) -> std::ops::Range { + let start = self.get_start_offset_with_header(ix) + O::offset_to_first_data(); let end = start + std::mem::size_of::() * len as usize; + start..end + } + + pub fn get_cell_slice(&self, ix: u64, len: u64) -> &[T] { + let range = self.get_range_no_header::(ix, len); //debug!("GET slice {} {}", start, end); - let item_slice: &[u8] = &self.mmap[start..end]; + let item_slice: &[u8] = &self.mmap[range]; unsafe { let item = item_slice.as_ptr() as *const T; std::slice::from_raw_parts(item, len as usize) @@ -206,9 +211,8 @@ impl BucketStorage { } pub fn get_mut(&mut self, ix: u64) -> &mut T { - let start = self.get_start_offset_no_header(ix); - let item_slice = &mut self.mmap[start..]; - let item_slice = &mut item_slice[..std::mem::size_of::()]; + let range = self.get_range_no_header::(ix, 1); + let item_slice = &mut self.mmap[range]; Self::get_mut_from_parts(item_slice) } @@ -216,11 +220,9 @@ impl BucketStorage { let start = self.get_start_offset_no_header(ix); let end = start + std::mem::size_of::() * len as usize; //debug!("GET mut slice {} {}", start, end); - let item_slice: &[u8] = &self.mmap[start..end]; - unsafe { - let item = item_slice.as_ptr() as *mut T; - std::slice::from_raw_parts_mut(item, len as usize) - } + let item_slice = &mut self.mmap[start..end]; + let item = item_slice.as_mut_ptr() as *mut T; + unsafe { std::slice::from_raw_parts_mut(item, len as usize) } } fn new_map(