Skip to content

Commit

Permalink
Speedup more stable swap math operations (#7214)
Browse files Browse the repository at this point in the history
* Speedup more stable swap math operations

* Try another speedup

* revert one sigfig change

* remove one more mutative operation

* update osmomath

* Update osmomath one more tiome

* Update osmomath/decimal.go

Co-authored-by: Matt, Park <[email protected]>

---------

Co-authored-by: Matt, Park <[email protected]>
(cherry picked from commit 9cd7231)

# Conflicts:
#	go.mod
#	go.sum
#	osmomath/decimal.go
#	osmomath/decimal_test.go
  • Loading branch information
ValarDragon authored and mergify[bot] committed Jan 2, 2024
1 parent 5999a0f commit 29d9423
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 7 deletions.
7 changes: 7 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,17 @@ require (
github.com/mattn/go-sqlite3 v1.14.17
github.com/ory/dockertest/v3 v3.10.0
github.com/osmosis-labs/go-mutesting v0.0.0-20221208041716-b43bcd97b3b3
<<<<<<< HEAD
github.com/osmosis-labs/osmosis/osmomath v0.0.7-0.20231230033922-af0ba43e918a
github.com/osmosis-labs/osmosis/osmoutils v0.0.7-0.20231230033922-af0ba43e918a
github.com/osmosis-labs/osmosis/x/epochs v0.0.3-0.20231230033922-af0ba43e918a
github.com/osmosis-labs/osmosis/x/ibc-hooks v0.0.9-0.20231230033922-af0ba43e918a
=======
github.com/osmosis-labs/osmosis/osmomath v0.0.7-0.20231231222019-bcdabda4fc36
github.com/osmosis-labs/osmosis/osmoutils v0.0.7-0.20231229191315-aff18520757d
github.com/osmosis-labs/osmosis/x/epochs v0.0.3-0.20231229191315-aff18520757d
github.com/osmosis-labs/osmosis/x/ibc-hooks v0.0.9-0.20231229191315-aff18520757d
>>>>>>> 9cd72313 (Speedup more stable swap math operations (#7214))
github.com/pkg/errors v0.9.1
github.com/rakyll/statik v0.1.7
github.com/redis/go-redis/v9 v9.3.0
Expand Down
13 changes: 13 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1465,6 +1465,7 @@ github.com/osmosis-labs/cosmos-sdk v0.47.5-osmo-4 h1:RCd53Gnnn9yrjjdh8sBkhitqTz/
github.com/osmosis-labs/cosmos-sdk v0.47.5-osmo-4/go.mod h1:EHwCeN9IXonsjKcjpS12MqeStdZvIdxt3VYXhus3G3c=
github.com/osmosis-labs/go-mutesting v0.0.0-20221208041716-b43bcd97b3b3 h1:YlmchqTmlwdWSmrRmXKR+PcU96ntOd8u10vTaTZdcNY=
github.com/osmosis-labs/go-mutesting v0.0.0-20221208041716-b43bcd97b3b3/go.mod h1:lV6KnqXYD/ayTe7310MHtM3I2q8Z6bBfMAi+bhwPYtI=
<<<<<<< HEAD
github.com/osmosis-labs/osmosis/osmomath v0.0.7-0.20231230033922-af0ba43e918a h1:kUYapBvN7T5bP8JJdfvEm43Sx0uswMQC7sMOSpLPBgI=
github.com/osmosis-labs/osmosis/osmomath v0.0.7-0.20231230033922-af0ba43e918a/go.mod h1:KI8rTPBt2+0uSbmqG4jAu2UcLTlbQbHMN5L5zKwaYZE=
github.com/osmosis-labs/osmosis/osmoutils v0.0.7-0.20231230033922-af0ba43e918a h1:BXsH0SEatBFZW8Sn76yHVbFHxubrPCDJl5iJq20/Bok=
Expand All @@ -1473,6 +1474,18 @@ github.com/osmosis-labs/osmosis/x/epochs v0.0.3-0.20231230033922-af0ba43e918a h1
github.com/osmosis-labs/osmosis/x/epochs v0.0.3-0.20231230033922-af0ba43e918a/go.mod h1:tQTs2/BbDThu5lm7wWo10LcLORpesqUs6lAc4JrPJ2E=
github.com/osmosis-labs/osmosis/x/ibc-hooks v0.0.9-0.20231230033922-af0ba43e918a h1:0uGi0vDzqZlBQlvdcMp6bjy+uCJiJxTFKD3yTIBFoC8=
github.com/osmosis-labs/osmosis/x/ibc-hooks v0.0.9-0.20231230033922-af0ba43e918a/go.mod h1:2fsIpbwZ0taYz4tCZllxIOdTWhFVJrV0778SSmukSWE=
=======
github.com/osmosis-labs/osmosis/osmomath v0.0.7-0.20231231215538-1a8c243520e9 h1:uv3HgeYw9QArhkO9YQGVfsnm/xS79ldMBJAMFZwXZno=
github.com/osmosis-labs/osmosis/osmomath v0.0.7-0.20231231215538-1a8c243520e9/go.mod h1:FQ45vPTc2l+NUA/gk7wZTxUh/hrDRWrRwZj8Ua5cpSU=
github.com/osmosis-labs/osmosis/osmomath v0.0.7-0.20231231222019-bcdabda4fc36 h1:UOpEx+lWFd9uX0goJMLaIvSjMCsWOmBzLOEVGZCi2Qk=
github.com/osmosis-labs/osmosis/osmomath v0.0.7-0.20231231222019-bcdabda4fc36/go.mod h1:Ger7K14gEgLOF+4qHNqicrGkNP9Ubg2tBnKkIzvXABE=
github.com/osmosis-labs/osmosis/osmoutils v0.0.7-0.20231229191315-aff18520757d h1:VKMEmZBVt6VrrjBA9dMsxqWJlbrzqbMXX69iOjuevRc=
github.com/osmosis-labs/osmosis/osmoutils v0.0.7-0.20231229191315-aff18520757d/go.mod h1:Jg/goBqOHnJSr4qgDLtVC88eU7MgtLhyc5+plNEj1Ag=
github.com/osmosis-labs/osmosis/x/epochs v0.0.3-0.20231229191315-aff18520757d h1:hEwBZm3JibS2RSZat6YQ68RF6ocL1zxdbNgN7viM8K8=
github.com/osmosis-labs/osmosis/x/epochs v0.0.3-0.20231229191315-aff18520757d/go.mod h1:1SJlKmf6tgG0fto/vKK/o07gV5zar/XEn9s9HzyFuoY=
github.com/osmosis-labs/osmosis/x/ibc-hooks v0.0.9-0.20231229191315-aff18520757d h1:0+gAf1ls/qfyPD57g0lNKJuUeGgSot/lC8XkRdaCQyw=
github.com/osmosis-labs/osmosis/x/ibc-hooks v0.0.9-0.20231229191315-aff18520757d/go.mod h1:k0KAmphBh45V/5ISLaoqRLLKbF2DPH+Uy7J6apdfT4w=
>>>>>>> 9cd72313 (Speedup more stable swap math operations (#7214))
github.com/osmosis-labs/wasmd v0.45.0-osmo h1:NIp7pvJV5HuBN1HwPgEmXKQM2TjVIVdJErIHnB9IMO8=
github.com/osmosis-labs/wasmd v0.45.0-osmo/go.mod h1:J6eRvwii5T1WxhetZkBg1kOJS3GTn1Bw2OLyZBb8EVU=
github.com/otiai10/copy v1.2.0/go.mod h1:rrF5dJ5F0t/EWSYODDu4j9/vEeYHMkc8jt0zJChqQWw=
Expand Down
6 changes: 4 additions & 2 deletions osmomath/binary_search.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,8 +279,10 @@ func BinarySearchBigDec(f func(BigDec) BigDec,

curIteration := 0
for ; curIteration < maxIterations; curIteration += 1 {
// TODO: Try replacing this with RSH
curEstimate = lowerbound.Add(upperbound).QuoRaw(2)
// (lowerbound + upperbound) / 2
curEstimate = lowerbound.Add(upperbound)
curEstimateBi := curEstimate.BigIntMut()
curEstimateBi.Rsh(curEstimateBi, 1)
curOutput = f(curEstimate)

// fmt.Println("binary search, input, target output, cur output", curEstimate, targetOutput, curOutput)
Expand Down
17 changes: 17 additions & 0 deletions osmomath/decimal.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,18 @@ func (d BigDec) BigInt() *big.Int {
return cp.Set(d.i)
}

<<<<<<< HEAD
=======
// BigIntMut returns the pointer of the underlying big.Int.
func (d BigDec) BigIntMut() *big.Int {
if d.IsNil() {
return nil
}

return d.i
}

>>>>>>> 9cd72313 (Speedup more stable swap math operations (#7214))
// addition
func (d BigDec) Add(d2 BigDec) BigDec {
copy := d.Clone()
Expand Down Expand Up @@ -281,6 +293,11 @@ func (d BigDec) Sub(d2 BigDec) BigDec {
return BigDec{res}
}

func (d BigDec) NegMut() BigDec {
d.i.Neg(d.i)
return d
} // reverse the decimal sign

// Clone performs a deep copy of the receiver
// and returns the new result.
func (d BigDec) Clone() BigDec {
Expand Down
23 changes: 23 additions & 0 deletions osmomath/decimal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1701,3 +1701,26 @@ func (s *decimalTestSuite) TestQuoTruncate_MutativeAndNonMutative() {
})
}
}
<<<<<<< HEAD
=======

func (s *decimalTestSuite) TestBigIntMut() {
r := big.NewInt(30)
d := osmomath.NewBigDecFromBigInt(r)

// Compare value of BigInt & BigIntMut
s.Require().Equal(d.BigInt(), d.BigIntMut())

// Modify BigIntMut() pointer and ensure i.BigIntMut() & i.BigInt() change
p1 := d.BigIntMut()
p1.SetInt64(40)
s.Require().Equal(big.NewInt(40), d.BigIntMut())
s.Require().Equal(big.NewInt(40), d.BigInt())

// Modify big.Int() pointer and ensure i.BigIntMut() & i.BigInt() don't change
p2 := d.BigInt()
p2.SetInt64(50)
s.Require().NotEqual(big.NewInt(50), d.BigIntMut())
s.Require().NotEqual(big.NewInt(50), d.BigInt())
}
>>>>>>> 9cd72313 (Speedup more stable swap math operations (#7214))
3 changes: 2 additions & 1 deletion osmomath/sigfig_round.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ func SigFigRound(d Dec, tenToSigFig Int) Dec {
dkSigFig := dTimesK.MulInt(tenToSigFig)
numerator := dkSigFig.RoundInt().ToLegacyDec()

// TODO: Use pre-computed table for 10^k
tenToK := NewInt(10).ToLegacyDec().Power(k)
denominator := tenToSigFig.Mul(tenToK.TruncateInt())
return numerator.QuoInt(denominator)
return numerator.QuoIntMut(denominator)
}
9 changes: 5 additions & 4 deletions x/gamm/pool-models/stableswap/amm.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,16 @@ func targetKCalculator(x0, y0, w, yf osmomath.BigDec) osmomath.BigDec {
// $$k_{iter}(x_f) = -x_{out}^3 + 3 x_0 x_{out}^2 - (y_f^2 + w + 3x_0^2)x_{out}$$
// where x_out = x_0 - x_f
func iterKCalculator(x0, w, yf osmomath.BigDec) func(osmomath.BigDec) osmomath.BigDec {
// compute coefficients first
cubicCoeff := osmomath.OneBigDec().Neg()
// compute coefficients first. Notice that the leading coefficient is -1, we will use this to compute faster.
// cubicCoeff := -1
quadraticCoeff := x0.MulInt64(3)
linearCoeff := quadraticCoeff.Mul(x0).Add(w).Add(yf.Mul(yf)).Neg()
linearCoeff := quadraticCoeff.Mul(x0).Add(w).Add(yf.Mul(yf)).NegMut()
return func(xf osmomath.BigDec) osmomath.BigDec {
xOut := x0.Sub(xf)
// horners method
// ax^3 + bx^2 + cx = x(c + x(b + ax))
res := cubicCoeff.Mul(xOut)
// a = -1
res := xOut.Neg()
res = res.AddMut(quadraticCoeff).MulMut(xOut)
res = res.AddMut(linearCoeff).MulMut(xOut)
return res
Expand Down

0 comments on commit 29d9423

Please sign in to comment.