Skip to content

Commit

Permalink
refactor: Use saturating subtraction for simplicity (#3880)
Browse files Browse the repository at this point in the history
We have nowadays saturating arithmetic implemented for `AmountOf`s, so
use this to simplify the code instead of having an `if-else` statement.
  • Loading branch information
dsarlis authored Feb 10, 2025
1 parent 527f4ad commit c309ff5
Showing 1 changed file with 2 additions and 7 deletions.
9 changes: 2 additions & 7 deletions rs/execution_environment/src/canister_settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use ic_types::{
ComputeAllocation, Cycles, InvalidComputeAllocationError, InvalidMemoryAllocationError,
MemoryAllocation, PrincipalId,
};
use num_traits::cast::ToPrimitive;
use num_traits::{cast::ToPrimitive, SaturatingSub};
use std::convert::TryFrom;

use crate::canister_manager::CanisterManagerError;
Expand Down Expand Up @@ -531,12 +531,7 @@ pub(crate) fn validate_canister_settings(
}
}

let allocated_bytes = if new_memory_bytes > old_memory_bytes {
new_memory_bytes - old_memory_bytes
} else {
NumBytes::new(0)
};

let allocated_bytes = new_memory_bytes.saturating_sub(&old_memory_bytes);
let reservation_cycles = cycles_account_manager.storage_reservation_cycles(
allocated_bytes,
subnet_memory_saturation,
Expand Down

0 comments on commit c309ff5

Please sign in to comment.