Skip to content

Commit

Permalink
Refactor to reduce unnecessary cloning
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
shuhuiluo committed Sep 3, 2024
1 parent 0dfcd2e commit 9a33332
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
}
12 changes: 6 additions & 6 deletions src/entities/base_currency.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>;
fn symbol(&self) -> Option<&String>;

/// The name of the currency, i.e. a descriptive textual non-unique identifier
fn name(&self) -> Option<String>;
fn name(&self) -> Option<&String>;
}

impl<M: Clone> BaseCurrency for CurrencyLike<M> {
Expand All @@ -46,12 +46,12 @@ impl<M: Clone> BaseCurrency for CurrencyLike<M> {
self.decimals
}

fn symbol(&self) -> Option<String> {
self.symbol.clone()
fn symbol(&self) -> Option<&String> {
self.symbol.as_ref()
}

fn name(&self) -> Option<String> {
self.name.clone()
fn name(&self) -> Option<&String> {
self.name.as_ref()
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/entities/fractions/currency_amount.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ impl<T: Currency> CurrencyAmount<T> {
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();
Expand Down
20 changes: 10 additions & 10 deletions src/entities/fractions/price.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
}

Expand Down Expand Up @@ -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,
))
}

Expand All @@ -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,
)
}

Expand Down Expand Up @@ -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]
Expand All @@ -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]
Expand Down

0 comments on commit 9a33332

Please sign in to comment.