Skip to content

Commit 57ea0f6

Browse files
authored
clippy: manual_div_ceil (solana-labs#4376)
1 parent 927c6d3 commit 57ea0f6

File tree

12 files changed

+15
-17
lines changed

12 files changed

+15
-17
lines changed

accounts-db/src/ancient_append_vecs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1207,7 +1207,7 @@ fn div_ceil(x: u64, y: NonZeroU64) -> u64 {
12071207
// SAFETY: Since `y` is NonZero:
12081208
// - we know the denominator is > 0, and thus safe (cannot have divide-by-zero)
12091209
// - we know `x + y` is non-zero, and thus the numerator is safe (cannot underflow)
1210-
(x + y - 1) / y
1210+
x.div_ceil(y)
12111211
}
12121212

12131213
#[cfg(test)]

bench-tps/src/main.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -210,9 +210,8 @@ fn main() {
210210
let max_fee = FeeRateGovernor::new(*target_lamports_per_signature, 0)
211211
.max_lamports_per_signature
212212
.saturating_add(max_lamports_for_prioritization(compute_unit_price));
213-
let num_lamports_per_account = (num_accounts - 1 + NUM_SIGNATURES_FOR_TXS * max_fee)
214-
/ num_accounts
215-
+ num_lamports_per_account;
213+
let num_lamports_per_account =
214+
(NUM_SIGNATURES_FOR_TXS * max_fee).div_ceil(num_accounts) + num_lamports_per_account;
216215
let mut accounts = HashMap::new();
217216
keypairs.iter().for_each(|keypair| {
218217
accounts.insert(

core/src/sigverify_stage.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ impl SigVerifyStage {
255255
while max_packets > 0 && !addrs.is_empty() {
256256
let num_addrs = addrs.len();
257257
addrs.retain(|_, packets| {
258-
let cap = (max_packets + num_addrs - 1) / num_addrs;
258+
let cap = max_packets.div_ceil(num_addrs);
259259
max_packets -= packets.len().min(cap);
260260
packets.truncate(packets.len().saturating_sub(cap));
261261
!packets.is_empty()

core/src/validator.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1892,7 +1892,7 @@ fn load_genesis(
18921892
// grows too large
18931893
let leader_schedule_slot_offset = genesis_config.epoch_schedule.leader_schedule_slot_offset;
18941894
let slots_per_epoch = genesis_config.epoch_schedule.slots_per_epoch;
1895-
let leader_epoch_offset = (leader_schedule_slot_offset + slots_per_epoch - 1) / slots_per_epoch;
1895+
let leader_epoch_offset = leader_schedule_slot_offset.div_ceil(slots_per_epoch);
18961896
assert!(leader_epoch_offset <= MAX_LEADER_SCHEDULE_EPOCH_OFFSET);
18971897

18981898
let genesis_hash = genesis_config.hash();

entry/src/entry.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -690,7 +690,7 @@ impl EntrySlice for [Entry] {
690690
transactions: vec![],
691691
}];
692692

693-
let aligned_len = ((self.len() + simd_len - 1) / simd_len) * simd_len;
693+
let aligned_len = self.len().div_ceil(simd_len) * simd_len;
694694
let mut hashes_bytes = vec![0u8; HASH_BYTES * aligned_len];
695695
genesis
696696
.iter()

gossip/src/crds_gossip_pull.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ impl CrdsFilterSet {
155155
let mask_bits = CrdsFilter::mask_bits(num_items as f64, max_items);
156156
let mut filters: Vec<_> = repeat_with(|| None).take(1usize << mask_bits).collect();
157157
let mut indices: Vec<_> = (0..filters.len()).collect();
158-
let size = (filters.len() + SAMPLE_RATE - 1) / SAMPLE_RATE;
158+
let size = filters.len().div_ceil(SAMPLE_RATE);
159159
for _ in 0..MAX_NUM_FILTERS.min(size) {
160160
let k = rng.gen_range(0..indices.len());
161161
let k = indices.swap_remove(k);

gossip/src/protocol.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -553,7 +553,7 @@ pub(crate) mod tests {
553553
let num_values_per_payload = (PUSH_MESSAGE_MAX_PAYLOAD_SIZE / value_size).max(1);
554554

555555
// Expected len is the ceiling of the division
556-
let expected_len = (NUM_VALUES + num_values_per_payload - 1) / num_values_per_payload;
556+
let expected_len = NUM_VALUES.div_ceil(num_values_per_payload);
557557
let msgs = vec![value; NUM_VALUES];
558558

559559
assert!(split_gossip_messages(PUSH_MESSAGE_MAX_PAYLOAD_SIZE, msgs).count() <= expected_len);

gossip/src/weighted_shuffle.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ fn get_num_nodes_and_tree_size(count: usize) -> (/*num_nodes:*/ usize, /*tree_si
220220
size += nodes;
221221
nodes *= FANOUT;
222222
}
223-
(size + nodes, size + (count + FANOUT - 1) / FANOUT)
223+
(size + nodes, size + count.div_ceil(FANOUT))
224224
}
225225

226226
#[cfg(test)]

ledger/src/shred/merkle.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1103,7 +1103,7 @@ pub(super) fn make_shreds_from_data(
11031103
let (proof_size, data_buffer_size, num_data_shreds) = (1u8..32)
11041104
.find_map(|proof_size| {
11051105
let data_buffer_size = ShredData::capacity(proof_size, chained, resigned).ok()?;
1106-
let num_data_shreds = (data.len() + data_buffer_size - 1) / data_buffer_size;
1106+
let num_data_shreds = data.len().div_ceil(data_buffer_size);
11071107
let num_data_shreds = num_data_shreds.max(min_num_data_shreds);
11081108
let erasure_batch_size =
11091109
shredder::get_erasure_batch_size(num_data_shreds, is_last_in_slot);

ledger/src/shredder.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ impl Shredder {
136136
process_stats.data_buffer_residual +=
137137
(data_buffer_size - serialized_shreds.len() % data_buffer_size) % data_buffer_size;
138138
// Integer division to ensure we have enough shreds to fit all the data
139-
let num_shreds = (serialized_shreds.len() + data_buffer_size - 1) / data_buffer_size;
139+
let num_shreds = serialized_shreds.len().div_ceil(data_buffer_size);
140140
let last_shred_index = next_shred_index + num_shreds as u32 - 1;
141141
// 1) Generate data shreds
142142
let make_data_shred = |data, shred_index: u32, fec_set_index: u32| {
@@ -471,7 +471,7 @@ fn get_fec_set_offsets(
471471
return None;
472472
}
473473
let num_chunks = (num_shreds / min_chunk_size).max(1);
474-
let chunk_size = (num_shreds + num_chunks - 1) / num_chunks;
474+
let chunk_size = num_shreds.div_ceil(num_chunks);
475475
let offsets = std::iter::repeat(offset).take(chunk_size);
476476
num_shreds -= chunk_size;
477477
offset += chunk_size;
@@ -541,7 +541,7 @@ mod tests {
541541
let size = serialized_size(&entries).unwrap() as usize;
542542
// Integer division to ensure we have enough shreds to fit all the data
543543
let data_buffer_size = ShredData::capacity(/*merkle_proof_size:*/ None).unwrap();
544-
let num_expected_data_shreds = (size + data_buffer_size - 1) / data_buffer_size;
544+
let num_expected_data_shreds = size.div_ceil(data_buffer_size);
545545
let num_expected_data_shreds = num_expected_data_shreds.max(if is_last_in_slot {
546546
DATA_SHREDS_PER_FEC_BLOCK
547547
} else {

0 commit comments

Comments
 (0)