Skip to content

Commit

Permalink
Add inline attributes to improve function call performance
Browse files Browse the repository at this point in the history
Annotated various functions and methods with #[inline] to suggest the compiler perform inline expansion, potentially enhancing performance for frequently called operations. This change spans across multiple modules, including token, weth9, base_currency, currency_amount, percent, fraction, price, compute_price_impact, and ether.
  • Loading branch information
shuhuiluo committed Sep 3, 2024
1 parent 9a33332 commit d71763f
Show file tree
Hide file tree
Showing 9 changed files with 68 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/entities/base_currency.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,22 @@ pub trait BaseCurrency: Clone {
}

impl<M: Clone> BaseCurrency for CurrencyLike<M> {
#[inline]
fn chain_id(&self) -> ChainId {
self.chain_id
}

#[inline]
fn decimals(&self) -> u8 {
self.decimals
}

#[inline]
fn symbol(&self) -> Option<&String> {
self.symbol.as_ref()
}

#[inline]
fn name(&self) -> Option<&String> {
self.name.as_ref()
}
Expand All @@ -59,6 +63,7 @@ impl<M: Clone> BaseCurrency for CurrencyLike<M> {
impl<M> Deref for CurrencyLike<M> {
type Target = M;

#[inline]
fn deref(&self) -> &Self::Target {
&self.meta
}
Expand Down
6 changes: 6 additions & 0 deletions src/entities/ether.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,19 @@ pub type Ether = CurrencyLike<()>;

impl Currency for Ether {
/// Checks if the currency is native to the blockchain.
#[inline]
fn is_native(&self) -> bool {
true
}

/// Retrieves the address associated with the currency.
#[inline]
fn address(&self) -> Address {
self.wrapped().address()
}

/// Checks if the currency is equal to another currency.
#[inline]
fn equals(&self, other: &impl Currency) -> bool {
match other.is_native() {
true => self.chain_id() == other.chain_id(),
Expand All @@ -24,6 +27,7 @@ impl Currency for Ether {
}

/// Returns the wrapped token representation of the currency.
#[inline]
fn wrapped(&self) -> Token {
match WETH9::default().get(self.chain_id()) {
Some(weth9) => weth9.clone(),
Expand All @@ -34,6 +38,7 @@ impl Currency for Ether {

impl Ether {
/// Creates a new instance of [`Ether`] with the specified chain ID.
#[inline]
pub fn new(chain_id: u64) -> Self {
Self {
chain_id,
Expand All @@ -45,6 +50,7 @@ impl Ether {
}

/// Retrieves or creates an [`Ether`] instance for the specified chain ID.
#[inline]
pub fn on_chain(chain_id: u64) -> Self {
Self::new(chain_id)
}
Expand Down
11 changes: 11 additions & 0 deletions src/entities/fractions/currency_amount.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub struct CurrencyMeta<T: Currency> {

impl<T: Currency> CurrencyAmount<T> {
/// Constructor method for creating a new currency amount
#[inline]
fn new(
currency: T,
numerator: impl Into<BigInt>,
Expand All @@ -38,11 +39,13 @@ impl<T: Currency> CurrencyAmount<T> {
}

/// Returns a new currency amount instance from the unitless amount of token (raw amount)
#[inline]
pub fn from_raw_amount(currency: T, raw_amount: impl Into<BigInt>) -> Result<Self, Error> {
Self::new(currency, raw_amount, 1)
}

/// Construct a currency amount with a denominator that is not equal to 0
#[inline]
pub fn from_fractional_amount(
currency: T,
numerator: impl Into<BigInt>,
Expand All @@ -52,6 +55,7 @@ impl<T: Currency> CurrencyAmount<T> {
}

/// Multiplication of currency amount by another fractional amount
#[inline]
pub fn multiply<M: Clone>(&self, other: &impl FractionBase<M>) -> Result<Self, Error> {
let multiplied = self.as_fraction() * other.as_fraction();
Self::from_fractional_amount(
Expand All @@ -62,6 +66,7 @@ impl<T: Currency> CurrencyAmount<T> {
}

/// Division of currency amount by another fractional amount
#[inline]
pub fn divide<M: Clone>(&self, other: &impl FractionBase<M>) -> Result<Self, Error> {
let divided = self.as_fraction() / other.as_fraction();
Self::from_fractional_amount(
Expand All @@ -72,13 +77,15 @@ impl<T: Currency> CurrencyAmount<T> {
}

/// Convert the currency amount to a string with exact precision
#[inline]
pub fn to_exact(&self) -> String {
BigDecimal::from(self.quotient())
.div(BigDecimal::from(BigInt::from(self.decimal_scale.clone())))
.to_string()
}

/// Addition of another currency amount to the current amount
#[inline]
pub fn add(&self, other: &Self) -> Result<Self, Error> {
if !self.currency.equals(&other.currency) {
return Err(Error::NotEqual);
Expand All @@ -88,6 +95,7 @@ impl<T: Currency> CurrencyAmount<T> {
}

/// Subtraction of another currency amount from the current amount
#[inline]
pub fn subtract(&self, other: &Self) -> Result<Self, Error> {
if !self.currency.equals(&other.currency) {
return Err(Error::NotEqual);
Expand All @@ -101,6 +109,7 @@ impl<T: Currency> CurrencyAmount<T> {
}

/// Convert the currency amount to a string with a specified number of significant digits
#[inline]
pub fn to_significant(
&self,
significant_digits: u8,
Expand All @@ -111,6 +120,7 @@ impl<T: Currency> CurrencyAmount<T> {
}

/// Convert the currency amount to a string with a fixed number of decimal places
#[inline]
pub fn to_fixed(&self, decimal_places: u8, rounding: Rounding) -> Result<String, Error> {
if decimal_places > self.currency.decimals() {
return Err(Error::NotEqual);
Expand All @@ -128,6 +138,7 @@ impl<T: Currency> CurrencyAmount<T> {
}

/// Wrap the currency amount if the currency is not native
#[inline]
pub fn wrapped(&self) -> Result<CurrencyAmount<Token>, Error> {
CurrencyAmount::from_fractional_amount(
self.currency.wrapped(),
Expand Down
26 changes: 26 additions & 0 deletions src/entities/fractions/fraction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub struct FractionLike<M> {
}

impl<M: Default> Default for FractionLike<M> {
#[inline]
fn default() -> Self {
Self {
numerator: BigInt::from(0),
Expand All @@ -24,6 +25,7 @@ impl<M: Default> Default for FractionLike<M> {
impl<M> Deref for FractionLike<M> {
type Target = M;

#[inline]
fn deref(&self) -> &Self::Target {
&self.meta
}
Expand All @@ -47,12 +49,14 @@ impl Fraction {
/// # Panics
///
/// This function will panic if the denominator is zero.
#[inline]
pub fn new(numerator: impl Into<BigInt>, denominator: impl Into<BigInt>) -> Self {
FractionBase::new(numerator, denominator, ())
}
}

/// Function to convert the custom Rounding enum to [`bigdecimal`]'s [`RoundingMode`]
#[inline]
const fn to_rounding_strategy(rounding: Rounding) -> RoundingMode {
match rounding {
Rounding::RoundDown => RoundingMode::Down,
Expand Down Expand Up @@ -88,11 +92,13 @@ pub trait FractionBase<M: Clone>: Sized {
fn denominator(&self) -> &BigInt;

/// Returns the floor division quotient of the fraction
#[inline]
fn quotient(&self) -> BigInt {
self.numerator().div_floor(self.denominator())
}

/// Returns the remainder after floor division as a new fraction
#[inline]
fn remainder(&self) -> Self {
Self::new(
self.numerator() % self.denominator(),
Expand All @@ -102,6 +108,7 @@ pub trait FractionBase<M: Clone>: Sized {
}

/// Returns the inverted fraction
#[inline]
fn invert(&self) -> Self {
Self::new(
self.denominator().clone(),
Expand All @@ -111,12 +118,14 @@ pub trait FractionBase<M: Clone>: Sized {
}

/// Converts the fraction to a [`BigDecimal`]
#[inline]
fn to_decimal(&self) -> BigDecimal {
BigDecimal::from(self.numerator().clone()).div(BigDecimal::from(self.denominator().clone()))
}

/// Converts the fraction to a string with a specified number of significant digits and rounding
/// strategy
#[inline]
fn to_significant(&self, significant_digits: u8, rounding: Rounding) -> Result<String, Error> {
if significant_digits == 0 {
return Err(Error::Invalid);
Expand All @@ -132,6 +141,7 @@ pub trait FractionBase<M: Clone>: Sized {

/// Converts the fraction to a string with a fixed number of decimal places and rounding
/// strategy
#[inline]
fn to_fixed(&self, decimal_places: u8, rounding: Rounding) -> String {
let rounding_strategy = to_rounding_strategy(rounding);
self.to_decimal()
Expand All @@ -140,6 +150,7 @@ pub trait FractionBase<M: Clone>: Sized {
}

/// Helper method for converting any superclass back to a simple [`Fraction`]
#[inline]
fn as_fraction(&self) -> Fraction {
Fraction::new(self.numerator().clone(), self.denominator().clone())
}
Expand All @@ -149,6 +160,7 @@ impl<M: Clone + PartialEq> FractionTrait<M> for FractionLike<M> {}

impl<M: Clone> FractionBase<M> for FractionLike<M> {
/// Constructor for creating a new [`FractionLike`] with metadata
#[inline]
fn new(numerator: impl Into<BigInt>, denominator: impl Into<BigInt>, meta: M) -> Self {
let denominator = denominator.into();
// Ensure the denominator is not zero
Expand All @@ -163,16 +175,19 @@ impl<M: Clone> FractionBase<M> for FractionLike<M> {
}

/// Accessor method for retrieving metadata
#[inline]
fn meta(&self) -> &M {
&self.meta
}

/// Accessor method for retrieving the numerator
#[inline]
fn numerator(&self) -> &BigInt {
&self.numerator
}

/// Accessor method for retrieving the denominator
#[inline]
fn denominator(&self) -> &BigInt {
&self.denominator
}
Expand All @@ -183,6 +198,7 @@ where
M: Clone + PartialEq,
{
/// Checks if the current fraction is equal to another fraction
#[inline]
fn eq(&self, other: &Self) -> bool {
self.numerator() * other.denominator() == other.numerator() * self.denominator()
&& self.meta() == other.meta()
Expand All @@ -195,6 +211,7 @@ impl<M> Ord for FractionLike<M>
where
M: Clone + PartialEq,
{
#[inline]
fn cmp(&self, other: &Self) -> Ordering {
(self.numerator() * other.denominator()).cmp(&(other.numerator() * self.denominator()))
}
Expand All @@ -204,6 +221,7 @@ impl<M> PartialOrd<Self> for FractionLike<M>
where
M: Clone + PartialEq,
{
#[inline]
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
Expand All @@ -212,6 +230,7 @@ where
impl<M: Clone> Add for FractionLike<M> {
type Output = Self;

#[inline]
fn add(self, other: Self) -> Self::Output {
if self.denominator == other.denominator {
FractionBase::new(
Expand All @@ -232,6 +251,7 @@ impl<M: Clone> Add for FractionLike<M> {
impl<M: Clone> Add<&Self> for FractionLike<M> {
type Output = Self;

#[inline]
fn add(self, other: &Self) -> Self::Output {
if self.denominator == other.denominator {
FractionBase::new(
Expand All @@ -252,6 +272,7 @@ impl<M: Clone> Add<&Self> for FractionLike<M> {
impl<M: Clone> Sub for FractionLike<M> {
type Output = Self;

#[inline]
fn sub(self, other: Self) -> Self::Output {
if self.denominator == other.denominator {
FractionBase::new(
Expand All @@ -272,6 +293,7 @@ impl<M: Clone> Sub for FractionLike<M> {
impl<M: Clone> Sub<&Self> for FractionLike<M> {
type Output = Self;

#[inline]
fn sub(self, other: &Self) -> Self::Output {
if self.denominator == other.denominator {
FractionBase::new(
Expand All @@ -292,6 +314,7 @@ impl<M: Clone> Sub<&Self> for FractionLike<M> {
impl<M: Clone> Mul for FractionLike<M> {
type Output = Self;

#[inline]
fn mul(self, other: Self) -> Self::Output {
FractionBase::new(
self.numerator * other.numerator,
Expand All @@ -304,6 +327,7 @@ impl<M: Clone> Mul for FractionLike<M> {
impl<M: Clone> Mul<&Self> for FractionLike<M> {
type Output = Self;

#[inline]
fn mul(self, other: &Self) -> Self::Output {
FractionBase::new(
self.numerator * &other.numerator,
Expand All @@ -317,6 +341,7 @@ impl<M: Clone> Div for FractionLike<M> {
type Output = Self;

/// There's little to no possibility of an error, so unwrap can be used
#[inline]
fn div(self, other: Self) -> Self::Output {
FractionBase::new(
self.numerator * other.denominator,
Expand All @@ -330,6 +355,7 @@ impl<M: Clone> Div<&Self> for FractionLike<M> {
type Output = Self;

/// There's little to no possibility of an error, so unwrap can be used
#[inline]
fn div(self, other: &Self) -> Self::Output {
FractionBase::new(
self.numerator * &other.denominator,
Expand Down
3 changes: 3 additions & 0 deletions src/entities/fractions/percent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ pub type Percent = FractionLike<IsPercent>;

impl Percent {
/// Constructor for creating a new [`Percent`] instance
#[inline]
pub fn new(numerator: impl Into<BigInt>, denominator: impl Into<BigInt>) -> Self {
FractionBase::new(numerator, denominator, IsPercent)
}

/// Converts the [`Percent`] to a string with a specified number of significant digits and
/// rounding strategy
#[inline]
pub fn to_significant(
&self,
significant_digits: u8,
Expand All @@ -32,6 +34,7 @@ impl Percent {

/// Converts the [`Percent`] to a string with a fixed number of decimal places and rounding
/// strategy
#[inline]
pub fn to_fixed(&self, decimal_places: u8, rounding: Rounding) -> String {
// Convert the Percent to a simple Fraction, multiply by 100, and then call to_fixed on the
// result
Expand Down
Loading

0 comments on commit d71763f

Please sign in to comment.