Skip to content

Commit

Permalink
Round up correctly when truncating max ancient storages
Browse files Browse the repository at this point in the history
  • Loading branch information
brooksprumo committed Jun 18, 2024
1 parent d2b3476 commit bc38383
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion accounts-db/src/ancient_append_vecs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,15 @@ impl AncientSlotInfos {
/// The idea is that 'all_infos' is sorted from smallest capacity to largest,
/// but that isn't required for this function to be 'correct'.
fn truncate_to_max_storages(&mut self, tuning: &PackedAncientStorageTuning) {
fn div_round_up(x: u64, y: NonZeroU64) -> u64 {
debug_assert!(
x.checked_add(y.get()).is_some(),
"x + y overflowed! x: {x}, y: {y}",
);
// SAFETY: Since `y` is NonZero, subtracting 1 never wraps.
(x + (y.get() - 1)) / y
}

// these indexes into 'all_infos' are useless once we truncate 'all_infos', so make sure they're cleared out to avoid any issues
self.shrink_indexes.clear();
let total_storages = self.all_infos.len();
Expand All @@ -203,7 +212,7 @@ impl AncientSlotInfos {
for (i, info) in self.all_infos.iter().enumerate() {
cumulative_bytes += info.alive_bytes;
let ancient_storages_required =
(cumulative_bytes.0 / tuning.ideal_storage_size + 1) as usize;
div_round_up(cumulative_bytes.0, tuning.ideal_storage_size) as usize;
let storages_remaining = total_storages - i - 1;

// if the remaining uncombined storages and the # of resulting
Expand Down

0 comments on commit bc38383

Please sign in to comment.