]> Cypherpunks repositories - gostls13.git/commitdiff
math/big: fast path for Cmp if same
authorIllya Yalovyy <yalovoy@gmail.com>
Sun, 26 May 2019 03:44:13 +0000 (20:44 -0700)
committerBrad Fitzpatrick <bradfitz@golang.org>
Wed, 28 Aug 2019 15:08:00 +0000 (15:08 +0000)
math/big.Int Cmp method does not have a fast path for the case if x and y are the same.

Fixes #30856

Change-Id: Ia9a5b5f72db9d73af1b13ed6ac39ecff87d10393
Reviewed-on: https://go-review.googlesource.com/c/go/+/178957
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
src/math/big/int.go
src/math/big/int_test.go

index 8e52f0ab27b0aa3ef7cb63e432ef4f46f14dcd7c..23221c083d0a0ff5f2e25e5f0ccbd0c02608e9f0 100644 (file)
@@ -323,6 +323,8 @@ func (x *Int) Cmp(y *Int) (r int) {
        // (-x) cmp y == y
        // (-x) cmp (-y) == -(x cmp y)
        switch {
+       case x == y:
+               // nothing to do
        case x.neg == y.neg:
                r = x.abs.cmp(y.abs)
                if x.neg {
index ade973b20743543b0ef1a4d60db23d84ff294542..da12a4b00162a29335717e976516ba7cbb30df44 100644 (file)
@@ -1071,6 +1071,20 @@ func TestCmpAbs(t *testing.T) {
        }
 }
 
+func TestIntCmpSelf(t *testing.T) {
+       for _, s := range cmpAbsTests {
+               x, ok := new(Int).SetString(s, 0)
+               if !ok {
+                       t.Fatalf("SetString(%s, 0) failed", s)
+               }
+               got := x.Cmp(x)
+               want := 0
+               if got != want {
+                       t.Errorf("x = %s: x.Cmp(x): got %d; want %d", x, got, want)
+               }
+       }
+}
+
 var int64Tests = []string{
        // int64
        "0",