Skip to content

Commit 7b1101b

Browse files
dataclients/kubernetes: preallocate slices (zalando#2786)
Preallocate some obvious cases found by https://github.com/alexkohler/prealloc Signed-off-by: Alexander Yastrebov <[email protected]>
1 parent 7c60b1d commit 7b1101b

File tree

3 files changed

+4
-8
lines changed

3 files changed

+4
-8
lines changed

dataclients/kubernetes/endpoints.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,11 @@ func formatEndpoint(a *address, p *port, protocol string) string {
3131
}
3232

3333
func formatEndpointsForSubsetAddresses(addresses []*address, port *port, protocol string) []string {
34-
var result []string
34+
result := make([]string, 0, len(addresses))
3535
for _, address := range addresses {
3636
result = append(result, formatEndpoint(address, port, protocol))
3737
}
38-
3938
return result
40-
4139
}
4240

4341
func (ep *endpoint) targetsByServicePort(protocol string, servicePort *servicePort) []string {

dataclients/kubernetes/endpointslices.go

+2-4
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,10 @@ func (eps *skipperEndpointSlice) targetsByServicePort(protocol, scheme string, s
5555
port = eps.getPort(protocol, servicePort.Name, servicePort.Port)
5656
}
5757

58-
var result []string
58+
result := make([]string, 0, len(eps.Endpoints))
5959
for _, ep := range eps.Endpoints {
6060
result = append(result, formatEndpointString(ep.Address, scheme, port))
6161
}
62-
6362
return result
6463
}
6564

@@ -68,11 +67,10 @@ func (eps *skipperEndpointSlice) targetsByServiceTarget(protocol, scheme string,
6867
pValue, _ := serviceTarget.Value.(int)
6968
port := eps.getPort(protocol, pName, pValue)
7069

71-
var result []string
70+
result := make([]string, 0, len(eps.Endpoints))
7271
for _, ep := range eps.Endpoints {
7372
result = append(result, formatEndpointString(ep.Address, scheme, port))
7473
}
75-
7674
return result
7775
}
7876

dataclients/kubernetes/kubernetestest/fixtures.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ func safeFileClose(t *testing.T, fd *os.File) {
142142
}
143143

144144
func compileRegexps(s []string) ([]*regexp.Regexp, error) {
145-
var r []*regexp.Regexp
145+
r := make([]*regexp.Regexp, 0, len(s))
146146
for _, si := range s {
147147
ri, err := regexp.Compile(si)
148148
if err != nil {

0 commit comments

Comments
 (0)