Skip to content

Commit

Permalink
Resolving Performance Degredation by removing formatted output
Browse files Browse the repository at this point in the history
  • Loading branch information
roryharr committed Dec 3, 2024
1 parent 3c0e51e commit d2b089e
Showing 1 changed file with 5 additions and 11 deletions.
16 changes: 5 additions & 11 deletions programs/stake/src/rewards.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,17 +185,12 @@ fn calculate_stake_rewards(

let rewards = points
.checked_mul(u128::from(point_value.rewards))
.expect(&format!(
"Rewards should fit within u128, inputs were {} and {}",
points, point_value.rewards
));
.expect("Rewards intermittent calculation should fit within u128");

// The unwrap is safe, as points_value.points is guaranteed to be non zero above.
// Expect is verifying that the result fits in u64
let rewards = u64::try_from(rewards.checked_div(point_value.points).unwrap()).expect(&format!(
"Rewards should fit within u64, inputs were {}, {} and {}",
points, point_value.rewards, point_value.points
));
let rewards = u64::try_from(rewards.checked_div(point_value.points).unwrap())
.expect("Rewards should fit within u64");

// don't bother trying to split if fractional lamports got truncated
if rewards == 0 {
Expand Down Expand Up @@ -236,8 +231,7 @@ mod tests {
use {
super::*,
crate::{points::null_tracer, stake_state::new_stake},
solana_sdk::{native_token, pubkey::Pubkey},
std::u64,
solana_sdk::{native_token, pubkey::Pubkey},
test_case::test_case,
};

Expand Down Expand Up @@ -618,7 +612,7 @@ mod tests {
);
}

#[test_case(u64::MAX, 1_000, u64::MAX => panics "Rewards should fit within u128")]
#[test_case(u64::MAX, 1_000, u64::MAX => panics "Rewards intermittent calculation should fit within u128")]
#[test_case(1, u64::MAX, u64::MAX => panics "Rewards should fit within u64")]
fn calculate_rewards_tests(stake: u64, rewards: u64, credits: u64) {
let mut vote_state = VoteState::default();
Expand Down

0 comments on commit d2b089e

Please sign in to comment.