Skip to content

Commit 8ff7628

Browse files
committed
Estimating the size of list when its possible to reduce memory usage and improve performance
1 parent cfe7579 commit 8ff7628

File tree

3 files changed

+6
-3
lines changed

3 files changed

+6
-3
lines changed

pkg/k8s/parse_annotations.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ func NewService(cfg config.Config, pod *corev1.Pod) *ParserService {
5252
}
5353

5454
func (s *ParserService) GetPodDbConfig() (*podDbConfig, error) {
55-
var dbConfigurations []DbConfiguration
55+
estimatedSize := len(s.pod.Annotations)
56+
dbConfigurations := make([]DbConfiguration, 0, estimatedSize)
5657
vaultDbPath, ok := s.pod.Annotations[ANNOTATION_VAULT_DB_PATH]
5758
if !ok || vaultDbPath == "" {
5859
vaultDbPath = s.cfg.DefaultEngine

pkg/k8s/pod_utils.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,9 @@ func (p *podServiceImpl) GetAllPodAndNamespace(ctx context.Context) ([]PodInform
5555
return nil, errors.Newf("no pods found in the cluster")
5656
}
5757

58-
var podInfos []PodInformations
58+
estimatedSize := len(pods.Items)
59+
podInfos := make([]PodInformations, 0, estimatedSize)
60+
5961
for _, pod := range pods.Items {
6062
if uuid, exists := pod.GetAnnotations()[ANNOTATION_VAULT_POD_UUID]; exists {
6163
podInfos = append(podInfos, PodInformations{

pkg/k8smutator/k8smutator.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ func generateUUID(logger log.Logger) string {
8383

8484
func handlePodConfiguration(ctx context.Context, cfg *config.Config, dbConfs *[]k8s.DbConfiguration, logger log.Logger, vaultDbPath, tok string, pod *corev1.Pod) (*corev1.Pod, string, []string, error) {
8585
if len(*dbConfs) > 0 {
86-
var podUuids []string
86+
podUuids := make([]string, 0, len(*dbConfs))
8787
for _, dbConf := range *dbConfs {
8888
// Configure vault connection using serviceAccount token
8989
err := checkConfiguration(dbConf)

0 commit comments

Comments
 (0)