From 5affdbaaafcfdffcf5c8907f3b1cd10f1571575e Mon Sep 17 00:00:00 2001 From: mgianluc Date: Mon, 4 Nov 2024 14:22:26 +0100 Subject: [PATCH] Where possible, set slice capacity Avoid whenever possible, append to cause copy --- controllers/handlers_helm.go | 16 ++++++++-------- controllers/handlers_kustomize.go | 9 +++++++-- controllers/handlers_resources.go | 4 ++-- controllers/handlers_utils.go | 10 +++++----- controllers/profile_utils.go | 4 ++-- controllers/reloader_utils.go | 2 +- controllers/set_utils.go | 2 +- 7 files changed, 26 insertions(+), 21 deletions(-) diff --git a/controllers/handlers_helm.go b/controllers/handlers_helm.go index 774c1111..a402ac0b 100644 --- a/controllers/handlers_helm.go +++ b/controllers/handlers_helm.go @@ -516,8 +516,8 @@ func walkChartsAndDeploy(ctx context.Context, c client.Client, clusterSummary *c } conflictErrorMessage := "" - releaseReports := make([]configv1beta1.ReleaseReport, 0) - chartDeployed := make([]configv1beta1.Chart, 0) + releaseReports := make([]configv1beta1.ReleaseReport, 0, len(clusterSummary.Spec.ClusterProfileSpec.HelmCharts)) + chartDeployed := make([]configv1beta1.Chart, 0, len(clusterSummary.Spec.ClusterProfileSpec.HelmCharts)) for i := range clusterSummary.Spec.ClusterProfileSpec.HelmCharts { currentChart := &clusterSummary.Spec.ClusterProfileSpec.HelmCharts[i] // Eventual conflicts are already resolved before this method is called (in updateStatusForeferencedHelmReleases) @@ -1703,7 +1703,7 @@ func updateStatusForNonReferencedHelmReleases(ctx context.Context, c client.Clie currentlyReferenced[helmInfo(currentChart.ReleaseNamespace, currentChart.ReleaseName)] = true } - helmReleaseSummaries := make([]configv1beta1.HelmChartSummary, 0) + helmReleaseSummaries := make([]configv1beta1.HelmChartSummary, 0, len(clusterSummary.Status.HelmReleaseSummaries)) for i := range clusterSummary.Status.HelmReleaseSummaries { summary := &clusterSummary.Status.HelmReleaseSummaries[i] if _, ok := currentlyReferenced[helmInfo(summary.ReleaseNamespace, summary.ReleaseName)]; ok { @@ -1866,7 +1866,7 @@ func collectResourcesFromManagedHelmChartsForDriftDetection(ctx context.Context, return nil, err } - helmResources := make([]libsveltosv1beta1.HelmResources, 0) + helmResources := make([]libsveltosv1beta1.HelmResources, 0, len(clusterSummary.Spec.ClusterProfileSpec.HelmCharts)) for i := range clusterSummary.Spec.ClusterProfileSpec.HelmCharts { currentChart := &clusterSummary.Spec.ClusterProfileSpec.HelmCharts[i] @@ -1927,13 +1927,13 @@ func collectResourcesFromManagedHelmChartsForDriftDetection(ctx context.Context, } func collectHelmContent(manifest string, logger logr.Logger) ([]*unstructured.Unstructured, error) { - resources := make([]*unstructured.Unstructured, 0) - elements, err := customSplit(manifest) if err != nil { return nil, err } + resources := make([]*unstructured.Unstructured, 0, len(elements)) + for i := range elements { policy, err := utils.GetUnstructured([]byte(elements[i])) if err != nil { @@ -1948,7 +1948,7 @@ func collectHelmContent(manifest string, logger logr.Logger) ([]*unstructured.Un } func unstructuredToSveltosResources(policies []*unstructured.Unstructured) []libsveltosv1beta1.Resource { - resources := make([]libsveltosv1beta1.Resource, 0) + resources := make([]libsveltosv1beta1.Resource, len(policies)) for i := range policies { r := libsveltosv1beta1.Resource{ @@ -1960,7 +1960,7 @@ func unstructuredToSveltosResources(policies []*unstructured.Unstructured) []lib IgnoreForConfigurationDrift: hasIgnoreConfigurationDriftAnnotation(policies[i]), } - resources = append(resources, r) + resources[i] = r } return resources diff --git a/controllers/handlers_kustomize.go b/controllers/handlers_kustomize.go index 95b85042..5dc15058 100644 --- a/controllers/handlers_kustomize.go +++ b/controllers/handlers_kustomize.go @@ -135,9 +135,9 @@ func deployKustomizeRefs(ctx context.Context, c client.Client, } // If we are here there are no conflicts (and error would have been returned by deployKustomizeRef) - remoteDeployed := make([]configv1beta1.Resource, 0) + remoteDeployed := make([]configv1beta1.Resource, len(remoteResourceReports)) for i := range remoteResourceReports { - remoteDeployed = append(remoteDeployed, remoteResourceReports[i].Resource) + remoteDeployed[i] = remoteResourceReports[i].Resource } // TODO: track resource deployed in the management cluster @@ -639,6 +639,8 @@ func getKustomizedResources(ctx context.Context, c client.Client, clusterSummary } resources := resMap.Resources() + objectsToDeployLocally = make([]*unstructured.Unstructured, 0, len(resources)) + objectsToDeployRemotely = make([]*unstructured.Unstructured, 0, len(resources)) for i := range resources { resource := resources[i] yaml, err := resource.AsYAML() @@ -821,6 +823,9 @@ func deployEachKustomizeRefs(ctx context.Context, c client.Client, remoteRestCon clusterSummary *configv1beta1.ClusterSummary, logger logr.Logger, ) (localResourceReports, remoteResourceReports []configv1beta1.ResourceReport, err error) { + capacity := len(clusterSummary.Spec.ClusterProfileSpec.KustomizationRefs) + localResourceReports = make([]configv1beta1.ResourceReport, 0, capacity) + remoteResourceReports = make([]configv1beta1.ResourceReport, 0, capacity) for i := range clusterSummary.Spec.ClusterProfileSpec.KustomizationRefs { kustomizationRef := &clusterSummary.Spec.ClusterProfileSpec.KustomizationRefs[i] var tmpLocal []configv1beta1.ResourceReport diff --git a/controllers/handlers_resources.go b/controllers/handlers_resources.go index 43ec9441..c7252111 100644 --- a/controllers/handlers_resources.go +++ b/controllers/handlers_resources.go @@ -89,9 +89,9 @@ func deployResources(ctx context.Context, c client.Client, return err } - remoteDeployed := make([]configv1beta1.Resource, 0) + remoteDeployed := make([]configv1beta1.Resource, len(remoteResourceReports)) for i := range remoteResourceReports { - remoteDeployed = append(remoteDeployed, remoteResourceReports[i].Resource) + remoteDeployed[i] = remoteResourceReports[i].Resource } // TODO: track resource deployed in the management cluster diff --git a/controllers/handlers_utils.go b/controllers/handlers_utils.go index aa4ba84e..aa18f7cc 100644 --- a/controllers/handlers_utils.go +++ b/controllers/handlers_utils.go @@ -745,7 +745,7 @@ func collectContent(ctx context.Context, clusterSummary *configv1beta1.ClusterSu instantiateTemplate bool, logger logr.Logger, ) ([]*unstructured.Unstructured, error) { - policies := make([]*unstructured.Unstructured, 0) + policies := make([]*unstructured.Unstructured, 0, len(data)) for k := range data { section := data[k] @@ -789,12 +789,11 @@ func collectContent(ctx context.Context, clusterSummary *configv1beta1.ClusterSu } func getUnstructured(section []byte, logger logr.Logger) ([]*unstructured.Unstructured, error) { - policies := make([]*unstructured.Unstructured, 0) elements, err := customSplit(string(section)) if err != nil { return nil, err } - + policies := make([]*unstructured.Unstructured, 0, len(elements)) for i := range elements { policy, err := utils.GetUnstructured([]byte(elements[i])) if err != nil { @@ -896,8 +895,8 @@ func collectReferencedObjects(ctx context.Context, controlClusterClient client.C clusterSummary *configv1beta1.ClusterSummary, references []configv1beta1.PolicyRef, logger logr.Logger) (local, remote []client.Object, err error) { - local = make([]client.Object, 0) - remote = make([]client.Object, 0) + local = make([]client.Object, 0, len(references)) + remote = make([]client.Object, 0, len(references)) for i := range references { var object client.Object reference := &references[i] @@ -999,6 +998,7 @@ func deployObjects(ctx context.Context, deployingToMgmtCluster bool, destClient mgmtResources map[string]*unstructured.Unstructured, logger logr.Logger, ) (reports []configv1beta1.ResourceReport, err error) { + reports = make([]configv1beta1.ResourceReport, 0, len(referencedObjects)) for i := range referencedObjects { var tmpResourceReports []configv1beta1.ResourceReport if referencedObjects[i].GetObjectKind().GroupVersionKind().Kind == string(libsveltosv1beta1.ConfigMapReferencedResourceKind) { diff --git a/controllers/profile_utils.go b/controllers/profile_utils.go index b9497c91..d9f4d41a 100644 --- a/controllers/profile_utils.go +++ b/controllers/profile_utils.go @@ -884,7 +884,7 @@ func reviseUpdatedAndUpdatingClusters(profileScope *scope.ProfileScope) { } updatedClusters := &libsveltosset.Set{} - currentUpdatedClusters := make([]corev1.ObjectReference, 0) + currentUpdatedClusters := make([]corev1.ObjectReference, 0, len(profileScope.GetStatus().UpdatedClusters.Clusters)) for i := range profileScope.GetStatus().UpdatedClusters.Clusters { cluster := &profileScope.GetStatus().UpdatedClusters.Clusters[i] if matchingCluster.Has(cluster) { @@ -896,7 +896,7 @@ func reviseUpdatedAndUpdatingClusters(profileScope *scope.ProfileScope) { profileScope.GetStatus().UpdatedClusters.Clusters = currentUpdatedClusters updatingClusters := &libsveltosset.Set{} - currentUpdatingClusters := make([]corev1.ObjectReference, 0) + currentUpdatingClusters := make([]corev1.ObjectReference, 0, len(profileScope.GetStatus().UpdatingClusters.Clusters)) for i := range profileScope.GetStatus().UpdatingClusters.Clusters { cluster := &profileScope.GetStatus().UpdatingClusters.Clusters[i] if matchingCluster.Has(cluster) { diff --git a/controllers/reloader_utils.go b/controllers/reloader_utils.go index fbb492a1..d57ed0bf 100644 --- a/controllers/reloader_utils.go +++ b/controllers/reloader_utils.go @@ -90,7 +90,7 @@ func deployReloaderInstance(ctx context.Context, remoteClient client.Client, clusterProfileName string, feature configv1beta1.FeatureID, resources []corev1.ObjectReference, logger logr.Logger) error { - reloaderInfo := make([]libsveltosv1beta1.ReloaderInfo, 0) + reloaderInfo := make([]libsveltosv1beta1.ReloaderInfo, 0, len(resources)) for i := range resources { resource := &resources[i] if watchForRollingUpgrade(resource) { diff --git a/controllers/set_utils.go b/controllers/set_utils.go index 30d73053..dc1bd4e3 100644 --- a/controllers/set_utils.go +++ b/controllers/set_utils.go @@ -53,7 +53,7 @@ func selectClusters(ctx context.Context, c client.Client, setScope *scope.SetSco currentMatchingHealthyCluster[healthyMatchingClusters[i]] = true } - currentSelectedClusters := make([]corev1.ObjectReference, 0) + currentSelectedClusters := make([]corev1.ObjectReference, 0, len(status.SelectedClusterRefs)) for i := range status.SelectedClusterRefs { cluster := &status.SelectedClusterRefs[i] if _, ok := currentMatchingHealthyCluster[*cluster]; ok {