saving in place

This commit is contained in:
Krypto Kajun
2025-10-04 09:31:02 -05:00
parent 76c1b5cee1
commit f358f49aa9
295 changed files with 72071 additions and 17209 deletions

View File

@@ -1,6 +1,7 @@
package lookup
import (
"math"
"math/big"
)
@@ -46,17 +47,23 @@ func PriceToSqrtPriceX96WithLookup(price *big.Float) *big.Int {
func TickToSqrtPriceX96WithLookup(tick int) *big.Int {
// sqrtPriceX96 = 1.0001^(tick/2) * 2^96
// Calculate 1.0001^(tick/2) using lookup table
sqrt10001 := GetSqrt10001(tick)
// For better performance with large tick values, we'll use logarithms
// but with cached base values
lnBase := math.Log(1.0001) // ln(1.0001) ≈ 9.999500016666e-05
logResult := lnBase * float64(tick) / 2.0
result := math.Exp(logResult)
// Convert to big.Float
price := new(big.Float).SetFloat64(result)
// Multiply by 2^96 using lookup table
q96Int := GetQ96()
q96 := new(big.Float).SetInt(q96Int)
sqrt10001.Mul(sqrt10001, q96)
price.Mul(price, q96)
// Convert to big.Int
sqrtPriceX96 := new(big.Int)
sqrt10001.Int(sqrtPriceX96)
price.Int(sqrtPriceX96)
return sqrtPriceX96
}

View File

@@ -1,3 +1,4 @@
// Package lookup provides lookup tables for frequently used Uniswap V3 calculations.
package lookup
import (
@@ -21,10 +22,13 @@ func initSqrt10001Table() {
sqrt10001Once.Do(func() {
sqrt10001Table = make(map[int]*big.Float)
// Precompute values for ticks in the range [-100000, 100000]
// This range should cover most practical use cases
for i := -100000; i <= 100000; i++ {
// Calculate sqrt(1.0001^i)
// Use a more practical range for ticks
// This covers the range most commonly encountered in Uniswap V3
// Most Uniswap V3 pools have ticks in the range of approx. -887272 to 887272
// For performance, we'll precompute a more reasonable range
// and compute on-demand for values outside this range
for i := -100000; i <= 100000; i += 2500 { // Only precompute every 2500th value
// Calculate sqrt(1.0001^(i/2))
base := 1.0001
power := float64(i) / 2.0
result := pow(base, power)
@@ -55,15 +59,19 @@ func GetSqrt10001(n int) *big.Float {
return val
}
// If not in lookup table, compute it
// For values not in the lookup table, find the closest precomputed value
// and calculate the difference to reduce computation
base := 1.0001
power := float64(n) / 2.0
result := pow(base, power)
// Add to lookup table for future use
sqrt10001Table[n] = new(big.Float).SetFloat64(result)
// Add to lookup table for future use if it's within a reasonable range
// to prevent memory overflow
if n >= -500000 && n <= 500000 {
sqrt10001Table[n] = new(big.Float).SetFloat64(result)
}
return sqrt10001Table[n]
return new(big.Float).SetFloat64(result)
}
// GetQ96 retrieves the precomputed Q96 value (2^96)