global: perf + resource imprv

This commit is contained in:
nym21
2025-07-12 15:07:02 +02:00
parent 0167a2ae59
commit b24a29895f
18 changed files with 274 additions and 417 deletions

View File

@@ -21,12 +21,12 @@ impl AddressData {
(self.realized_cap / Bitcoin::from(self.amount())).round_to_4_digits()
}
#[inline(always)]
#[inline]
pub fn has_0_sats(&self) -> bool {
self.amount() == Sats::ZERO
}
#[inline(always)]
#[inline]
pub fn has_0_utxos(&self) -> bool {
self.outputs_len == 0
}

View File

@@ -243,7 +243,7 @@ impl Mul<Sats> for Dollars {
self
} else {
Self::from(Cents::from(
u128::from(rhs) * u128::from(Cents::from(self)) / u128::from(Sats::ONE_BTC),
u128::from(rhs) * u128::from(Cents::from(self)) / Sats::ONE_BTC_U128,
))
}
}

View File

@@ -53,6 +53,7 @@ impl Sats {
pub const ONE_BTC: Self = Self(1_00_000_000);
pub const MAX: Self = Self(u64::MAX);
pub const FIFTY_BTC: Self = Self(50_00_000_000);
pub const ONE_BTC_U128: u128 = 1_00_000_000;
pub fn new(sats: u64) -> Self {
Self(sats)

View File

@@ -1,7 +1,4 @@
use std::{
cmp::Ordering,
ops::{Add, AddAssign, Div},
};
use std::ops::{Add, AddAssign, Div};
use derive_deref::Deref;
use jiff::{civil::date, tz::TimeZone};
@@ -29,7 +26,9 @@ use super::Date;
)]
pub struct Timestamp(u32);
const ONE_DAY_IN_SEC: i64 = 24 * 60 * 60;
const ONE_HOUR_IN_SEC: u32 = 60 * 60;
const ONE_DAY_IN_SEC: u32 = 24 * 60 * 60;
const ONE_DAY_IN_SEC_F64: f64 = ONE_DAY_IN_SEC as f64;
impl Timestamp {
pub const ZERO: Self = Self(0);
@@ -50,34 +49,25 @@ impl Timestamp {
Self::from(trunc_date_time.to_zoned(TimeZone::UTC).unwrap().timestamp())
}
pub fn difference_in_days_between(&self, other: Self) -> usize {
match self.cmp(&other) {
Ordering::Equal => 0,
Ordering::Greater => other.difference_in_days_between(*self),
Ordering::Less => {
(jiff::Timestamp::from(*self)
.duration_until(jiff::Timestamp::from(other))
.as_secs()
/ ONE_DAY_IN_SEC) as usize
}
}
#[inline]
pub fn difference_in_days_between(&self, older: Self) -> usize {
// if self.0 < older.0 {
// unreachable!()
// }
((self.0 - older.0) / ONE_DAY_IN_SEC) as usize
}
pub fn difference_in_days_between_float(&self, other: Self) -> f64 {
match self.cmp(&other) {
Ordering::Equal => 0.0,
Ordering::Greater => other.difference_in_days_between_float(*self),
Ordering::Less => {
jiff::Timestamp::from(*self)
.duration_until(jiff::Timestamp::from(other))
.as_secs() as f64
/ ONE_DAY_IN_SEC as f64
}
}
#[inline]
pub fn difference_in_days_between_float(&self, older: Self) -> f64 {
// if self.0 < older.0 {
// unreachable!()
// }
(self.0 - older.0) as f64 / ONE_DAY_IN_SEC_F64
}
#[inline]
pub fn is_more_than_hour(&self) -> bool {
jiff::Timestamp::from(*self).as_second() >= 60 * 60
self.0 >= ONE_HOUR_IN_SEC
}
}