bucket map try_write takes iter (#30440)
This commit is contained in:
parent
d2c6bd1410
commit
1269011477
|
@ -76,7 +76,7 @@ pub struct Bucket<T> {
|
||||||
pub reallocated: Reallocated,
|
pub reallocated: Reallocated,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: Clone + Copy> Bucket<T> {
|
impl<'b, T: Clone + Copy + 'b> Bucket<T> {
|
||||||
pub fn new(
|
pub fn new(
|
||||||
drives: Arc<Vec<PathBuf>>,
|
drives: Arc<Vec<PathBuf>>,
|
||||||
max_search: MaxSearch,
|
max_search: MaxSearch,
|
||||||
|
@ -268,10 +268,11 @@ impl<T: Clone + Copy> Bucket<T> {
|
||||||
pub fn try_write(
|
pub fn try_write(
|
||||||
&mut self,
|
&mut self,
|
||||||
key: &Pubkey,
|
key: &Pubkey,
|
||||||
data: &[T],
|
data: impl Iterator<Item = &'b T>,
|
||||||
|
data_len: usize,
|
||||||
ref_count: RefCount,
|
ref_count: RefCount,
|
||||||
) -> Result<(), BucketMapError> {
|
) -> Result<(), BucketMapError> {
|
||||||
let best_fit_bucket = IndexEntry::data_bucket_from_num_slots(data.len() as u64);
|
let best_fit_bucket = IndexEntry::data_bucket_from_num_slots(data_len as u64);
|
||||||
if self.data.get(best_fit_bucket as usize).is_none() {
|
if self.data.get(best_fit_bucket as usize).is_none() {
|
||||||
// fail early if the data bucket we need doesn't exist - we don't want the index entry partially allocated
|
// fail early if the data bucket we need doesn't exist - we don't want the index entry partially allocated
|
||||||
return Err(BucketMapError::DataNoSpace((best_fit_bucket, 0)));
|
return Err(BucketMapError::DataNoSpace((best_fit_bucket, 0)));
|
||||||
|
@ -289,14 +290,17 @@ impl<T: Clone + Copy> Bucket<T> {
|
||||||
let elem_uid = self.index.uid_unchecked(elem_ix);
|
let elem_uid = self.index.uid_unchecked(elem_ix);
|
||||||
let bucket_ix = elem.data_bucket_ix();
|
let bucket_ix = elem.data_bucket_ix();
|
||||||
let current_bucket = &self.data[bucket_ix as usize];
|
let current_bucket = &self.data[bucket_ix as usize];
|
||||||
let num_slots = data.len() as u64;
|
let num_slots = data_len as u64;
|
||||||
if best_fit_bucket == bucket_ix && elem.num_slots > 0 {
|
if best_fit_bucket == bucket_ix && elem.num_slots > 0 {
|
||||||
// in place update
|
// in place update
|
||||||
let elem_loc = elem.data_loc(current_bucket);
|
let elem_loc = elem.data_loc(current_bucket);
|
||||||
let slice: &mut [T] = current_bucket.get_mut_cell_slice(elem_loc, data.len() as u64);
|
let slice: &mut [T] = current_bucket.get_mut_cell_slice(elem_loc, data_len as u64);
|
||||||
assert_eq!(current_bucket.uid(elem_loc), Some(elem_uid));
|
assert_eq!(current_bucket.uid(elem_loc), Some(elem_uid));
|
||||||
elem.num_slots = num_slots;
|
elem.num_slots = num_slots;
|
||||||
slice.copy_from_slice(data);
|
|
||||||
|
slice.iter_mut().zip(data).for_each(|(dest, src)| {
|
||||||
|
*dest = *src;
|
||||||
|
});
|
||||||
Ok(())
|
Ok(())
|
||||||
} else {
|
} else {
|
||||||
// need to move the allocation to a best fit spot
|
// need to move the allocation to a best fit spot
|
||||||
|
@ -321,7 +325,9 @@ impl<T: Clone + Copy> Bucket<T> {
|
||||||
let best_bucket = &mut self.data[best_fit_bucket as usize];
|
let best_bucket = &mut self.data[best_fit_bucket as usize];
|
||||||
best_bucket.allocate(ix, elem_uid, false).unwrap();
|
best_bucket.allocate(ix, elem_uid, false).unwrap();
|
||||||
let slice = best_bucket.get_mut_cell_slice(ix, num_slots);
|
let slice = best_bucket.get_mut_cell_slice(ix, num_slots);
|
||||||
slice.copy_from_slice(data);
|
slice.iter_mut().zip(data).for_each(|(dest, src)| {
|
||||||
|
*dest = *src;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
@ -498,7 +504,7 @@ impl<T: Clone + Copy> Bucket<T> {
|
||||||
pub fn insert(&mut self, key: &Pubkey, value: (&[T], RefCount)) {
|
pub fn insert(&mut self, key: &Pubkey, value: (&[T], RefCount)) {
|
||||||
let (new, refct) = value;
|
let (new, refct) = value;
|
||||||
loop {
|
loop {
|
||||||
let rv = self.try_write(key, new, refct);
|
let rv = self.try_write(key, new.iter(), new.len(), refct);
|
||||||
match rv {
|
match rv {
|
||||||
Ok(_) => return,
|
Ok(_) => return,
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
|
|
|
@ -137,6 +137,9 @@ impl<T: Clone + Copy> BucketApi<T> {
|
||||||
value: (&[T], RefCount),
|
value: (&[T], RefCount),
|
||||||
) -> Result<(), BucketMapError> {
|
) -> Result<(), BucketMapError> {
|
||||||
let mut bucket = self.get_write_bucket();
|
let mut bucket = self.get_write_bucket();
|
||||||
bucket.as_mut().unwrap().try_write(pubkey, value.0, value.1)
|
bucket
|
||||||
|
.as_mut()
|
||||||
|
.unwrap()
|
||||||
|
.try_write(pubkey, value.0.iter(), value.0.len(), value.1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue