-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Fix quadratic complexity in Reconciler.Reconcile(). #10989
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what @codingllama said
@@ -90,14 +90,21 @@ type ResourceWithLabels interface { | |||
// ResourcesWithLabels is a list of labeled resources. | |||
type ResourcesWithLabels []ResourceWithLabels |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we still need this type?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, we do: it is rather popular in the codebase. I could work to replace it, but not everywhere ResourcesWithLabelsMap
makes sense.
* Fix quadratic Reconciler.Reconcile() complexity. Co-authored-by: Alan Parra <[email protected]>
* Fix quadratic Reconciler.Reconcile() complexity. Co-authored-by: Alan Parra <[email protected]>
Reconciler.Reconcile()
is "accidentally quadratic".For large number of resources N, the running time of this function blows up:
This complexity arises because
Reconciler.Reconcile()
calls bothprocessNewResource
andprocessRegisteredResource
N times, and each of those callscurrentResources.Find()
which also has N complexity. Combined we arrive at NxN=N^2.After simple fix (switching to map lookup instead of linear list scan):
For increased efficiency and to make it clear reconciler operates on a list of unique resources, I've changed the respective signatures to the new type
types.ResourcesWithLabelsMap
.Additionally, two (unused) functions called
Find()
were removed due to the danger of accidentally repeating the problem in question.collection.ToMap()["foo"]
should be used instead.