disk index: move num_slots() to MultipleSlots (#30991)

This commit is contained in:
Jeff Washington (jwash) 2023-03-30 14:39:15 -05:00 committed by GitHub
parent 2672637395
commit b5afcbb72f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 17 deletions

View File

@ -285,15 +285,18 @@ impl<'b, T: Clone + Copy + 'static> Bucket<T> {
};
elem.set_ref_count(&mut self.index, ref_count);
let bucket_ix = elem.data_bucket_ix(&self.index);
let current_multiple_slots = elem.get_multiple_slots(&self.index);
let bucket_ix =
IndexEntry::<T>::data_bucket_from_num_slots(current_multiple_slots.num_slots());
let num_slots = data_len as u64;
if best_fit_bucket == bucket_ix && elem.num_slots(&self.index) > 0 {
if best_fit_bucket == bucket_ix && current_multiple_slots.num_slots() > 0 {
let current_bucket = &mut self.data[bucket_ix as usize];
// in place update
let elem_loc = elem.data_loc(&self.index, current_bucket);
assert!(!current_bucket.is_free(elem_loc));
let slice: &mut [T] = current_bucket.get_mut_cell_slice(elem_loc, data_len as u64);
elem.set_num_slots(&mut self.index, num_slots);
let current_multiple_slots = elem.get_multiple_slots_mut(&mut self.index);
current_multiple_slots.set_num_slots(num_slots);
slice.iter_mut().zip(data).for_each(|(dest, src)| {
*dest = *src;
@ -318,12 +321,12 @@ impl<'b, T: Clone + Copy + 'static> Bucket<T> {
let ix = i % cap;
if best_bucket.is_free(ix) {
let elem_loc = elem.data_loc(&self.index, current_bucket);
let old_slots = elem.num_slots(&self.index);
let old_slots = current_multiple_slots.num_slots();
let multiple_slots = elem.get_multiple_slots_mut(&mut self.index);
multiple_slots.set_storage_offset(ix);
multiple_slots
.set_storage_capacity_when_created_pow2(best_bucket.capacity_pow2);
elem.set_num_slots(&mut self.index, num_slots);
multiple_slots.set_num_slots(num_slots);
if old_slots > 0 {
let current_bucket = &mut self.data[bucket_ix as usize];
current_bucket.free(elem_loc);
@ -346,7 +349,8 @@ impl<'b, T: Clone + Copy + 'static> Bucket<T> {
pub fn delete_key(&mut self, key: &Pubkey) {
if let Some((elem, elem_ix)) = self.find_index_entry(key) {
if elem.num_slots(&self.index) > 0 {
let multiple_slots = elem.get_multiple_slots_mut(&mut self.index);
if multiple_slots.num_slots() > 0 {
let ix = elem.data_bucket_ix(&self.index) as usize;
let data_bucket = &self.data[ix];
let loc = elem.data_loc(&self.index, data_bucket);

View File

@ -107,6 +107,14 @@ impl MultipleSlots {
fn storage_offset(&self) -> u64 {
self.storage_cap_and_offset.offset()
}
pub(crate) fn num_slots(&self) -> Slot {
self.num_slots
}
pub(crate) fn set_num_slots(&mut self, num_slots: Slot) {
self.num_slots = num_slots;
}
}
/// Pack the storage offset and capacity-when-crated-pow2 fields into a single u64
@ -143,10 +151,12 @@ impl<T> IndexEntryPlaceInBucket<T> {
}
pub fn data_bucket_ix(&self, index_bucket: &BucketStorage<IndexBucket<T>>) -> u64 {
IndexEntry::<T>::data_bucket_from_num_slots(self.num_slots(index_bucket))
IndexEntry::<T>::data_bucket_from_num_slots(
self.get_multiple_slots(index_bucket).num_slots(),
)
}
fn get_multiple_slots<'a>(
pub(crate) fn get_multiple_slots<'a>(
&self,
index_bucket: &'a BucketStorage<IndexBucket<T>>,
) -> &'a MultipleSlots {
@ -184,7 +194,8 @@ impl<T> IndexEntryPlaceInBucket<T> {
index_bucket: &BucketStorage<IndexBucket<T>>,
data_buckets: &'a [BucketStorage<DataBucket>],
) -> Option<(&'a [T], RefCount)> {
let num_slots = self.num_slots(index_bucket);
let multiple_slots = self.get_multiple_slots(index_bucket);
let num_slots = multiple_slots.num_slots();
let slice = if num_slots > 0 {
let data_bucket_ix = self.data_bucket_ix(index_bucket);
let data_bucket = &data_buckets[data_bucket_ix as usize];
@ -218,14 +229,6 @@ impl<T> IndexEntryPlaceInBucket<T> {
let index_entry = index_bucket.get_mut::<IndexEntry<T>>(self.ix);
index_entry.ref_count = ref_count;
}
pub fn num_slots(&self, index_bucket: &BucketStorage<IndexBucket<T>>) -> Slot {
self.get_multiple_slots(index_bucket).num_slots
}
pub fn set_num_slots(&self, index_bucket: &mut BucketStorage<IndexBucket<T>>, num_slots: Slot) {
self.get_multiple_slots_mut(index_bucket).num_slots = num_slots;
}
}
#[cfg(test)]