Skip to content

Commit fa1e46d

Browse files
authored
Merge pull request #239 from apelisse/use-using-recursively
Use EqualsUsing recursively
2 parents 1dca6fb + 8bff488 commit fa1e46d

File tree

3 files changed

+93
-5
lines changed

3 files changed

+93
-5
lines changed

value/equals_test.go

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
Copyright 2023 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package value_test
18+
19+
import (
20+
"io/ioutil"
21+
"path/filepath"
22+
"testing"
23+
24+
"gopkg.in/yaml.v2"
25+
"sigs.k8s.io/structured-merge-diff/v4/value"
26+
)
27+
28+
func testdata(file string) string {
29+
return filepath.Join("..", "internal", "testdata", file)
30+
}
31+
32+
func read(file string) []byte {
33+
s, err := ioutil.ReadFile(file)
34+
if err != nil {
35+
panic(err)
36+
}
37+
return s
38+
}
39+
40+
func BenchmarkEquals(b *testing.B) {
41+
benches := []struct {
42+
filename string
43+
}{
44+
{
45+
filename: "pod.yaml",
46+
},
47+
{
48+
filename: "endpoints.yaml",
49+
},
50+
{
51+
filename: "list.yaml",
52+
},
53+
{
54+
filename: "node.yaml",
55+
},
56+
{
57+
filename: "prometheus-crd.yaml",
58+
},
59+
}
60+
61+
for _, bench := range benches {
62+
b.Run(bench.filename, func(b *testing.B) {
63+
var obj interface{}
64+
err := yaml.Unmarshal(read(testdata(bench.filename)), &obj)
65+
if err != nil {
66+
b.Fatalf("Failed to unmarshal object: %v", err)
67+
}
68+
v := value.NewValueInterface(obj)
69+
b.Run("Equals", func(b *testing.B) {
70+
b.ReportAllocs()
71+
for i := 0; i < b.N; i++ {
72+
if !value.Equals(v, v) {
73+
b.Fatalf("Object should be equal")
74+
}
75+
}
76+
})
77+
b.Run("EqualsUsingFreelist", func(b *testing.B) {
78+
b.ReportAllocs()
79+
a := value.NewFreelistAllocator()
80+
for i := 0; i < b.N; i++ {
81+
if !value.EqualsUsing(a, v, v) {
82+
b.Fatalf("Object should be equal")
83+
}
84+
}
85+
})
86+
})
87+
}
88+
}

value/mapreflect.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ func (r mapReflect) EqualsUsing(a Allocator, m Map) bool {
136136
if !ok {
137137
return false
138138
}
139-
return Equals(vr.mustReuse(lhsVal, entry, nil, nil), value)
139+
return EqualsUsing(a, vr.mustReuse(lhsVal, entry, nil, nil), value)
140140
})
141141
}
142142

value/mapunstructured.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,12 @@ func (m mapUnstructuredInterface) EqualsUsing(a Allocator, other Map) bool {
8888
}
8989
vv := a.allocValueUnstructured()
9090
defer a.Free(vv)
91-
return other.Iterate(func(key string, value Value) bool {
91+
return other.IterateUsing(a, func(key string, value Value) bool {
9292
lhsVal, ok := m[key]
9393
if !ok {
9494
return false
9595
}
96-
return Equals(vv.reuse(lhsVal), value)
96+
return EqualsUsing(a, vv.reuse(lhsVal), value)
9797
})
9898
}
9999

@@ -168,12 +168,12 @@ func (m mapUnstructuredString) EqualsUsing(a Allocator, other Map) bool {
168168
}
169169
vv := a.allocValueUnstructured()
170170
defer a.Free(vv)
171-
return other.Iterate(func(key string, value Value) bool {
171+
return other.IterateUsing(a, func(key string, value Value) bool {
172172
lhsVal, ok := m[key]
173173
if !ok {
174174
return false
175175
}
176-
return Equals(vv.reuse(lhsVal), value)
176+
return EqualsUsing(a, vv.reuse(lhsVal), value)
177177
})
178178
}
179179

0 commit comments

Comments
 (0)