1
0
mirror of https://github.com/golang/go synced 2024-11-17 06:54:48 -07:00

reflect: optimize DeepEqual() for maps

benchmark                     old ns/op     new ns/op     delta
BenchmarkMapsDeepEqual-10     235           200           -15.05%

benchmark                     old allocs     new allocs     delta
BenchmarkMapsDeepEqual-10     7              6              -14.29%

benchmark                     old bytes     new bytes     delta
BenchmarkMapsDeepEqual-10     96            48            -50.00%

Change-Id: Ifa625ad25524cc9ee438711917606626b33a9597
Reviewed-on: https://go-review.googlesource.com/c/go/+/512576
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: David Chase <drchase@google.com>
Auto-Submit: Ian Lance Taylor <iant@google.com>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Run-TryBot: Ian Lance Taylor <iant@google.com>
Run-TryBot: Ian Lance Taylor <iant@golang.org>
This commit is contained in:
korzhao 2023-07-06 11:12:29 +08:00 committed by Gopher Robot
parent c17e0cd2da
commit f024e390bb
2 changed files with 16 additions and 3 deletions

View File

@ -107,6 +107,18 @@ func BenchmarkDeepEqual(b *testing.B) {
}
}
func BenchmarkMapsDeepEqual(b *testing.B) {
m1 := map[int]int{
1: 1, 2: 2,
}
m2 := map[int]int{
1: 1, 2: 2,
}
for i := 0; i < b.N; i++ {
DeepEqual(m1, m2)
}
}
func BenchmarkIsZero(b *testing.B) {
source := ValueOf(struct {
ArrayComparable [4]T

View File

@ -142,9 +142,10 @@ func deepValueEqual(v1, v2 Value, visited map[visit]bool) bool {
if v1.UnsafePointer() == v2.UnsafePointer() {
return true
}
for _, k := range v1.MapKeys() {
val1 := v1.MapIndex(k)
val2 := v2.MapIndex(k)
iter := v1.MapRange()
for iter.Next() {
val1 := iter.Value()
val2 := v2.MapIndex(iter.Key())
if !val1.IsValid() || !val2.IsValid() || !deepValueEqual(val1, val2, visited) {
return false
}