From a92eed86d49618999e5420d582b8ec0043dfde55 Mon Sep 17 00:00:00 2001 From: Dev Ojha Date: Fri, 12 Apr 2024 11:28:55 +0900 Subject: [PATCH 1/2] Finish LRU cache --- go.mod | 2 ++ go.sum | 2 ++ .../pools/routable_concentrated_pool.go | 20 ++++++++++++++++++- 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/go.mod b/go.mod index 3c992fa4d..99d187ff6 100644 --- a/go.mod +++ b/go.mod @@ -29,6 +29,8 @@ require ( google.golang.org/grpc v1.61.1 ) +require github.com/hashicorp/golang-lru/v2 v2.0.7 // indirect + require ( cloud.google.com/go v0.112.0 // indirect cloud.google.com/go/compute v1.24.0 // indirect diff --git a/go.sum b/go.sum index 6a1e4767a..176f3ffe9 100644 --- a/go.sum +++ b/go.sum @@ -1074,6 +1074,8 @@ github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= github.com/hashicorp/golang-lru v0.5.5-0.20210104140557-80c98217689d h1:dg1dEPuWpEqDnvIw251EVy4zlP8gWbsGj4BsUKCRpYs= github.com/hashicorp/golang-lru v0.5.5-0.20210104140557-80c98217689d/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4= +github.com/hashicorp/golang-lru/v2 v2.0.7 h1:a+bsQ5rvGLjzHuww6tVxozPZFVghXaHOwFs4luLUK2k= +github.com/hashicorp/golang-lru/v2 v2.0.7/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyfM2/ZepoAG6RGpeM= github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4= github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= github.com/hashicorp/logutils v1.0.0/go.mod h1:QIAnNjmIWmVIIkWDTG1z5v++HQmx9WQRO+LraFDTW64= diff --git a/router/usecase/pools/routable_concentrated_pool.go b/router/usecase/pools/routable_concentrated_pool.go index 433c1b0b1..7cf0e08f6 100644 --- a/router/usecase/pools/routable_concentrated_pool.go +++ b/router/usecase/pools/routable_concentrated_pool.go @@ -7,6 +7,7 @@ import ( "cosmossdk.io/math" storetypes "github.com/cosmos/cosmos-sdk/store/types" sdk "github.com/cosmos/cosmos-sdk/types" + lru "github.com/hashicorp/golang-lru/v2" "github.com/osmosis-labs/sqs/sqsdomain" "github.com/osmosis-labs/sqs/domain" @@ -28,6 +29,23 @@ type routableConcentratedPoolImpl struct { TakerFee osmomath.Dec "json:\"taker_fee\"" } +// Size is roughly `keys * (2.5 * Key_size + 2*value_size)`. (Plus whatever excess overhead hashmaps internally have) +// key is 8 bytes, value is ~152 bytes +// so at 100k keys its max RAM of ~30MB +var tickToSqrtPriceCache, _ = lru.New2Q[int64, osmomath.BigDec](1000000) + +func getTickToSqrtPrice(tick int64) (osmomath.BigDec, error) { + if sqrtPrice, ok := tickToSqrtPriceCache.Get(tick); ok { + return sqrtPrice, nil + } + + sqrtPrice, err := clmath.TickToSqrtPrice(tick) + if err != nil { + tickToSqrtPriceCache.Add(tick, sqrtPrice) + } + return sqrtPrice, err +} + // GetPoolDenoms implements sqsdomain.RoutablePool. func (r *routableConcentratedPoolImpl) GetPoolDenoms() []string { return r.ChainPool.GetPoolDenoms(sdk.Context{}) @@ -152,7 +170,7 @@ func (r *routableConcentratedPoolImpl) CalculateTokenOutByTokenIn(ctx context.Co } // Get the sqrt price for the next initialized tick index. - sqrtPriceTarget, err := clmath.TickToSqrtPrice(nextInitializedTickIndex) + sqrtPriceTarget, err := getTickToSqrtPrice(nextInitializedTickIndex) if err != nil { return sdk.Coin{}, err } From bdb62dd67bc5d7737239d8efff780487269d03d6 Mon Sep 17 00:00:00 2001 From: Dev Ojha Date: Fri, 12 Apr 2024 11:43:37 +0900 Subject: [PATCH 2/2] add two nolints --- router/usecase/pools/routable_concentrated_pool.go | 1 + router/usecase/pools/routable_cw_transmuter_pool.go | 1 + 2 files changed, 2 insertions(+) diff --git a/router/usecase/pools/routable_concentrated_pool.go b/router/usecase/pools/routable_concentrated_pool.go index 7cf0e08f6..edeb47c6e 100644 --- a/router/usecase/pools/routable_concentrated_pool.go +++ b/router/usecase/pools/routable_concentrated_pool.go @@ -187,6 +187,7 @@ func (r *routableConcentratedPoolImpl) CalculateTokenOutByTokenIn(ctx context.Co } // Return the total amount out. + //nolint:all return sdk.Coin{tokenOutDenom, amountOutTotal.TruncateInt()}, nil } diff --git a/router/usecase/pools/routable_cw_transmuter_pool.go b/router/usecase/pools/routable_cw_transmuter_pool.go index 76c8a7ac9..c50129a46 100644 --- a/router/usecase/pools/routable_cw_transmuter_pool.go +++ b/router/usecase/pools/routable_cw_transmuter_pool.go @@ -69,6 +69,7 @@ func (r *routableTransmuterPoolImpl) CalculateTokenOutByTokenIn(ctx context.Cont // No slippage swaps - just return the same amount of token out as token in // as long as there is enough liquidity in the pool. + //nolint:all return sdk.Coin{r.TokenOutDenom, tokenIn.Amount}, nil }