Skip to content

Commit

Permalink
Merge pull request #2821 from thaJeztah/loop_da_loop
Browse files Browse the repository at this point in the history
allocator: use a map for network-IDs to prevent O(n2)
  • Loading branch information
dperny committed Feb 12, 2019
2 parents 6894bde + 97400d2 commit bf7b2cb
Showing 1 changed file with 5 additions and 11 deletions.
16 changes: 5 additions & 11 deletions manager/allocator/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -989,8 +989,11 @@ func (a *Allocator) allocateNode(ctx context.Context, node *api.Node, existingAd

nc := a.netCtx

var nwIDs = make(map[string]struct{}, len(networks))

// go through all of the networks we've passed in
for _, network := range networks {
nwIDs[network.ID] = struct{}{}

// for each one, create space for an attachment. then, search through
// all of the attachments already on the node. if the attachment
Expand Down Expand Up @@ -1049,17 +1052,8 @@ func (a *Allocator) allocateNode(ctx context.Context, node *api.Node, existingAd
// https://github.com/golang/go/wiki/SliceTricks#filtering-without-allocating
attachments := node.Attachments[:0]
for _, attach := range node.Attachments {
// for every attachment, go through every network. if the attachment
// belongs to one of the networks, then go to the next attachment. if
// no network matches, then the the attachment should be removed.
attachmentBelongs := false
for _, network := range networks {
if network.ID == attach.Network.ID {
attachmentBelongs = true
break
}
}
if attachmentBelongs {
if _, ok := nwIDs[attach.Network.ID]; ok {
// attachment belongs to one of the networks, so keep it
attachments = append(attachments, attach)
} else {
// free the attachment and remove it from the node's attachments by
Expand Down

0 comments on commit bf7b2cb

Please sign in to comment.