From 70f96bda25744cd7a7b3e5b11e072cd595a4672c Mon Sep 17 00:00:00 2001 From: "Jeff Washington (jwash)" Date: Fri, 17 Dec 2021 10:16:34 -0600 Subject: [PATCH] disk buckets: refactor (#21972) --- bucket_map/src/bucket.rs | 14 +++++--------- bucket_map/src/index_entry.rs | 23 +++++++++++++++++++++-- 2 files changed, 26 insertions(+), 11 deletions(-) diff --git a/bucket_map/src/bucket.rs b/bucket_map/src/bucket.rs index d3ff2e0e56..ec1df8a8c3 100644 --- a/bucket_map/src/bucket.rs +++ b/bucket_map/src/bucket.rs @@ -198,14 +198,10 @@ impl Bucket { continue; } index.allocate(ii, elem_uid, is_resizing).unwrap(); - let mut elem: &mut IndexEntry = index.get_mut(ii); - elem.key = *key; - // These will be overwritten after allocation by callers. + let elem: &mut IndexEntry = index.get_mut(ii); + // These fields will be overwritten after allocation by callers. // Since this part of the mmapped file could have previously been used by someone else, there can be garbage here. - elem.ref_count = 0; - elem.storage_offset = 0; - elem.storage_capacity_when_created_pow2 = 0; - elem.num_slots = 0; + elem.init(key); //debug!( "INDEX ALLOC {:?} {} {} {}", key, ii, index.capacity, elem_uid ); return Ok(ii); } @@ -285,8 +281,8 @@ impl Bucket { if elem.num_slots > 0 { current_bucket.free(elem_loc, elem_uid); } - elem.storage_offset = ix; - elem.storage_capacity_when_created_pow2 = best_bucket.capacity_pow2; + elem.set_storage_offset(ix); + elem.set_storage_capacity_when_created_pow2(best_bucket.capacity_pow2); elem.num_slots = data.len() as u64; //debug!( "DATA ALLOC {:?} {} {} {}", key, elem.data_location, best_bucket.capacity, elem_uid ); if elem.num_slots > 0 { diff --git a/bucket_map/src/index_entry.rs b/bucket_map/src/index_entry.rs index 9fc540627a..201c564074 100644 --- a/bucket_map/src/index_entry.rs +++ b/bucket_map/src/index_entry.rs @@ -19,13 +19,32 @@ use { pub struct IndexEntry { pub key: Pubkey, // can this be smaller if we have reduced the keys into buckets already? pub ref_count: RefCount, // can this be smaller? Do we ever need more than 4B refcounts? - pub storage_offset: u64, // smaller? since these are variably sized, this could get tricky. well, actually accountinfo is not variable sized... + storage_offset: u64, // smaller? since these are variably sized, this could get tricky. well, actually accountinfo is not variable sized... // if the bucket doubled, the index can be recomputed using create_bucket_capacity_pow2 - pub storage_capacity_when_created_pow2: u8, // see data_location + storage_capacity_when_created_pow2: u8, // see data_location pub num_slots: Slot, // can this be smaller? epoch size should ~ be the max len. this is the num elements in the slot list } impl IndexEntry { + pub fn init(&mut self, pubkey: &Pubkey) { + self.key = *pubkey; + self.ref_count = 0; + self.storage_offset = 0; + self.storage_capacity_when_created_pow2 = 0; + self.num_slots = 0; + } + + pub fn set_storage_capacity_when_created_pow2( + &mut self, + storage_capacity_when_created_pow2: u8, + ) { + self.storage_capacity_when_created_pow2 = storage_capacity_when_created_pow2; + } + + pub fn set_storage_offset(&mut self, storage_offset: u64) { + self.storage_offset = storage_offset; + } + pub fn data_bucket_from_num_slots(num_slots: Slot) -> u64 { (num_slots as f64).log2().ceil() as u64 // use int log here? }