Skip to content

Commit

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

* 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

* fix conflicts (still need to run action)

* fix go mods to run script

* go mod updates v21 instead of main

---------

Co-authored-by: Dev Ojha <[email protected]>
Co-authored-by: Adam Tucker <[email protected]>
Co-authored-by: Adam Tucker <[email protected]>
Co-authored-by: mattverse <[email protected]>
  • Loading branch information
5 people authored Jan 9, 2024
1 parent 1a29dd0 commit 769e6f8
Show file tree
Hide file tree
Showing 11 changed files with 66 additions and 28 deletions.
8 changes: 4 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ 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.20240109043813-c4bc5e23e4fa
github.com/osmosis-labs/osmosis/osmoutils v0.0.7-0.20240109043813-c4bc5e23e4fa
github.com/osmosis-labs/osmosis/x/epochs v0.0.3-0.20240109043813-c4bc5e23e4fa
github.com/osmosis-labs/osmosis/x/ibc-hooks v0.0.9-0.20240109043813-c4bc5e23e4fa
github.com/osmosis-labs/osmosis/osmomath v0.0.7-0.20240109094227-7d7b3f1a5f6d
github.com/osmosis-labs/osmosis/osmoutils v0.0.7-0.20240109094227-7d7b3f1a5f6d
github.com/osmosis-labs/osmosis/x/epochs v0.0.3-0.20240109094227-7d7b3f1a5f6d
github.com/osmosis-labs/osmosis/x/ibc-hooks v0.0.9-0.20240109094227-7d7b3f1a5f6d
github.com/osmosis-labs/sqs v0.0.0-20240108192026-6ccc0a29f77d
github.com/osmosis-labs/sqs/sqsdomain v0.0.0-20240108192026-6ccc0a29f77d
github.com/pkg/errors v0.9.1
Expand Down
20 changes: 10 additions & 10 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -573,8 +573,8 @@ github.com/Microsoft/go-winio v0.6.1 h1:9/kr64B9VUZrLm5YYwbGtUJnMgqWVOdUAXu6Migc
github.com/Microsoft/go-winio v0.6.1/go.mod h1:LRdKpFKfdobln8UmuiYcKPot9D2v6svN5+sAH+4kjUM=
github.com/Nvveen/Gotty v0.0.0-20120604004816-cd527374f1e5 h1:TngWCqHvy9oXAN6lEVMRuU21PR1EtLVZJmdB18Gu3Rw=
github.com/Nvveen/Gotty v0.0.0-20120604004816-cd527374f1e5/go.mod h1:lmUJ/7eu/Q8D7ML55dXQrVaamCz2vxCfdQBasLZfHKk=
github.com/OneOfOne/xxhash v1.2.2 h1:KMrpdQIwFcEqXDklaen+P1axHaj9BSKzvpUUfnHldSE=
github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
github.com/OneOfOne/xxhash v1.2.8 h1:31czK/TI9sNkxIKfaUfGlU47BAxQ0ztGgd9vPyqimf8=
github.com/OpenPeeDeeP/depguard/v2 v2.1.0 h1:aQl70G173h/GZYhWf36aE5H0KaujXfVMnn/f1kSDVYY=
github.com/OpenPeeDeeP/depguard/v2 v2.1.0/go.mod h1:PUBgk35fX4i7JDmwzlJwJ+GMe6NfO1723wmJMgPThNQ=
github.com/Shopify/sarama v1.19.0/go.mod h1:FVkBWblsNy7DGZRfXLU0O9RCGt5g3g3yEuWXgklEdEo=
Expand Down Expand Up @@ -659,8 +659,8 @@ github.com/btcsuite/btcd/btcutil v1.1.0/go.mod h1:5OapHB7A2hBBWLm48mmw4MOHNJCcUB
github.com/btcsuite/btcd/btcutil v1.1.3 h1:xfbtw8lwpp0G6NwSHb+UE67ryTFHJAiNuipusjXSohQ=
github.com/btcsuite/btcd/btcutil v1.1.3/go.mod h1:UR7dsSJzJUfMmFiiLlIrMq1lS9jh9EdCV7FStZSnpi0=
github.com/btcsuite/btcd/chaincfg/chainhash v1.0.0/go.mod h1:7SFka0XMvUgj3hfZtydOrQY2mwhPclbT2snogU7SQQc=
github.com/btcsuite/btcd/chaincfg/chainhash v1.0.1 h1:q0rUy8C/TYNBQS1+CGKw68tLOFYSNEs0TFnxxnS9+4U=
github.com/btcsuite/btcd/chaincfg/chainhash v1.0.1/go.mod h1:7SFka0XMvUgj3hfZtydOrQY2mwhPclbT2snogU7SQQc=
github.com/btcsuite/btcd/chaincfg/chainhash v1.0.2 h1:KdUfX2zKommPRa+PD0sWZUyXe9w277ABlgELO7H04IM=
github.com/btcsuite/btclog v0.0.0-20170628155309-84c8d2346e9f/go.mod h1:TdznJufoqS23FtqVCzL0ZqgP5MqXbb4fg/WgDys70nA=
github.com/btcsuite/btcutil v0.0.0-20190425235716-9e5f4b9a998d/go.mod h1:+5NJ2+qvTyV9exUAL/rxXi3DcLg2Ts+ymUAY5y4NvMg=
github.com/btcsuite/go-socks v0.0.0-20170105172521-4720035b7bfd/go.mod h1:HHNXQzUsZCxOoE+CPiyCTO6x34Zs86zZUiwtpXoGdtg=
Expand Down Expand Up @@ -1457,14 +1457,14 @@ github.com/osmosis-labs/cosmos-sdk v0.47.5-v21-osmo-5 h1:yNtKKl4wVbYcX3vW79GbKNs
github.com/osmosis-labs/cosmos-sdk v0.47.5-v21-osmo-5/go.mod h1:4BNXIoS9XecywMSdPr/DIwCgH2/Qjyr2+qhrHu8YSEo=
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.20240109043813-c4bc5e23e4fa h1:ijOQARtaBCepiLa6bQl5Z77IOYZmHsYhkPi9IeroxGY=
github.com/osmosis-labs/osmosis/osmomath v0.0.7-0.20240109043813-c4bc5e23e4fa/go.mod h1:F3CP8lI5HQN3sPaZ6mBnS21ehhNSqlgsjx1hDx8uCmc=
github.com/osmosis-labs/osmosis/osmoutils v0.0.7-0.20240109043813-c4bc5e23e4fa h1:jAvpqiiS8X6dcYgfl7zeIWIlRGKphkArxU7EQ16ZZuo=
github.com/osmosis-labs/osmosis/osmoutils v0.0.7-0.20240109043813-c4bc5e23e4fa/go.mod h1:UX7hKiZbXIW4gDJ573KdVmvhxZLY7OUe6HTvR0YOnnE=
github.com/osmosis-labs/osmosis/x/epochs v0.0.3-0.20240109043813-c4bc5e23e4fa h1:hS2GIQS8TYp1yHZ+Fi9L6oPuIlfdwlnd1/bhYq+e4kQ=
github.com/osmosis-labs/osmosis/x/epochs v0.0.3-0.20240109043813-c4bc5e23e4fa/go.mod h1:fKo2dDDexDNScy44i2JDQU74a9WQ7ZABGwYQboc2EKc=
github.com/osmosis-labs/osmosis/x/ibc-hooks v0.0.9-0.20240109043813-c4bc5e23e4fa h1:aFQQf8QOddtbVObH7QvGlsrTZnYGlXbDDlrZTfxuD00=
github.com/osmosis-labs/osmosis/x/ibc-hooks v0.0.9-0.20240109043813-c4bc5e23e4fa/go.mod h1:YdU4Vli5YZijhyqYoKwg3LrjDmqkFk1UnIBREcSBb7o=
github.com/osmosis-labs/osmosis/osmomath v0.0.7-0.20240109094227-7d7b3f1a5f6d h1:coFEEv9ZQMacmfiKiz8tDCu/vAj/OCXdN5gyQKmMTQ8=
github.com/osmosis-labs/osmosis/osmomath v0.0.7-0.20240109094227-7d7b3f1a5f6d/go.mod h1:h9tbR8pCWvseEwRv29qAsgzTvr/CYddq0PY24gg+xBY=
github.com/osmosis-labs/osmosis/osmoutils v0.0.7-0.20240109094227-7d7b3f1a5f6d h1:ARQAHQAINF8iogS5Vc+1M6xD6lu10bcvQFvB/hVkoqk=
github.com/osmosis-labs/osmosis/osmoutils v0.0.7-0.20240109094227-7d7b3f1a5f6d/go.mod h1:QWydjLgWWlTQvizZc/W6bJzYn8vTVDk7X9zWyaBObSw=
github.com/osmosis-labs/osmosis/x/epochs v0.0.3-0.20240109094227-7d7b3f1a5f6d h1:V8Q+OvUAMBgSwqpiB6GD/Qy0Me8ws3oU5N+e6Vc2XK0=
github.com/osmosis-labs/osmosis/x/epochs v0.0.3-0.20240109094227-7d7b3f1a5f6d/go.mod h1:l7yluv3Sumqwpu+jZhk95SIFiwwFEdP7iVwVSwCwvHs=
github.com/osmosis-labs/osmosis/x/ibc-hooks v0.0.9-0.20240109094227-7d7b3f1a5f6d h1:HVBbpBHtC2o8GhjsrliAIPZ4jkyQtJTst+gW7sYMCH8=
github.com/osmosis-labs/osmosis/x/ibc-hooks v0.0.9-0.20240109094227-7d7b3f1a5f6d/go.mod h1:t80eA5n31RiYkj0AosDG2uPrnhC6jsGsJTnp9NK5wRM=
github.com/osmosis-labs/sqs v0.0.0-20240108192026-6ccc0a29f77d h1:J650EirkcVLQuc6B2fiVmM4kNQqntYioxd6v/D0RfLw=
github.com/osmosis-labs/sqs v0.0.0-20240108192026-6ccc0a29f77d/go.mod h1:mWCacHuCAOXS9TqOuGtSPVcO8rsfieHCaQjDE/wijZY=
github.com/osmosis-labs/sqs/sqsdomain v0.0.0-20240108192026-6ccc0a29f77d h1:udZMOAqOYdBaYdkFMmylbuOGJBlCG70qehW9QR3cOdQ=
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
14 changes: 14 additions & 0 deletions osmomath/decimal.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,15 @@ func (d BigDec) BigInt() *big.Int {
return cp.Set(d.i)
}

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

return d.i
}

// addition
func (d BigDec) Add(d2 BigDec) BigDec {
copy := d.Clone()
Expand Down Expand Up @@ -281,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
20 changes: 20 additions & 0 deletions osmomath/decimal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1701,3 +1701,23 @@ func (s *decimalTestSuite) TestQuoTruncate_MutativeAndNonMutative() {
})
}
}

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())
}
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)
}
2 changes: 1 addition & 1 deletion osmoutils/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ require (
github.com/cosmos/gogoproto v1.4.11
github.com/cosmos/iavl v0.20.1
github.com/cosmos/ibc-go/v7 v7.3.1
github.com/osmosis-labs/osmosis/osmomath v0.0.7-0.20240109043813-c4bc5e23e4fa
github.com/osmosis-labs/osmosis/osmomath v0.0.7-0.20240109094227-7d7b3f1a5f6d
github.com/osmosis-labs/osmosis/v21 v21.0.0-alpha
github.com/spf13/cast v1.6.0
github.com/spf13/cobra v1.8.0
Expand Down
4 changes: 2 additions & 2 deletions osmoutils/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1167,8 +1167,8 @@ github.com/osmosis-labs/cometbft v0.37.2-v21-osmo-1 h1:dAPp/n4+qqbJgPkMXcU+M0xUW
github.com/osmosis-labs/cometbft v0.37.2-v21-osmo-1/go.mod h1:Y2MMMN//O5K4YKd8ze4r9jmk4Y7h0ajqILXbH5JQFVs=
github.com/osmosis-labs/cosmos-sdk v0.47.5-v21-osmo-5 h1:yNtKKl4wVbYcX3vW79GbKNsB0Gb670ihBrnD5+qWtek=
github.com/osmosis-labs/cosmos-sdk v0.47.5-v21-osmo-5/go.mod h1:4BNXIoS9XecywMSdPr/DIwCgH2/Qjyr2+qhrHu8YSEo=
github.com/osmosis-labs/osmosis/osmomath v0.0.7-0.20240109043813-c4bc5e23e4fa h1:ijOQARtaBCepiLa6bQl5Z77IOYZmHsYhkPi9IeroxGY=
github.com/osmosis-labs/osmosis/osmomath v0.0.7-0.20240109043813-c4bc5e23e4fa/go.mod h1:F3CP8lI5HQN3sPaZ6mBnS21ehhNSqlgsjx1hDx8uCmc=
github.com/osmosis-labs/osmosis/osmomath v0.0.7-0.20240109094227-7d7b3f1a5f6d h1:coFEEv9ZQMacmfiKiz8tDCu/vAj/OCXdN5gyQKmMTQ8=
github.com/osmosis-labs/osmosis/osmomath v0.0.7-0.20240109094227-7d7b3f1a5f6d/go.mod h1:h9tbR8pCWvseEwRv29qAsgzTvr/CYddq0PY24gg+xBY=
github.com/osmosis-labs/osmosis/v21 v21.0.0-alpha h1:3qX2j8bVY0D7Q08Fvy5r6tr2X2AutLifBRM+hOYvkjc=
github.com/osmosis-labs/osmosis/v21 v21.0.0-alpha/go.mod h1:lnfUSROf56UFfH1cPsRVx1W9BrwYkvsU6NwkzpjIYCU=
github.com/osmosis-labs/osmosis/x/epochs v0.0.3-0.20231128160617-9a7cef4bff58 h1:DykYyH/LhDzWyAfX1H/EAJXfBr1JIrBmic1zGf36WEE=
Expand Down
2 changes: 1 addition & 1 deletion x/epochs/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ require (
github.com/cosmos/gogoproto v1.4.11
github.com/golang/protobuf v1.5.3
github.com/grpc-ecosystem/grpc-gateway v1.16.0
github.com/osmosis-labs/osmosis/osmoutils v0.0.7-0.20240109043813-c4bc5e23e4fa
github.com/osmosis-labs/osmosis/osmoutils v0.0.7-0.20240109094227-7d7b3f1a5f6d
github.com/osmosis-labs/osmosis/v21 v21.0.0-alpha
github.com/spf13/cobra v1.8.0
github.com/stretchr/testify v1.8.4
Expand Down
6 changes: 3 additions & 3 deletions x/epochs/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1181,7 +1181,7 @@ github.com/onsi/gomega v1.7.1/go.mod h1:XdKZgCCFLUoM/7CFJVPcG8C1xQ1AJ0vpAezJrB7J
github.com/onsi/gomega v1.10.1/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1ybHNo=
github.com/onsi/gomega v1.17.0/go.mod h1:HnhC7FXeEQY45zxNK3PPoIUhzk/80Xly9PcubAlGdZY=
github.com/onsi/gomega v1.19.0/go.mod h1:LY+I3pBVzYsTBU1AnDwOSxaYi9WoWiqgwooUqq9yPro=
github.com/onsi/gomega v1.20.0 h1:8W0cWlwFkflGPLltQvLRB7ZVD5HuP6ng320w2IS245Q=
github.com/onsi/gomega v1.27.10 h1:naR28SdDFlqrG6kScpT8VWpu1xWY5nJRCF3XaYyBjhI=
github.com/op/go-logging v0.0.0-20160315200505-970db520ece7/go.mod h1:HzydrMdWErDVzsI23lYNej1Htcns9BCg93Dk0bBINWk=
github.com/opencontainers/go-digest v1.0.0 h1:apOUWs51W5PlhuyGyz9FCeeBIOUDA/6nW8Oi/yOhh5U=
github.com/opencontainers/go-digest v1.0.0/go.mod h1:0JzlMkj0TRzQZfJkVvzbP0HBR3IKzErnv2BNG4W4MAM=
Expand All @@ -1202,8 +1202,8 @@ github.com/osmosis-labs/cosmos-sdk v0.47.5-v21-osmo-5 h1:yNtKKl4wVbYcX3vW79GbKNs
github.com/osmosis-labs/cosmos-sdk v0.47.5-v21-osmo-5/go.mod h1:4BNXIoS9XecywMSdPr/DIwCgH2/Qjyr2+qhrHu8YSEo=
github.com/osmosis-labs/osmosis/osmomath v0.0.7-0.20240109043813-c4bc5e23e4fa h1:ijOQARtaBCepiLa6bQl5Z77IOYZmHsYhkPi9IeroxGY=
github.com/osmosis-labs/osmosis/osmomath v0.0.7-0.20240109043813-c4bc5e23e4fa/go.mod h1:F3CP8lI5HQN3sPaZ6mBnS21ehhNSqlgsjx1hDx8uCmc=
github.com/osmosis-labs/osmosis/osmoutils v0.0.7-0.20240109043813-c4bc5e23e4fa h1:jAvpqiiS8X6dcYgfl7zeIWIlRGKphkArxU7EQ16ZZuo=
github.com/osmosis-labs/osmosis/osmoutils v0.0.7-0.20240109043813-c4bc5e23e4fa/go.mod h1:UX7hKiZbXIW4gDJ573KdVmvhxZLY7OUe6HTvR0YOnnE=
github.com/osmosis-labs/osmosis/osmoutils v0.0.7-0.20240109094227-7d7b3f1a5f6d h1:ARQAHQAINF8iogS5Vc+1M6xD6lu10bcvQFvB/hVkoqk=
github.com/osmosis-labs/osmosis/osmoutils v0.0.7-0.20240109094227-7d7b3f1a5f6d/go.mod h1:QWydjLgWWlTQvizZc/W6bJzYn8vTVDk7X9zWyaBObSw=
github.com/osmosis-labs/osmosis/v21 v21.0.0-alpha h1:3qX2j8bVY0D7Q08Fvy5r6tr2X2AutLifBRM+hOYvkjc=
github.com/osmosis-labs/osmosis/v21 v21.0.0-alpha/go.mod h1:lnfUSROf56UFfH1cPsRVx1W9BrwYkvsU6NwkzpjIYCU=
github.com/osmosis-labs/osmosis/x/ibc-hooks v0.0.9-0.20240109043813-c4bc5e23e4fa h1:aFQQf8QOddtbVObH7QvGlsrTZnYGlXbDDlrZTfxuD00=
Expand Down
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 769e6f8

Please sign in to comment.