Skip to content

Commit dcbd309

Browse files
flags: optimise memory allocation
Signed-off-by: Artur Melanchyk <[email protected]>
1 parent ce4b4e5 commit dcbd309

File tree

2 files changed

+5
-4
lines changed

2 files changed

+5
-4
lines changed

pkg/flags/selective_string.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ func (ss *SelectiveStringValue) Valids() []string {
5959
// valids[0] will be default value. Caller must be sure
6060
// len(valids) != 0 or it will panic.
6161
func NewSelectiveStringValue(valids ...string) *SelectiveStringValue {
62-
vm := make(map[string]struct{})
62+
vm := make(map[string]struct{}, len(valids))
6363
for _, v := range valids {
6464
vm[v] = struct{}{}
6565
}
@@ -106,7 +106,7 @@ func (ss *SelectiveStringsValue) Valids() []string {
106106
// for which any one of the given strings is a valid value,
107107
// and any other value is an error.
108108
func NewSelectiveStringsValue(valids ...string) *SelectiveStringsValue {
109-
vm := make(map[string]struct{})
109+
vm := make(map[string]struct{}, len(valids))
110110
for _, v := range valids {
111111
vm[v] = struct{}{}
112112
}

pkg/flags/unique_strings.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,9 @@ type UniqueStringsValue struct {
3131
// Implements "flag.Value" interface.
3232
// The values are set in order.
3333
func (us *UniqueStringsValue) Set(s string) error {
34-
us.Values = make(map[string]struct{})
35-
for _, v := range strings.Split(s, ",") {
34+
values := strings.Split(s, ",")
35+
us.Values = make(map[string]struct{}, len(values))
36+
for _, v := range values {
3637
us.Values[v] = struct{}{}
3738
}
3839
return nil

0 commit comments

Comments
 (0)