From f939c959c7ecef6acd87c5c7171b61a0b2fe403b Mon Sep 17 00:00:00 2001 From: Dev Ojha Date: Wed, 27 Dec 2023 12:23:20 -0600 Subject: [PATCH 1/7] Speedup more stable swap math operations --- osmomath/binary_search.go | 6 ++++-- osmomath/decimal.go | 4 ++-- osmomath/decimal_test.go | 8 ++++---- osmomath/sigfig_round.go | 5 +++-- x/gamm/pool-models/stableswap/amm.go | 4 +++- 5 files changed, 16 insertions(+), 11 deletions(-) diff --git a/osmomath/binary_search.go b/osmomath/binary_search.go index 17ef1d9c129..e6d449fb9b7 100644 --- a/osmomath/binary_search.go +++ b/osmomath/binary_search.go @@ -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) diff --git a/osmomath/decimal.go b/osmomath/decimal.go index 99591a51727..c9ad392d370 100644 --- a/osmomath/decimal.go +++ b/osmomath/decimal.go @@ -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. +func (d BigDec) BigIntMut() *big.Int { if d.IsNil() { return nil } diff --git a/osmomath/decimal_test.go b/osmomath/decimal_test.go index d6f517ea65a..eb5bb6bb8a6 100644 --- a/osmomath/decimal_test.go +++ b/osmomath/decimal_test.go @@ -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()) } diff --git a/osmomath/sigfig_round.go b/osmomath/sigfig_round.go index 692d5c72262..7dfea791aad 100644 --- a/osmomath/sigfig_round.go +++ b/osmomath/sigfig_round.go @@ -17,10 +17,11 @@ func SigFigRound(d Dec, tenToSigFig Int) Dec { dTimesK.MulInt64Mut(10) } // d * 10^k * 10^sigfig - dkSigFig := dTimesK.MulInt(tenToSigFig) + dkSigFig := dTimesK.MulIntMut(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) } diff --git a/x/gamm/pool-models/stableswap/amm.go b/x/gamm/pool-models/stableswap/amm.go index 80f009996b2..512fee20e02 100644 --- a/x/gamm/pool-models/stableswap/amm.go +++ b/x/gamm/pool-models/stableswap/amm.go @@ -60,11 +60,13 @@ func targetKCalculator(x0, y0, w, yf osmomath.BigDec) osmomath.BigDec { return yfRemoved.Sub(constantTerm) } +var negOneBigDec = osmomath.OneBigDec().Neg() + // $$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() + cubicCoeff := negOneBigDec quadraticCoeff := x0.MulInt64(3) linearCoeff := quadraticCoeff.Mul(x0).Add(w).Add(yf.Mul(yf)).Neg() return func(xf osmomath.BigDec) osmomath.BigDec { From 4e5a5f42d8239ae8550a6125cfe9221931b80785 Mon Sep 17 00:00:00 2001 From: Dev Ojha Date: Sun, 31 Dec 2023 15:25:00 -0600 Subject: [PATCH 2/7] Try another speedup --- x/gamm/pool-models/stableswap/amm.go | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/x/gamm/pool-models/stableswap/amm.go b/x/gamm/pool-models/stableswap/amm.go index 512fee20e02..e1e111a0f1b 100644 --- a/x/gamm/pool-models/stableswap/amm.go +++ b/x/gamm/pool-models/stableswap/amm.go @@ -60,20 +60,19 @@ func targetKCalculator(x0, y0, w, yf osmomath.BigDec) osmomath.BigDec { return yfRemoved.Sub(constantTerm) } -var negOneBigDec = osmomath.OneBigDec().Neg() - // $$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 := negOneBigDec + // cubicCoeff := negOneBigDec quadraticCoeff := x0.MulInt64(3) linearCoeff := quadraticCoeff.Mul(x0).Add(w).Add(yf.Mul(yf)).Neg() 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 From 4fedfda9c154b5d96b6731399859c86402da7c8a Mon Sep 17 00:00:00 2001 From: Dev Ojha Date: Sun, 31 Dec 2023 15:33:51 -0600 Subject: [PATCH 3/7] revert one sigfig change --- osmomath/sigfig_round.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/osmomath/sigfig_round.go b/osmomath/sigfig_round.go index 7dfea791aad..26ccbaa19fd 100644 --- a/osmomath/sigfig_round.go +++ b/osmomath/sigfig_round.go @@ -17,7 +17,7 @@ func SigFigRound(d Dec, tenToSigFig Int) Dec { dTimesK.MulInt64Mut(10) } // d * 10^k * 10^sigfig - dkSigFig := dTimesK.MulIntMut(tenToSigFig) + dkSigFig := dTimesK.MulInt(tenToSigFig) numerator := dkSigFig.RoundInt().ToLegacyDec() // TODO: Use pre-computed table for 10^k From 1a8c243520e97e676dc10a2a35c27e46619787a3 Mon Sep 17 00:00:00 2001 From: Dev Ojha Date: Sun, 31 Dec 2023 15:55:38 -0600 Subject: [PATCH 4/7] remove one more mutative operation --- osmomath/decimal.go | 5 +++++ x/gamm/pool-models/stableswap/amm.go | 6 +++--- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/osmomath/decimal.go b/osmomath/decimal.go index c9ad392d370..b34eed0abe2 100644 --- a/osmomath/decimal.go +++ b/osmomath/decimal.go @@ -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 { diff --git a/x/gamm/pool-models/stableswap/amm.go b/x/gamm/pool-models/stableswap/amm.go index e1e111a0f1b..50337581abc 100644 --- a/x/gamm/pool-models/stableswap/amm.go +++ b/x/gamm/pool-models/stableswap/amm.go @@ -63,10 +63,10 @@ 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 := negOneBigDec + // 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 From ffa615614f3a6fb3fa92e374ea782becca934980 Mon Sep 17 00:00:00 2001 From: Dev Ojha Date: Sun, 31 Dec 2023 16:10:34 -0600 Subject: [PATCH 5/7] update osmomath --- go.mod | 2 +- go.sum | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/go.mod b/go.mod index 0c7cb2fa727..529a8686322 100644 --- a/go.mod +++ b/go.mod @@ -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.20231215070550-ca294af82738 + github.com/osmosis-labs/osmosis/osmomath v0.0.7-0.20231231215538-1a8c243520e9 github.com/osmosis-labs/osmosis/osmoutils v0.0.7-0.20231211161501-28475581c846 github.com/osmosis-labs/osmosis/x/epochs v0.0.3-0.20231211161501-28475581c846 github.com/osmosis-labs/osmosis/x/ibc-hooks v0.0.9-0.20231211161501-28475581c846 diff --git a/go.sum b/go.sum index 5ef7b949245..6071f727318 100644 --- a/go.sum +++ b/go.sum @@ -1468,6 +1468,8 @@ github.com/osmosis-labs/go-mutesting v0.0.0-20221208041716-b43bcd97b3b3 h1:Ylmch 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.20231215070550-ca294af82738 h1:60wdzrarvo9BlI7R5aCIEpOAThDpsr/aVJe68aEayVw= github.com/osmosis-labs/osmosis/osmomath v0.0.7-0.20231215070550-ca294af82738/go.mod h1:FQ45vPTc2l+NUA/gk7wZTxUh/hrDRWrRwZj8Ua5cpSU= +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/osmoutils v0.0.7-0.20231211161501-28475581c846 h1:+xo3n7+SwSiH0VZSYSSHWPkXx8XD/l01OnTEfYafAFw= github.com/osmosis-labs/osmosis/osmoutils v0.0.7-0.20231211161501-28475581c846/go.mod h1:BsZRNH2ypxCdG8VObfFzbzjSflX5umvCziyBTAS9Ftw= github.com/osmosis-labs/osmosis/x/epochs v0.0.3-0.20231211161501-28475581c846 h1:ZnZKvZGVcirtNYZ+nk+qr0GO4ljxGe/z7X5+7+UklsM= From 809ff1fac6c1e6178f56f0a783083fefdaa335ac Mon Sep 17 00:00:00 2001 From: Dev Ojha Date: Sun, 31 Dec 2023 19:36:32 -0600 Subject: [PATCH 6/7] Update osmomath one more tiome --- go.mod | 2 +- go.sum | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/go.mod b/go.mod index 42b26a89e58..761a4d2a000 100644 --- a/go.mod +++ b/go.mod @@ -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.20231231215538-1a8c243520e9 + 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 diff --git a/go.sum b/go.sum index 514c7e14f14..077becda65c 100644 --- a/go.sum +++ b/go.sum @@ -1468,6 +1468,8 @@ github.com/osmosis-labs/go-mutesting v0.0.0-20221208041716-b43bcd97b3b3 h1:Ylmch 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.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= From fb38b76f11bfe3756809e5db99e94580c3cc1182 Mon Sep 17 00:00:00 2001 From: Dev Ojha Date: Tue, 2 Jan 2024 22:33:52 +0100 Subject: [PATCH 7/7] Update osmomath/decimal.go Co-authored-by: Matt, Park <45252226+mattverse@users.noreply.github.com> --- osmomath/decimal.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/osmomath/decimal.go b/osmomath/decimal.go index b34eed0abe2..02db83a5264 100644 --- a/osmomath/decimal.go +++ b/osmomath/decimal.go @@ -253,7 +253,7 @@ func (d BigDec) BigInt() *big.Int { return cp.Set(d.i) } -// BigInt returns a copy of the underlying big.Int. +// BigIntMut returns the pointer of the underlying big.Int. func (d BigDec) BigIntMut() *big.Int { if d.IsNil() { return nil