@@ -18,6 +18,7 @@ package typed
1818
1919import (
2020 "fmt"
21+ "sync"
2122
2223 "sigs.k8s.io/structured-merge-diff/fieldpath"
2324 "sigs.k8s.io/structured-merge-diff/schema"
@@ -66,7 +67,9 @@ func (tv TypedValue) AsValue() *value.Value {
6667
6768// Validate returns an error with a list of every spec violation.
6869func (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 {
7073 return errs
7174 }
7275 return nil
@@ -77,6 +80,7 @@ func (tv TypedValue) Validate() error {
7780func (tv TypedValue ) ToFieldSet () (* fieldpath.Set , error ) {
7881 s := fieldpath .NewSet ()
7982 w := tv .walker ()
83+ defer w .finished ()
8084 w .leafFieldCallback = func (p fieldpath.Path ) { s .Insert (p ) }
8185 w .nodeFieldCallback = func (p fieldpath.Path ) { s .Insert (p ) }
8286 if errs := w .validate (); len (errs ) != 0 {
@@ -207,6 +211,10 @@ func (tv TypedValue) Empty() *TypedValue {
207211 return & tv
208212}
209213
214+ var mwPool = sync.Pool {
215+ New : func () interface {} { return & mergingWalker {} },
216+ }
217+
210218func merge (lhs , rhs * TypedValue , rule , postRule mergeRule ) (* TypedValue , error ) {
211219 if lhs .schema != rhs .schema {
212220 return nil , errorFormatter {}.
@@ -217,14 +225,27 @@ func merge(lhs, rhs *TypedValue, rule, postRule mergeRule) (*TypedValue, error)
217225 errorf ("expected objects of the same type, but got %v and %v" , lhs .typeRef , rhs .typeRef )
218226 }
219227
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+
228249 errs := mw .merge ()
229250 if len (errs ) > 0 {
230251 return nil , errs
0 commit comments