Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

all: preallocate slices with known size #10716

Merged
merged 1 commit into from Mar 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkg/azure/api/api.go
Expand Up @@ -278,7 +278,7 @@ func (c *Client) GetVpcsAndSubnets(ctx context.Context) (ipamTypes.VirtualNetwor
// the interfaceID. The provided IPs must belong to the subnet as specified by
// the subnet ID.
func (c *Client) AssignPrivateIpAddresses(ctx context.Context, subnetID, interfaceID string, ips []net.IP) error {
var ipConfigurations []network.InterfaceIPConfiguration
ipConfigurations := make([]network.InterfaceIPConfiguration, 0, len(ips))

for _, ip := range ips {
ipString := ip.String()
Expand Down
2 changes: 1 addition & 1 deletion pkg/checker/checker.go
Expand Up @@ -105,7 +105,7 @@ func DeepAllowUnexported(vs ...interface{}) cmp.Option {
for _, v := range vs {
structTypes(reflect.ValueOf(v), m)
}
var typs []interface{}
typs := make([]interface{}, 0, len(m))
for t := range m {
typs = append(typs, reflect.New(t).Elem().Interface())
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/endpoint/restore_test.go
Expand Up @@ -145,7 +145,7 @@ func (ds *EndpointSuite) TestReadEPsFromDirNames(c *C) {
c.Assert(len(eps), Equals, len(epsWanted))

sort.Slice(epsWanted, func(i, j int) bool { return epsWanted[i].ID < epsWanted[j].ID })
var restoredEPs []*Endpoint
restoredEPs := make([]*Endpoint, 0, len(eps))
for _, ep := range eps {
restoredEPs = append(restoredEPs, ep)
}
Expand Down
7 changes: 2 additions & 5 deletions pkg/fqdn/name_manager.go
Expand Up @@ -64,15 +64,12 @@ func (n *NameManager) GetModel() *models.NameManager {
n.Mutex.Lock()
defer n.Mutex.Unlock()

var (
namesToPoll []string
allSelectors []*models.SelectorEntry
)

namesToPoll := make([]string, 0, len(n.namesToPoll))
for name := range n.namesToPoll {
namesToPoll = append(namesToPoll, name)
}

allSelectors := make([]*models.SelectorEntry, 0, len(n.allSelectors))
for fqdnSel, regex := range n.allSelectors {
pair := &models.SelectorEntry{
SelectorString: fqdnSel.String(),
Expand Down
2 changes: 1 addition & 1 deletion pkg/ip/ip.go
Expand Up @@ -194,7 +194,6 @@ func getNetworkPrefix(ipNet *net.IPNet) *net.IP {
}

func removeCIDR(allowCIDR, removeCIDR *net.IPNet) ([]*net.IPNet, error) {
var allows []*net.IPNet
var allowIsIpv4, removeIsIpv4 bool
var allowBitLen int

Expand Down Expand Up @@ -240,6 +239,7 @@ func removeCIDR(allowCIDR, removeCIDR *net.IPNet) ([]*net.IPNet, error) {
// Create CIDR prefixes with mask size of Y+1, Y+2 ... X where Y is the mask
// length of the CIDR prefix B from which we are excluding a CIDR prefix A
// with mask length X.
allows := make([]*net.IPNet, 0, removeSize-allowSize)
for i := (allowBitLen - allowSize - 1); i >= (allowBitLen - removeSize); i-- {
// The mask for each CIDR prefix is simply the ith bit flipped, and then
// zero'ing out all subsequent bits (the host identifier part of the
Expand Down
2 changes: 1 addition & 1 deletion pkg/ipam/allocator/pool_allocator.go
Expand Up @@ -54,7 +54,7 @@ func (s *PoolAllocator) Allocate(ip net.IP) error {
// AllocateMany allocates multiple IP addresses. The operation succeeds if all
// IPs can be allocated. On failure, all IPs are released again.
func (s *PoolAllocator) AllocateMany(num int) ([]net.IP, error) {
var ips []net.IP
ips := make([]net.IP, 0, num)

for i := 0; i < num; i++ {
ip, err := s.allocator.AllocateNext()
Expand Down
4 changes: 2 additions & 2 deletions pkg/ipcache/cidr.go
Expand Up @@ -54,10 +54,10 @@ func AllocateCIDRsForIPs(prefixes []net.IP) ([]*identity.Identity, error) {

func allocateCIDRs(prefixes []*net.IPNet) ([]*identity.Identity, error) {
// maintain list of used identities to undo on error
var usedIdentities []*identity.Identity
usedIdentities := make([]*identity.Identity, 0, len(prefixes))

// maintain list of newly allocated identities to update ipcache
allocatedIdentities := map[string]*identity.Identity{}
allocatedIdentities := make(map[string]*identity.Identity, len(prefixes))
newlyAllocatedIdentities := map[string]*identity.Identity{}

for _, prefix := range prefixes {
Expand Down
2 changes: 1 addition & 1 deletion pkg/k8s/watchers/watcher.go
Expand Up @@ -617,7 +617,7 @@ func genCartesianProduct(
bes *k8s.Endpoints,
) []loadbalancer.SVC {

var svcs []loadbalancer.SVC
svcs := make([]loadbalancer.SVC, 0, len(ports))

for fePortName, fePort := range ports {
var besValues []loadbalancer.Backend
Expand Down
2 changes: 1 addition & 1 deletion pkg/labels/labels.go
Expand Up @@ -442,7 +442,7 @@ func (l Label) FormatForKVStore() string {
// DO NOT BREAK THE FORMAT OF THIS. THE RETURNED STRING IS USED AS KEY IN
// THE KEY-VALUE STORE.
func (l Labels) SortedList() []byte {
var keys []string
keys := make([]string, 0, len(l))
for k := range l {
keys = append(keys, k)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/policy/visibility_test.go
Expand Up @@ -34,7 +34,7 @@ func (ds *PolicyTestSuite) TestGenerateL7RulesByParser(c *C) {
c.Assert(m, Not(IsNil))
c.Assert(len(m), Equals, 1)

var l7Rules []*PerSelectorPolicy
l7Rules := make([]*PerSelectorPolicy, 0, len(m))
for _, v := range m {
l7Rules = append(l7Rules, v)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/service/service_test.go
Expand Up @@ -237,7 +237,7 @@ func (m *ManagerTestSuite) TestHealthCheckNodePort(c *C) {

func (m *ManagerTestSuite) TestGetServiceNameByAddr(c *C) {
fe := frontend1.DeepCopy()
var be []lb.Backend
be := make([]lb.Backend, 0, len(backends1))
for _, backend := range backends1 {
be = append(be, *backend.DeepCopy())
}
Expand Down