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

Speedup more stable swap math operations #7214

Merged
merged 8 commits into from
Jan 2, 2024
Merged
Show file tree
Hide file tree
Changes from 7 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: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ 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
github.com/osmosis-labs/osmosis/osmomath v0.0.7-0.20231229191315-aff18520757d
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
Expand Down
6 changes: 4 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1466,8 +1466,10 @@ 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=
github.com/osmosis-labs/osmosis/osmomath v0.0.7-0.20231229191315-aff18520757d h1:/vmj70Yv4D4PbPmNxLRxj80aVmHllT0UD4ThvjbRChA=
github.com/osmosis-labs/osmosis/osmomath v0.0.7-0.20231229191315-aff18520757d/go.mod h1:CfSV2Zn8fYu+Jwv1zNhb45dbG4O79cZ/VM3ey79+Xq8=
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=
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
9 changes: 7 additions & 2 deletions osmomath/decimal.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,8 +253,8 @@ func (d BigDec) BigInt() *big.Int {
return cp.Set(d.i)
}

// BigIntMut converts BigDec to big.Int, mutative the input
func (d BigDec) ToBigInt() *big.Int {
// BigInt returns a copy of the underlying big.Int.
ValarDragon marked this conversation as resolved.
Show resolved Hide resolved
func (d BigDec) BigIntMut() *big.Int {
if d.IsNil() {
return nil
}
Expand Down Expand Up @@ -290,6 +290,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
8 changes: 4 additions & 4 deletions osmomath/decimal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1707,17 +1707,17 @@ func (s *decimalTestSuite) TestBigIntMut() {
d := osmomath.NewBigDecFromBigInt(r)

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

// Modify BigIntMut() pointer and ensure i.BigIntMut() & i.BigInt() change
p1 := d.ToBigInt()
p1 := d.BigIntMut()
p1.SetInt64(40)
s.Require().Equal(big.NewInt(40), d.ToBigInt())
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.ToBigInt())
s.Require().NotEqual(big.NewInt(50), d.BigIntMut())
s.Require().NotEqual(big.NewInt(50), d.BigInt())
}
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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note to other reviewers: x_{out}^3 remains same

// 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