From 4f39b164f3a52b121a11e2ac91f5fa147ca39ca0 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Tue, 24 Jan 2023 20:42:46 +0100 Subject: [PATCH] pkg/cri: optimize slice initialization Some of this code was originally added in b7b1200dd3716c88312f6ce8fafdb2c2881edb29, which likely meant to initialize the slice with a length to reduce allocations, however, instead of initializing with a zero-length and a capacity, it initialized the slice with a fixed length, which was corrected in commit 0c63c42f8183d13c2c106c01f5bb3560d39b3295. This patch initializes the slice with a zero-length and expected capacity. Signed-off-by: Sebastiaan van Stijn --- pkg/cri/sbserver/helpers.go | 2 +- pkg/cri/server/helpers.go | 2 +- pkg/cri/store/container/status.go | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/pkg/cri/sbserver/helpers.go b/pkg/cri/sbserver/helpers.go index d83b67b86..eb7afb660 100644 --- a/pkg/cri/sbserver/helpers.go +++ b/pkg/cri/sbserver/helpers.go @@ -478,7 +478,7 @@ func copyResourcesToStatus(spec *runtimespec.Spec, status containerstore.Status) } if spec.Linux.Resources.HugepageLimits != nil { - hugepageLimits := make([]*runtime.HugepageLimit, 0) + hugepageLimits := make([]*runtime.HugepageLimit, 0, len(spec.Linux.Resources.HugepageLimits)) for _, l := range spec.Linux.Resources.HugepageLimits { hugepageLimits = append(hugepageLimits, &runtime.HugepageLimit{ PageSize: l.Pagesize, diff --git a/pkg/cri/server/helpers.go b/pkg/cri/server/helpers.go index 5f3ba0fb9..9844b5564 100644 --- a/pkg/cri/server/helpers.go +++ b/pkg/cri/server/helpers.go @@ -476,7 +476,7 @@ func copyResourcesToStatus(spec *runtimespec.Spec, status containerstore.Status) } if spec.Linux.Resources.HugepageLimits != nil { - hugepageLimits := make([]*runtime.HugepageLimit, 0) + hugepageLimits := make([]*runtime.HugepageLimit, 0, len(spec.Linux.Resources.HugepageLimits)) for _, l := range spec.Linux.Resources.HugepageLimits { hugepageLimits = append(hugepageLimits, &runtime.HugepageLimit{ PageSize: l.Pagesize, diff --git a/pkg/cri/store/container/status.go b/pkg/cri/store/container/status.go index a2e2d585e..0f6d38b4c 100644 --- a/pkg/cri/store/container/status.go +++ b/pkg/cri/store/container/status.go @@ -219,7 +219,7 @@ func deepCopyOf(s Status) Status { } copy.Resources = &runtime.ContainerResources{} if s.Resources != nil && s.Resources.Linux != nil { - hugepageLimits := make([]*runtime.HugepageLimit, 0) + hugepageLimits := make([]*runtime.HugepageLimit, 0, len(s.Resources.Linux.HugepageLimits)) for _, l := range s.Resources.Linux.HugepageLimits { if l != nil { hugepageLimits = append(hugepageLimits, &runtime.HugepageLimit{