From dcbd309945244c82d96c77a35f6f08d626b5695e Mon Sep 17 00:00:00 2001 From: Artur Melanchyk Date: Wed, 15 Jan 2025 19:35:16 +0100 Subject: [PATCH] flags: optimise memory allocation Signed-off-by: Artur Melanchyk --- pkg/flags/selective_string.go | 4 ++-- pkg/flags/unique_strings.go | 5 +++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/pkg/flags/selective_string.go b/pkg/flags/selective_string.go index 4b90fbf4b49..cfba14455b4 100644 --- a/pkg/flags/selective_string.go +++ b/pkg/flags/selective_string.go @@ -59,7 +59,7 @@ func (ss *SelectiveStringValue) Valids() []string { // valids[0] will be default value. Caller must be sure // len(valids) != 0 or it will panic. func NewSelectiveStringValue(valids ...string) *SelectiveStringValue { - vm := make(map[string]struct{}) + vm := make(map[string]struct{}, len(valids)) for _, v := range valids { vm[v] = struct{}{} } @@ -106,7 +106,7 @@ func (ss *SelectiveStringsValue) Valids() []string { // for which any one of the given strings is a valid value, // and any other value is an error. func NewSelectiveStringsValue(valids ...string) *SelectiveStringsValue { - vm := make(map[string]struct{}) + vm := make(map[string]struct{}, len(valids)) for _, v := range valids { vm[v] = struct{}{} } diff --git a/pkg/flags/unique_strings.go b/pkg/flags/unique_strings.go index e67af1f9b5a..575516cb7e2 100644 --- a/pkg/flags/unique_strings.go +++ b/pkg/flags/unique_strings.go @@ -31,8 +31,9 @@ type UniqueStringsValue struct { // Implements "flag.Value" interface. // The values are set in order. func (us *UniqueStringsValue) Set(s string) error { - us.Values = make(map[string]struct{}) - for _, v := range strings.Split(s, ",") { + values := strings.Split(s, ",") + us.Values = make(map[string]struct{}, len(values)) + for _, v := range values { us.Values[v] = struct{}{} } return nil