@@ -18,6 +18,7 @@ package typed
18
18
19
19
import (
20
20
"fmt"
21
+ "sync"
21
22
22
23
"sigs.k8s.io/structured-merge-diff/fieldpath"
23
24
"sigs.k8s.io/structured-merge-diff/schema"
@@ -66,7 +67,9 @@ func (tv TypedValue) AsValue() *value.Value {
66
67
67
68
// Validate returns an error with a list of every spec violation.
68
69
func (tv TypedValue ) Validate () error {
69
- if errs := tv .walker ().validate (); len (errs ) != 0 {
70
+ w := tv .walker ()
71
+ defer w .finished ()
72
+ if errs := w .validate (); len (errs ) != 0 {
70
73
return errs
71
74
}
72
75
return nil
@@ -77,6 +80,7 @@ func (tv TypedValue) Validate() error {
77
80
func (tv TypedValue ) ToFieldSet () (* fieldpath.Set , error ) {
78
81
s := fieldpath .NewSet ()
79
82
w := tv .walker ()
83
+ defer w .finished ()
80
84
w .leafFieldCallback = func (p fieldpath.Path ) { s .Insert (p ) }
81
85
w .nodeFieldCallback = func (p fieldpath.Path ) { s .Insert (p ) }
82
86
if errs := w .validate (); len (errs ) != 0 {
@@ -203,6 +207,10 @@ func (tv TypedValue) Empty() *TypedValue {
203
207
return & tv
204
208
}
205
209
210
+ var mwPool = sync.Pool {
211
+ New : func () interface {} { return & mergingWalker {} },
212
+ }
213
+
206
214
func merge (lhs , rhs * TypedValue , rule , postRule mergeRule ) (* TypedValue , error ) {
207
215
if lhs .schema != rhs .schema {
208
216
return nil , errorFormatter {}.
@@ -213,14 +221,27 @@ func merge(lhs, rhs *TypedValue, rule, postRule mergeRule) (*TypedValue, error)
213
221
errorf ("expected objects of the same type, but got %v and %v" , lhs .typeRef , rhs .typeRef )
214
222
}
215
223
216
- mw := mergingWalker {
217
- lhs : & lhs .value ,
218
- rhs : & rhs .value ,
219
- schema : lhs .schema ,
220
- typeRef : lhs .typeRef ,
221
- rule : rule ,
222
- postItemHook : postRule ,
223
- }
224
+ mw := mwPool .Get ().(* mergingWalker )
225
+ defer func () {
226
+ mw .lhs = nil
227
+ mw .rhs = nil
228
+ mw .schema = nil
229
+ mw .typeRef = schema.TypeRef {}
230
+ mw .rule = nil
231
+ mw .postItemHook = nil
232
+ mw .out = nil
233
+ mw .inLeaf = false
234
+
235
+ mwPool .Put (mw )
236
+ }()
237
+
238
+ mw .lhs = & lhs .value
239
+ mw .rhs = & rhs .value
240
+ mw .schema = lhs .schema
241
+ mw .typeRef = lhs .typeRef
242
+ mw .rule = rule
243
+ mw .postItemHook = postRule
244
+
224
245
errs := mw .merge ()
225
246
if len (errs ) > 0 {
226
247
return nil , errs
0 commit comments