From eb54e544c28516f275a6255afe7a4e9d02e4bc9a Mon Sep 17 00:00:00 2001 From: "Jeff Washington (jwash)" <75863576+jeffwashington@users.noreply.github.com> Date: Thu, 11 Feb 2021 20:05:41 -0600 Subject: [PATCH] add div_ceil (#15267) --- runtime/src/accounts_db.rs | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/runtime/src/accounts_db.rs b/runtime/src/accounts_db.rs index cffb85702e..054c653300 100644 --- a/runtime/src/accounts_db.rs +++ b/runtime/src/accounts_db.rs @@ -3395,6 +3395,14 @@ impl AccountsDB { }) } + fn div_ceil(x: usize, y: usize) -> usize { + let mut result = x / y; + if x % y != 0 { + result += 1; + } + result + } + // For the first iteration, there could be more items in the tuple than just hash and lamports. // Using extractor allows us to avoid an unnecessary array copy on the first iteration. fn compute_merkle_root_and_capitalization_loop( @@ -3413,11 +3421,7 @@ impl AccountsDB { let mut time = Measure::start("time"); let total_hashes = hashes.len(); - // we need div_ceil here - let mut chunks = total_hashes / fanout; - if total_hashes % fanout != 0 { - chunks += 1; - } + let chunks = Self::div_ceil(total_hashes, fanout); let result: Vec<_> = (0..chunks) .into_par_iter() @@ -5181,6 +5185,21 @@ pub mod tests { ancestors } + #[test] + fn test_accountsdb_div_ceil() { + assert_eq!(AccountsDB::div_ceil(10, 3), 4); + assert_eq!(AccountsDB::div_ceil(0, 1), 0); + assert_eq!(AccountsDB::div_ceil(0, 5), 0); + assert_eq!(AccountsDB::div_ceil(9, 3), 3); + assert_eq!(AccountsDB::div_ceil(9, 9), 1); + } + + #[test] + #[should_panic(expected = "attempt to divide by zero")] + fn test_accountsdb_div_ceil_fail() { + assert_eq!(AccountsDB::div_ceil(10, 0), 0); + } + #[test] fn test_accountsdb_rest_of_hash_calculation() { solana_logger::setup();