Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an LRU cache for tick to sqrt price #167

Merged
merged 2 commits into from
Apr 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading