mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-09 12:07:47 +00:00
Avoid allocating an array for EncodeToVersion
This commit is contained in:
parent
b1a759cbbc
commit
a9c3edc613
@ -104,6 +104,11 @@ func NewCodec(
|
|||||||
}
|
}
|
||||||
internal.encodeVersion[v.Group] = v
|
internal.encodeVersion[v.Group] = v
|
||||||
}
|
}
|
||||||
|
if len(internal.encodeVersion) == 1 {
|
||||||
|
for _, v := range internal.encodeVersion {
|
||||||
|
internal.preferredEncodeVersion = []unversioned.GroupVersion{v}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if decodeVersion != nil {
|
if decodeVersion != nil {
|
||||||
internal.decodeVersion = make(map[string]unversioned.GroupVersion)
|
internal.decodeVersion = make(map[string]unversioned.GroupVersion)
|
||||||
@ -129,6 +134,8 @@ type codec struct {
|
|||||||
|
|
||||||
encodeVersion map[string]unversioned.GroupVersion
|
encodeVersion map[string]unversioned.GroupVersion
|
||||||
decodeVersion map[string]unversioned.GroupVersion
|
decodeVersion map[string]unversioned.GroupVersion
|
||||||
|
|
||||||
|
preferredEncodeVersion []unversioned.GroupVersion
|
||||||
}
|
}
|
||||||
|
|
||||||
// Decode attempts a decode of the object, then tries to convert it to the internal version. If into is provided and the decoding is
|
// Decode attempts a decode of the object, then tries to convert it to the internal version. If into is provided and the decoding is
|
||||||
@ -248,13 +255,16 @@ func (c *codec) EncodeToStream(obj runtime.Object, w io.Writer, overrides ...unv
|
|||||||
}
|
}
|
||||||
|
|
||||||
// attempt a conversion to the sole encode version
|
// attempt a conversion to the sole encode version
|
||||||
if !ok && len(c.encodeVersion) == 1 {
|
if !ok && c.preferredEncodeVersion != nil {
|
||||||
ok = true
|
ok = true
|
||||||
for _, v := range c.encodeVersion {
|
targetGV = c.preferredEncodeVersion[0]
|
||||||
targetGV = v
|
if len(overrides) > 0 {
|
||||||
|
// ensure the target override is first
|
||||||
|
overrides = promoteOrPrependGroupVersion(targetGV, overrides)
|
||||||
|
} else {
|
||||||
|
// avoids allocating a new array for each call to EncodeToVersion
|
||||||
|
overrides = c.preferredEncodeVersion
|
||||||
}
|
}
|
||||||
// ensure the target override is first
|
|
||||||
overrides = promoteOrPrependGroupVersion(targetGV, overrides)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// if no fallback is available, error
|
// if no fallback is available, error
|
||||||
|
Loading…
Reference in New Issue
Block a user