From 9a33332acb99ddf3a46363538c40f59feb07b30a Mon Sep 17 00:00:00 2001 From: Shuhui Luo <107524008+shuhuiluo@users.noreply.github.com> Date: Tue, 3 Sep 2024 00:28:59 -0400 Subject: [PATCH] Refactor to reduce unnecessary cloning Updated methods to return references instead of owned values and removed redundant clone operations. This should reduce unnecessary heap allocations and improve performance by minimizing malloc overhead. --- src/constants.rs | 2 +- src/entities/base_currency.rs | 12 ++++++------ src/entities/fractions/currency_amount.rs | 2 +- src/entities/fractions/price.rs | 20 ++++++++++---------- 4 files changed, 18 insertions(+), 18 deletions(-) diff --git a/src/constants.rs b/src/constants.rs index 414f109..e410cca 100644 --- a/src/constants.rs +++ b/src/constants.rs @@ -28,5 +28,5 @@ pub enum Rounding { lazy_static! { /// Represents the maximum amount contained in a uint256 pub static ref MAX_UINT256: BigInt = - BigInt::from_bytes_be(Sign::Plus, &U256::MAX.to_be_bytes::<32>()); + BigInt::from_biguint(Sign::Plus, BigUint::from_bytes_le(&U256::MAX.as_le_bytes())); } diff --git a/src/entities/base_currency.rs b/src/entities/base_currency.rs index e1ff168..f5efa01 100644 --- a/src/entities/base_currency.rs +++ b/src/entities/base_currency.rs @@ -31,10 +31,10 @@ pub trait BaseCurrency: Clone { fn decimals(&self) -> u8; /// The symbol of the currency, i.e. a short textual non-unique identifier - fn symbol(&self) -> Option; + fn symbol(&self) -> Option<&String>; /// The name of the currency, i.e. a descriptive textual non-unique identifier - fn name(&self) -> Option; + fn name(&self) -> Option<&String>; } impl BaseCurrency for CurrencyLike { @@ -46,12 +46,12 @@ impl BaseCurrency for CurrencyLike { self.decimals } - fn symbol(&self) -> Option { - self.symbol.clone() + fn symbol(&self) -> Option<&String> { + self.symbol.as_ref() } - fn name(&self) -> Option { - self.name.clone() + fn name(&self) -> Option<&String> { + self.name.as_ref() } } diff --git a/src/entities/fractions/currency_amount.rs b/src/entities/fractions/currency_amount.rs index fde33b0..b2eff80 100644 --- a/src/entities/fractions/currency_amount.rs +++ b/src/entities/fractions/currency_amount.rs @@ -23,7 +23,7 @@ impl CurrencyAmount { let numerator = numerator.into(); let denominator = denominator.into(); // Ensure the amount does not exceed MAX_UINT256 - if !numerator.div_floor(&denominator).le(&MAX_UINT256) { + if numerator.div_floor(&denominator) > *MAX_UINT256 { return Err(Error::MaxUint); } let exponent = currency.decimals(); diff --git a/src/entities/fractions/price.rs b/src/entities/fractions/price.rs index 5e5d2d9..bc965b1 100644 --- a/src/entities/fractions/price.rs +++ b/src/entities/fractions/price.rs @@ -58,8 +58,8 @@ where Self::new( base_amount.meta.currency, quote_amount.meta.currency, - res.denominator().clone(), - res.numerator().clone(), + res.denominator, + res.numerator, ) } @@ -87,8 +87,8 @@ where Ok(Price::new( self.base_currency.clone(), other.quote_currency.clone(), - fraction.denominator().clone(), - fraction.numerator().clone(), + fraction.denominator, + fraction.numerator, )) } @@ -103,8 +103,8 @@ where let fraction = self.as_fraction() * currency_amount.as_fraction(); CurrencyAmount::from_fractional_amount( self.quote_currency.clone(), - fraction.numerator().clone(), - fraction.denominator().clone(), + fraction.numerator, + fraction.denominator, ) } @@ -153,8 +153,8 @@ mod test { price.to_significant(5, Rounding::RoundDown).unwrap(), "54321" ); - assert!(price.clone().base_currency.equals(&TOKEN0.clone())); - assert!(price.clone().quote_currency.equals(&TOKEN1.clone())); + assert!(price.base_currency.equals(&TOKEN0.clone())); + assert!(price.quote_currency.equals(&TOKEN1.clone())); } #[test] @@ -167,8 +167,8 @@ mod test { price.to_significant(5, Rounding::RoundDown).unwrap(), "54321" ); - assert!(price.clone().base_currency.equals(&TOKEN0.clone())); - assert!(price.clone().quote_currency.equals(&TOKEN1.clone())); + assert!(price.base_currency.equals(&TOKEN0.clone())); + assert!(price.quote_currency.equals(&TOKEN1.clone())); } #[test]