Skip to content

Commit

Permalink
Add an LRU cache for tick to sqrt price (#167)
Browse files Browse the repository at this point in the history
* Finish LRU cache

* add two nolints
  • Loading branch information
ValarDragon authored Apr 12, 2024
1 parent ef32fcf commit 4a528c6
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 1 deletion.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
21 changes: 20 additions & 1 deletion router/usecase/pools/routable_concentrated_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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{})
Expand Down Expand Up @@ -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
}
Expand All @@ -169,6 +187,7 @@ func (r *routableConcentratedPoolImpl) CalculateTokenOutByTokenIn(ctx context.Co
}

// Return the total amount out.
//nolint:all
return sdk.Coin{tokenOutDenom, amountOutTotal.TruncateInt()}, nil
}

Expand Down
1 change: 1 addition & 0 deletions router/usecase/pools/routable_cw_transmuter_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down

0 comments on commit 4a528c6

Please sign in to comment.