encoding: avoid setting GVK unnecessarily

Setting the group/version/kind is not necessary when the object already has it.
In that particular case some extra work and the data race when the
same object is used multiple times in parallel can be avoided.
This commit is contained in:
Patrick Ohly 2023-11-07 08:11:44 +01:00
parent ad18954259
commit f0aab8c984

View File

@ -236,10 +236,14 @@ func (e WithVersionEncoder) Encode(obj Object, stream io.Writer) error {
gvk = preferredGVK
}
}
// The gvk only needs to be set if not already as desired.
if gvk != oldGVK {
kind.SetGroupVersionKind(gvk)
err = e.Encoder.Encode(obj, stream)
kind.SetGroupVersionKind(oldGVK)
return err
defer kind.SetGroupVersionKind(oldGVK)
}
return e.Encoder.Encode(obj, stream)
}
// WithoutVersionDecoder clears the group version kind of a deserialized object.