computer: stateful: perf improvements

This commit is contained in:
nym21
2025-07-11 11:43:53 +02:00
parent 4489920cbf
commit 8c289df336
7 changed files with 220 additions and 122 deletions

View File

@@ -18,7 +18,7 @@ impl AddressData {
}
pub fn realized_price(&self) -> Dollars {
(self.realized_cap / Bitcoin::from(self.amount())).round_nearest_cent()
(self.realized_cap / Bitcoin::from(self.amount())).round_to_4_digits()
}
#[inline(always)]

View File

@@ -24,10 +24,28 @@ use super::Dollars;
)]
pub struct Cents(i64);
const SIGNIFICANT_DIGITS: i32 = 4;
impl Cents {
pub const fn mint(value: i64) -> Self {
Self(value)
}
pub fn round_to_4_digits(self) -> Self {
let v = self.0;
let ilog10 = v.checked_ilog10().unwrap_or(0) as i32;
Self::from(if ilog10 >= SIGNIFICANT_DIGITS {
let log_diff = ilog10 - SIGNIFICANT_DIGITS + 1;
let pow = 10.0_f64.powi(log_diff);
((v as f64 / pow).round() * pow) as i64
} else {
v
})
}
}
impl From<Dollars> for Cents {

View File

@@ -42,6 +42,10 @@ impl Dollars {
pub fn round_nearest_cent(self) -> Self {
Dollars((self.0 * 100.0).round() / 100.0)
}
pub fn round_to_4_digits(self) -> Self {
Self::from(Cents::from(self).round_to_4_digits())
}
}
impl From<f32> for Dollars {