diff --git a/changelog/v0.38.2/less-snapshot-allocs.yaml b/changelog/v0.38.2/less-snapshot-allocs.yaml new file mode 100644 index 000000000..444383801 --- /dev/null +++ b/changelog/v0.38.2/less-snapshot-allocs.yaml @@ -0,0 +1,3 @@ +changelog: + - type: NON_USER_FACING + description: Use make + len for snapshot copies to avoid allocations from growing the maps. diff --git a/pkg/resource/snapshot.go b/pkg/resource/snapshot.go index 0c5442ee1..c29a8fdb5 100644 --- a/pkg/resource/snapshot.go +++ b/pkg/resource/snapshot.go @@ -62,7 +62,7 @@ func (s Snapshot) cloneInternal(deepCopy bool, selectors ...GVKSelectorFunc) Sna if deepCopy { clone[k] = copyNnsMap(v) } else { - clone[k] = map[types.NamespacedName]client.Object{} + clone[k] = make(map[types.NamespacedName]client.Object, len(v)) maps.Copy(clone[k], v) } continue @@ -78,7 +78,7 @@ func (s Snapshot) cloneInternal(deepCopy bool, selectors ...GVKSelectorFunc) Sna if deepCopy { clone[k] = copyNnsMap(v) } else { - clone[k] = map[types.NamespacedName]client.Object{} + clone[k] = make(map[types.NamespacedName]client.Object, len(v)) maps.Copy(clone[k], v) } continue @@ -127,7 +127,7 @@ func (s ClusterSnapshot) ForEachObject( } func copyNnsMap(m map[types.NamespacedName]client.Object) map[types.NamespacedName]client.Object { - nnsMapCopy := map[types.NamespacedName]client.Object{} + nnsMapCopy := make(map[types.NamespacedName]client.Object, len(m)) for k, v := range m { nnsMapCopy[k] = v.DeepCopyObject().(client.Object) }