]> Cypherpunks repositories - gostls13.git/commitdiff
reflect: remove depth from deepequal recursion
authorKeith Randall <khr@golang.org>
Thu, 18 Jun 2020 01:22:38 +0000 (18:22 -0700)
committerKeith Randall <khr@golang.org>
Mon, 17 Aug 2020 22:05:16 +0000 (22:05 +0000)
We aren't using it for anything. The visited map will terminate
any recursion for us.

Change-Id: I36e6bd8e34952123c2ed46323067e42928ec7168
Reviewed-on: https://go-review.googlesource.com/c/go/+/238759
Run-TryBot: Keith Randall <khr@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Emmanuel Odeke <emm.odeke@gmail.com>
src/reflect/deepequal.go

index 8a2bf8b09e23a42274fc70d741605566c765d323..be66464129a795ab47b74efcdaaa2477b5dcb21f 100644 (file)
@@ -21,7 +21,7 @@ type visit struct {
 // Tests for deep equality using reflected types. The map argument tracks
 // comparisons that have already been seen, which allows short circuiting on
 // recursive types.
-func deepValueEqual(v1, v2 Value, visited map[visit]bool, depth int) bool {
+func deepValueEqual(v1, v2 Value, visited map[visit]bool) bool {
        if !v1.IsValid() || !v2.IsValid() {
                return v1.IsValid() == v2.IsValid()
        }
@@ -29,8 +29,6 @@ func deepValueEqual(v1, v2 Value, visited map[visit]bool, depth int) bool {
                return false
        }
 
-       // if depth > 10 { panic("deepValueEqual") }    // for debugging
-
        // We want to avoid putting more in the visited map than we need to.
        // For any possible reference cycle that might be encountered,
        // hard(v1, v2) needs to return true for at least one of the types in the cycle,
@@ -79,7 +77,7 @@ func deepValueEqual(v1, v2 Value, visited map[visit]bool, depth int) bool {
        switch v1.Kind() {
        case Array:
                for i := 0; i < v1.Len(); i++ {
-                       if !deepValueEqual(v1.Index(i), v2.Index(i), visited, depth+1) {
+                       if !deepValueEqual(v1.Index(i), v2.Index(i), visited) {
                                return false
                        }
                }
@@ -95,7 +93,7 @@ func deepValueEqual(v1, v2 Value, visited map[visit]bool, depth int) bool {
                        return true
                }
                for i := 0; i < v1.Len(); i++ {
-                       if !deepValueEqual(v1.Index(i), v2.Index(i), visited, depth+1) {
+                       if !deepValueEqual(v1.Index(i), v2.Index(i), visited) {
                                return false
                        }
                }
@@ -104,15 +102,15 @@ func deepValueEqual(v1, v2 Value, visited map[visit]bool, depth int) bool {
                if v1.IsNil() || v2.IsNil() {
                        return v1.IsNil() == v2.IsNil()
                }
-               return deepValueEqual(v1.Elem(), v2.Elem(), visited, depth+1)
+               return deepValueEqual(v1.Elem(), v2.Elem(), visited)
        case Ptr:
                if v1.Pointer() == v2.Pointer() {
                        return true
                }
-               return deepValueEqual(v1.Elem(), v2.Elem(), visited, depth+1)
+               return deepValueEqual(v1.Elem(), v2.Elem(), visited)
        case Struct:
                for i, n := 0, v1.NumField(); i < n; i++ {
-                       if !deepValueEqual(v1.Field(i), v2.Field(i), visited, depth+1) {
+                       if !deepValueEqual(v1.Field(i), v2.Field(i), visited) {
                                return false
                        }
                }
@@ -130,7 +128,7 @@ func deepValueEqual(v1, v2 Value, visited map[visit]bool, depth int) bool {
                for _, k := range v1.MapKeys() {
                        val1 := v1.MapIndex(k)
                        val2 := v2.MapIndex(k)
-                       if !val1.IsValid() || !val2.IsValid() || !deepValueEqual(val1, val2, visited, depth+1) {
+                       if !val1.IsValid() || !val2.IsValid() || !deepValueEqual(val1, val2, visited) {
                                return false
                        }
                }
@@ -207,5 +205,5 @@ func DeepEqual(x, y interface{}) bool {
        if v1.Type() != v2.Type() {
                return false
        }
-       return deepValueEqual(v1, v2, make(map[visit]bool), 0)
+       return deepValueEqual(v1, v2, make(map[visit]bool))
 }