Skip to content

Commit

Permalink
AcctIdx: bucket perf improvements (solana-labs#20328)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffwashington authored Sep 30, 2021
1 parent 59112ad commit 5e05f12
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 41 deletions.
38 changes: 22 additions & 16 deletions bucket_map/src/bucket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,12 @@ impl<T: Clone + Copy> Bucket<T> {
data: &[T],
ref_count: u64,
) -> Result<(), BucketMapError> {
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() {
// fail early if the data bucket we need doesn't exist - we don't want the index entry partially allocated
//error!("resizing because missing bucket");
return Err(BucketMapError::DataNoSpace((best_fit_bucket, 0)));
}
let index_entry = self.find_entry_mut(key);
let (elem, elem_ix) = match index_entry {
None => {
Expand All @@ -213,11 +219,6 @@ impl<T: Clone + Copy> Bucket<T> {
}
};
let elem_uid = self.index.uid(elem_ix);
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() {
//error!("resizing because missing bucket");
return Err(BucketMapError::DataNoSpace((best_fit_bucket, 0)));
}
let bucket_ix = elem.data_bucket_ix();
let current_bucket = &self.data[bucket_ix as usize];
if best_fit_bucket == bucket_ix && elem.num_slots > 0 {
Expand Down Expand Up @@ -366,22 +367,27 @@ impl<T: Clone + Copy> Bucket<T> {
//debug!( "INDEX_IX: {:?} uid:{} loc: {} cap:{}", key, uid, location, index.capacity() );
}

/// grow the appropriate piece
pub fn grow(&mut self, err: BucketMapError) {
match err {
BucketMapError::DataNoSpace(sz) => {
//debug!("GROWING SPACE {:?}", sz);
self.grow_data(sz);
}
BucketMapError::IndexNoSpace(sz) => {
//debug!("GROWING INDEX {}", sz);
self.grow_index(sz);
}
}
}

pub fn insert(&mut self, key: &Pubkey, value: (&[T], RefCount)) {
let (new, refct) = value;
loop {
let rv = self.try_write(key, new, refct);
match rv {
Err(BucketMapError::DataNoSpace(sz)) => {
//debug!("GROWING SPACE {:?}", sz);
self.grow_data(sz);
continue;
}
Err(BucketMapError::IndexNoSpace(sz)) => {
//debug!("GROWING INDEX {}", sz);
self.grow_index(sz);
continue;
}
Ok(()) => return,
Ok(_) => return,
Err(err) => self.grow(err),
}
}
}
Expand Down
78 changes: 54 additions & 24 deletions bucket_map/src/bucket_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use std::fs;
use std::ops::RangeBounds;
use std::path::PathBuf;
use std::sync::Arc;
use std::sync::RwLock;
use std::sync::{RwLock, RwLockWriteGuard};
use tempfile::TempDir;

#[derive(Debug, Default, Clone)]
Expand Down Expand Up @@ -166,8 +166,12 @@ impl<T: Clone + Copy + Debug> BucketMap<T> {
}

/// Update Pubkey `key`'s value with 'value'
pub fn insert(&self, key: &Pubkey, value: (&[T], RefCount)) {
let ix = self.bucket_ix(key);
pub fn insert(&self, ix: usize, key: &Pubkey, value: (&[T], RefCount)) {
let mut bucket = self.get_bucket(ix);
bucket.as_mut().unwrap().insert(key, value)
}

fn get_bucket(&self, ix: usize) -> RwLockWriteGuard<Option<Bucket<T>>> {
let mut bucket = self.buckets[ix].write().unwrap();
if bucket.is_none() {
*bucket = Some(Bucket::new(
Expand All @@ -176,8 +180,24 @@ impl<T: Clone + Copy + Debug> BucketMap<T> {
Arc::clone(&self.stats),
));
}
let bucket = bucket.as_mut().unwrap();
bucket.insert(key, value)
bucket
}

/// Update Pubkey `key`'s value with 'value'
pub fn try_insert(
&self,
ix: usize,
key: &Pubkey,
value: (&[T], RefCount),
) -> Result<(), BucketMapError> {
let mut bucket = self.get_bucket(ix);
bucket.as_mut().unwrap().try_write(key, value.0, value.1)
}

/// if err is a grow error, then grow the appropriate piece
pub fn grow(&self, ix: usize, err: BucketMapError) {
let mut bucket = self.get_bucket(ix);
bucket.as_mut().unwrap().grow(err);
}

/// Update Pubkey `key`'s value with function `updatefn`
Expand All @@ -186,16 +206,8 @@ impl<T: Clone + Copy + Debug> BucketMap<T> {
F: Fn(Option<(&[T], RefCount)>) -> Option<(Vec<T>, RefCount)>,
{
let ix = self.bucket_ix(key);
let mut bucket = self.buckets[ix].write().unwrap();
if bucket.is_none() {
*bucket = Some(Bucket::new(
Arc::clone(&self.drives),
self.max_search,
Arc::clone(&self.stats),
));
}
let bucket = bucket.as_mut().unwrap();
bucket.update(key, updatefn)
let mut bucket = self.get_bucket(ix);
bucket.as_mut().unwrap().update(key, updatefn)
}

/// Get the bucket index for Pubkey `key`
Expand Down Expand Up @@ -247,21 +259,39 @@ mod tests {

#[test]
fn bucket_map_test_insert2() {
let key = Pubkey::new_unique();
let config = BucketMapConfig::new(1 << 1);
let index = BucketMap::new(config);
index.insert(&key, (&[0], 0));
assert_eq!(index.read_value(&key), Some((vec![0], 0)));
for pass in 0..3 {
let key = Pubkey::new_unique();
let config = BucketMapConfig::new(1 << 1);
let index = BucketMap::new(config);
let ix = index.bucket_ix(&key);
if pass == 0 {
index.insert(ix, &key, (&[0], 0));
} else {
let result = index.try_insert(ix, &key, (&[0], 0));
assert!(result.is_err());
assert_eq!(index.read_value(&key), None);
if pass == 2 {
// another call to try insert again - should still return an error
let result = index.try_insert(ix, &key, (&[0], 0));
assert!(result.is_err());
assert_eq!(index.read_value(&key), None);
}
index.grow(ix, result.unwrap_err());
let result = index.try_insert(ix, &key, (&[0], 0));
assert!(result.is_ok());
}
assert_eq!(index.read_value(&key), Some((vec![0], 0)));
}
}

#[test]
fn bucket_map_test_update2() {
let key = Pubkey::new_unique();
let config = BucketMapConfig::new(1 << 1);
let index = BucketMap::new(config);
index.insert(&key, (&[0], 0));
index.insert(index.bucket_ix(&key), &key, (&[0], 0));
assert_eq!(index.read_value(&key), Some((vec![0], 0)));
index.insert(&key, (&[1], 0));
index.insert(index.bucket_ix(&key), &key, (&[1], 0));
assert_eq!(index.read_value(&key), Some((vec![1], 0)));
}

Expand Down Expand Up @@ -475,7 +505,7 @@ mod tests {
let insert = thread_rng().gen_range(0, 2) == 0;
maps.iter().for_each(|map| {
if insert {
map.insert(&k, (&v.0, v.1))
map.insert(map.bucket_ix(&k), &k, (&v.0, v.1))
} else {
map.update(&k, |current| {
assert!(current.is_none());
Expand All @@ -494,7 +524,7 @@ mod tests {
let insert = thread_rng().gen_range(0, 2) == 0;
maps.iter().for_each(|map| {
if insert {
map.insert(&k, (&v, rc))
map.insert(map.bucket_ix(&k), &k, (&v, rc))
} else {
map.update(&k, |current| {
assert_eq!(current, v_old.map(|(v, rc)| (&v[..], *rc)), "{}", k);
Expand Down
2 changes: 1 addition & 1 deletion runtime/src/in_mem_accounts_index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -690,7 +690,7 @@ impl<T: IndexValue> InMemAccountsIndex<T> {
continue; // marked dirty after we grabbed it above, so handle this the next time this bucket is flushed
}
flush_entries_updated_on_disk += 1;
disk.insert(&k, (&v.slot_list.read().unwrap(), v.ref_count()));
disk.insert(self.bin, &k, (&v.slot_list.read().unwrap(), v.ref_count()));
}
Self::update_time_stat(&self.stats().flush_update_us, m);
Self::update_stat(
Expand Down

0 comments on commit 5e05f12

Please sign in to comment.