Skip to content
Open
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
21 changes: 21 additions & 0 deletions utils/mempool.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package utils

import (
"math/big"
"sync"
)

var intPool = &sync.Pool{
New: func() interface{} {
return new(big.Int)
},
}

func GetBigInt() *big.Int {
return intPool.Get().(*big.Int)
}

func PutBigInt(i *big.Int) {
i.SetInt64(0) // or you might want to reset to some default value
intPool.Put(i)
}
10 changes: 6 additions & 4 deletions utils/tick_math.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ var (
)

func mulShift(val *big.Int, mulBy *big.Int) *big.Int {

return new(big.Int).Rsh(new(big.Int).Mul(val, mulBy), 128)
return GetBigInt().Rsh(new(big.Int).Mul(val, mulBy), 128)
//return new(big.Int).Rsh(new(big.Int).Mul(val, mulBy), 128)
}

var (
Expand Down Expand Up @@ -66,10 +66,11 @@ func GetSqrtRatioAtTick(tick int) (*big.Int, error) {
absTick = -tick
}
var ratio *big.Int

if absTick&0x1 != 0 {
ratio = sqrtConst1
ratio = GetBigInt().Set(sqrtConst1)
} else {
ratio = sqrtConst2
ratio = GetBigInt().Set(sqrtConst2)
}
if (absTick & 0x2) != 0 {
ratio = mulShift(ratio, sqrtConst3)
Expand Down Expand Up @@ -132,6 +133,7 @@ func GetSqrtRatioAtTick(tick int) (*big.Int, error) {
ratio = new(big.Int).Div(entities.MaxUint256, ratio)
}

defer PutBigInt(ratio)
// back to Q96
if new(big.Int).Rem(ratio, Q32).Cmp(constants.Zero) > 0 {
return new(big.Int).Add((new(big.Int).Div(ratio, Q32)), constants.One), nil
Expand Down