From 522b4967389881749405b5872f46d0b8d0625b81 Mon Sep 17 00:00:00 2001 From: Niklas Fiekas Date: Tue, 11 Jan 2022 14:44:47 +0100 Subject: [PATCH] Optimize multi_get_cf_opt() to use size hint (#579) --- src/db.rs | 26 +++++++++++--------------- 1 file changed, 11 insertions(+), 15 deletions(-) diff --git a/src/db.rs b/src/db.rs index 131a8fc..0738b7d 100644 --- a/src/db.rs +++ b/src/db.rs @@ -870,26 +870,22 @@ impl DBWithThreadMode { I: IntoIterator, W: AsColumnFamilyRef, { - let mut boxed_keys: Vec> = Vec::new(); - let mut keys_sizes = Vec::new(); - let mut column_families = Vec::new(); - for (cf, key) in keys { - boxed_keys.push(Box::from(key.as_ref())); - keys_sizes.push(key.as_ref().len()); - column_families.push(cf); - } - let ptr_keys: Vec<_> = boxed_keys + let (cfs_and_keys, keys_sizes): (Vec<(_, Box<[u8]>)>, Vec<_>) = keys + .into_iter() + .map(|(cf, key)| ((cf, Box::from(key.as_ref())), key.as_ref().len())) + .unzip(); + let ptr_keys: Vec<_> = cfs_and_keys .iter() - .map(|k| k.as_ptr() as *const c_char) + .map(|(_, k)| k.as_ptr() as *const c_char) .collect(); - let ptr_cfs: Vec<_> = column_families + let ptr_cfs: Vec<_> = cfs_and_keys .iter() - .map(|c| c.inner() as *const _) + .map(|(c, _)| c.inner() as *const _) .collect(); - let mut values = vec![ptr::null_mut(); boxed_keys.len()]; - let mut values_sizes = vec![0_usize; boxed_keys.len()]; - let mut errors = vec![ptr::null_mut(); boxed_keys.len()]; + let mut values = vec![ptr::null_mut(); ptr_keys.len()]; + let mut values_sizes = vec![0_usize; ptr_keys.len()]; + let mut errors = vec![ptr::null_mut(); ptr_keys.len()]; unsafe { ffi::rocksdb_multi_get_cf( self.inner,