Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

get rid of unnecessary mut #28017

Merged
merged 1 commit into from
Sep 26, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 21 additions & 6 deletions runtime/src/cache_hash_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,26 @@ struct CacheHashDataFile {
}

impl CacheHashDataFile {
/// get '&mut T' from cache file [ix]
fn get_mut<T: Sized>(&mut self, ix: u64) -> &mut T {
let item_slice = self.get_slice_internal::<T>(ix);
unsafe {
let item = item_slice.as_ptr() as *mut T;
&mut *item
}
}

/// get '&T' from cache file [ix]
fn get<T: Sized>(&self, ix: u64) -> &T {
let item_slice = self.get_slice_internal::<T>(ix);
unsafe {
let item = item_slice.as_ptr() as *const T;
Copy link
Contributor

@brooksprumo brooksprumo Sep 26, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Can this let line be moved out of the unsafe block? Iiuc, only the deref needs to be scoped by unsafe.

Same for the get_mut fn as well.

&*item
}
}

/// get the bytes representing cache file [ix]
fn get_slice_internal<T: Sized>(&self, ix: u64) -> &[u8] {
let start = (ix * self.cell_size) as usize + std::mem::size_of::<Header>();
let end = start + std::mem::size_of::<T>();
assert!(
Expand All @@ -43,11 +62,7 @@ impl CacheHashDataFile {
ix,
self.cell_size
);
let item_slice: &[u8] = &self.mmap[start..end];
unsafe {
let item = item_slice.as_ptr() as *mut T;
&mut *item
}
&self.mmap[start..end]
}

fn get_header_mut(&mut self) -> &mut Header {
Expand Down Expand Up @@ -228,7 +243,7 @@ impl CacheHashData {
stats.entries_loaded_from_cache += entries;
let mut m2 = Measure::start("decode");
for i in 0..entries {
let d = cache_file.get_mut::<EntryType>(i as u64);
let d = cache_file.get::<EntryType>(i as u64);
let mut pubkey_to_bin_index = bin_calculator.bin_from_pubkey(&d.pubkey);
assert!(
pubkey_to_bin_index >= start_bin_index,
Expand Down