Skip to content

Commit

Permalink
Remove temporary boxed keys in batched_multi_get
Browse files Browse the repository at this point in the history
By forcing the caller to give us an iterator of key references, we can avoid expensive and needless Boxing of keys.
  • Loading branch information
axnsan12 committed Jul 8, 2023
1 parent c62c8c2 commit fc39adf
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1173,38 +1173,38 @@ impl<T: ThreadMode, D: DBInner> DBCommon<T, D> {
/// Return the values associated with the given keys and the specified column family
/// where internally the read requests are processed in batch if block-based table
/// SST format is used. It is a more optimized version of multi_get_cf.
pub fn batched_multi_get_cf<K, I>(
pub fn batched_multi_get_cf<'a, K, I>(
&self,
cf: &impl AsColumnFamilyRef,
keys: I,
sorted_input: bool,
) -> Vec<Result<Option<DBPinnableSlice>, Error>>
where
K: AsRef<[u8]>,
I: IntoIterator<Item = K>,
K: AsRef<[u8]> + 'a + ?Sized,
I: IntoIterator<Item = &'a K>,
{
self.batched_multi_get_cf_opt(cf, keys, sorted_input, &ReadOptions::default())
}

/// Return the values associated with the given keys and the specified column family
/// where internally the read requests are processed in batch if block-based table
/// SST format is used. It is a more optimized version of multi_get_cf_opt.
pub fn batched_multi_get_cf_opt<K, I>(
/// SST format is used. It is a more optimized version of multi_get_cf_opt.
pub fn batched_multi_get_cf_opt<'a, K, I>(
&self,
cf: &impl AsColumnFamilyRef,
keys: I,
sorted_input: bool,
readopts: &ReadOptions,
) -> Vec<Result<Option<DBPinnableSlice>, Error>>
where
K: AsRef<[u8]>,
I: IntoIterator<Item = K>,
K: AsRef<[u8]> + 'a + ?Sized,
I: IntoIterator<Item = &'a K>,
{
let (keys, keys_sizes): (Vec<Box<[u8]>>, Vec<_>) = keys
let keys: Vec<&[u8]> = keys.into_iter().map(|k| k.as_ref()).collect::<Vec<_>>();
let (ptr_keys, keys_sizes): (Vec<_>, Vec<_>) = keys
.into_iter()
.map(|k| (Box::from(k.as_ref()), k.as_ref().len()))
.map(|k| (k.as_ptr() as *const c_char, k.len()))
.unzip();
let ptr_keys: Vec<_> = keys.iter().map(|k| k.as_ptr() as *const c_char).collect();

let mut pinned_values = vec![ptr::null_mut(); ptr_keys.len()];
let mut errors = vec![ptr::null_mut(); ptr_keys.len()];
Expand Down

0 comments on commit fc39adf

Please sign in to comment.