@@ -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 {
@@ -207,6 +211,10 @@ func (tv TypedValue) Empty() *TypedValue {
207
211
return & tv
208
212
}
209
213
214
+ var mwPool = sync.Pool {
215
+ New : func () interface {} { return & mergingWalker {} },
216
+ }
217
+
210
218
func merge (lhs , rhs * TypedValue , rule , postRule mergeRule ) (* TypedValue , error ) {
211
219
if lhs .schema != rhs .schema {
212
220
return nil , errorFormatter {}.
@@ -217,14 +225,27 @@ func merge(lhs, rhs *TypedValue, rule, postRule mergeRule) (*TypedValue, error)
217
225
errorf ("expected objects of the same type, but got %v and %v" , lhs .typeRef , rhs .typeRef )
218
226
}
219
227
220
- mw := mergingWalker {
221
- lhs : & lhs .value ,
222
- rhs : & rhs .value ,
223
- schema : lhs .schema ,
224
- typeRef : lhs .typeRef ,
225
- rule : rule ,
226
- postItemHook : postRule ,
227
- }
228
+ mw := mwPool .Get ().(* mergingWalker )
229
+ defer func () {
230
+ mw .lhs = nil
231
+ mw .rhs = nil
232
+ mw .schema = nil
233
+ mw .typeRef = schema.TypeRef {}
234
+ mw .rule = nil
235
+ mw .postItemHook = nil
236
+ mw .out = nil
237
+ mw .inLeaf = false
238
+
239
+ mwPool .Put (mw )
240
+ }()
241
+
242
+ mw .lhs = & lhs .value
243
+ mw .rhs = & rhs .value
244
+ mw .schema = lhs .schema
245
+ mw .typeRef = lhs .typeRef
246
+ mw .rule = rule
247
+ mw .postItemHook = postRule
248
+
228
249
errs := mw .merge ()
229
250
if len (errs ) > 0 {
230
251
return nil , errs
0 commit comments