Skip to content

Commit c071989

Browse files
avkr003amityadav0
andauthored
micro optimization for 10^n (#1277)
Co-authored-by: Amit Yadav <amy29981@gmail.com>
1 parent 28cb2ec commit c071989

File tree

6 files changed

+17
-13
lines changed

6 files changed

+17
-13
lines changed

utils/math.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ func Pow10(decimal uint64) osmomath.BigDec {
4040
return value
4141
}
4242

43+
// Pow10Int64 returns 10 raised to the power of the input decimal value as an int64.
44+
// Panics if decimal is greater than 18.
45+
// Way faster than Pow10 (100x)
4346
func Pow10Int64(decimal uint64) int64 {
4447
if decimal <= 18 {
4548
return pow10Int64Cache[decimal]

x/amm/keeper/estimate_price.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ func (k Keeper) CalculateUSDValue(ctx sdk.Context, denom string, amount math.Int
9090
return osmomath.BigDecFromSDKInt(amount).Mul(tokenPrice)
9191
}
9292

93+
// CalcAmmPrice Panics if decimal is > 18, but we do not support >18 as per AddAssetEntry in AssetProfile
9394
func (k Keeper) CalcAmmPrice(ctx sdk.Context, denom string, decimal uint64) osmomath.BigDec {
9495
usdcDenom, found := k.assetProfileKeeper.GetUsdcDenom(ctx)
9596
if !found || denom == usdcDenom {
@@ -102,7 +103,7 @@ func (k Keeper) CalcAmmPrice(ctx sdk.Context, denom string, decimal uint64) osmo
102103
}
103104

104105
routes := resp.InRoute
105-
tokenIn := sdk.NewCoin(denom, math.NewInt(utils.Pow10(decimal).TruncateInt64()))
106+
tokenIn := sdk.NewCoin(denom, math.NewInt(utils.Pow10Int64(decimal)))
106107
discount := osmomath.OneBigDec()
107108
spotPrice, _, _, _, _, _, _, _, err := k.CalcInRouteSpotPrice(ctx, tokenIn, routes, discount, osmomath.ZeroBigDec())
108109
if err != nil {

x/assetprofile/types/errors.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ var (
99
ErrAssetProfileNotFound = errors.Register(ModuleName, 1, "asset profile not found for denom")
1010
ErrChannelIdAndDenomHashMismatch = errors.Register(ModuleName, 2, "channel id and denom hash mismatch")
1111
ErrNotValidIbcDenom = errors.Register(ModuleName, 3, "not valid ibc denom")
12-
ErrDecimalsInvalid = errors.Register(ModuleName, 4, "decimals have to be a value between 6 and 18")
12+
ErrDecimalsInvalid = errors.Register(ModuleName, 4, "decimals have to be a value between 6 and 18") // utils.Pow10Int64 used everywhere for faster multiplication which panics if >18
1313
ErrInvalidBaseDenom = errors.Register(ModuleName, 5, "invalid base denom")
1414
)

x/masterchef/keeper/apr_denom.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ func (k Keeper) CalculateApr(ctx sdk.Context, query *types.QueryAprRequest) (osm
116116
if !found {
117117
return osmomath.ZeroBigDec(), assetprofiletypes.ErrAssetProfileNotFound
118118
}
119-
yearlyDexRewardAmount := usdcAmount.MulInt64(365).Quo(utils.Pow10(entry.Decimals))
119+
yearlyDexRewardAmount := usdcAmount.MulInt64(365).QuoInt64(utils.Pow10Int64(entry.Decimals))
120120

121121
apr := yearlyDexRewardAmount.
122122
Quo(edenDenomPrice).

x/perpetual/keeper/asset_price.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,11 @@ func (k Keeper) GetAssetPriceAndAssetUsdcDenomRatio(ctx sdk.Context, asset strin
3030
}
3131

3232
if info.Decimal < USDCInfo.Decimals {
33-
baseUnitMultiplier := utils.Pow10(USDCInfo.Decimals - info.Decimal)
34-
return price, price.Quo(USDCPrice).Mul(baseUnitMultiplier), nil
33+
baseUnitMultiplier := utils.Pow10Int64(USDCInfo.Decimals - info.Decimal)
34+
return price, price.Quo(USDCPrice).MulInt64(baseUnitMultiplier), nil
3535
} else {
36-
baseUnitMultiplier := utils.Pow10(info.Decimal - USDCInfo.Decimals)
37-
return price, price.Quo(USDCPrice).Quo(baseUnitMultiplier), nil
36+
baseUnitMultiplier := utils.Pow10Int64(info.Decimal - USDCInfo.Decimals)
37+
return price, price.Quo(USDCPrice).QuoInt64(baseUnitMultiplier), nil
3838
}
3939
}
4040

@@ -52,10 +52,10 @@ func (k Keeper) ConvertPriceToAssetUsdcDenomRatio(ctx sdk.Context, asset string,
5252
return osmomath.ZeroBigDec(), fmt.Errorf("error converting price to base units, asset price %s not found", ptypes.BaseCurrency)
5353
}
5454
if info.Decimal < USDCInfo.Decimals {
55-
baseUnitMultiplier := utils.Pow10(USDCInfo.Decimals - info.Decimal)
56-
return price.Quo(USDCPrice).Mul(baseUnitMultiplier), nil
55+
baseUnitMultiplier := utils.Pow10Int64(USDCInfo.Decimals - info.Decimal)
56+
return price.Quo(USDCPrice).MulInt64(baseUnitMultiplier), nil
5757
} else {
58-
baseUnitMultiplier := utils.Pow10(info.Decimal - USDCInfo.Decimals)
59-
return price.Quo(USDCPrice).Quo(baseUnitMultiplier), nil
58+
baseUnitMultiplier := utils.Pow10Int64(info.Decimal - USDCInfo.Decimals)
59+
return price.Quo(USDCPrice).QuoInt64(baseUnitMultiplier), nil
6060
}
6161
}

x/tier/keeper/query_get_consolidated_price.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ func (k Keeper) GetAllPrices(goCtx context.Context, req *types.QueryGetAllPrices
4747
if assetEntry.Denom == ptypes.Eden {
4848
denom = ptypes.Elys
4949
}
50-
tokenPriceOracle := k.oracleKeeper.GetDenomPrice(ctx, denom).Mul(utils.Pow10(assetEntry.Decimals))
51-
tokenPriceAmm := k.amm.CalcAmmPrice(ctx, denom, assetEntry.Decimals).Mul(utils.Pow10(assetEntry.Decimals))
50+
tokenPriceOracle := k.oracleKeeper.GetDenomPrice(ctx, denom).MulInt64(utils.Pow10Int64(assetEntry.Decimals))
51+
tokenPriceAmm := k.amm.CalcAmmPrice(ctx, denom, assetEntry.Decimals).MulInt64(utils.Pow10Int64(assetEntry.Decimals))
5252
prices = append(prices, &types.Price{
5353
Denom: assetEntry.Denom,
5454
OraclePrice: tokenPriceOracle.Dec(),

0 commit comments

Comments
 (0)