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

allocator: use a map for network-IDs to prevent O(n2) #2821

Merged
merged 1 commit into from
Feb 12, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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