Optimize GetControllerOf method

In addition create a similar method that doesn't copy objects.

Benchmark for the new no-copy method vs the old one:
```
benchmark                       old ns/op     new ns/op     delta
BenchmarkGetControllerOf-12     214           14.8          -93.08%

benchmark                       old allocs     new allocs     delta
BenchmarkGetControllerOf-12     1              0              -100.00%

benchmark                       old bytes     new bytes     delta
BenchmarkGetControllerOf-12     80            0             -100.00%
```

Benchamrk for the new (copy) method vs the old one:
```
benchmark                       old ns/op     new ns/op     delta
BenchmarkGetControllerOf-12     128           114           -10.94%

benchmark                       old allocs     new allocs     delta
BenchmarkGetControllerOf-12     1              1              +0.00%

benchmark                       old bytes     new bytes     delta
BenchmarkGetControllerOf-12     80            80            +0.00%

```

Overall there is a 10% improvement for the old vs new (copy) method and
huge improvent (x10) for the old vs new (no-copy).

I changed the IsControlledBy and a few other methods to use the new (no-copy) method.
This commit is contained in:
Matt Matejczyk
2019-09-23 15:48:39 +02:00
parent 57d87502ba
commit 01ccd2e19e
4 changed files with 51 additions and 7 deletions

View File

@@ -224,7 +224,7 @@ func (rh *realHistory) ListControllerRevisions(parent metav1.Object, selector la
}
var owned []*apps.ControllerRevision
for i := range history {
ref := metav1.GetControllerOf(history[i])
ref := metav1.GetControllerOfNoCopy(history[i])
if ref == nil || ref.UID == parent.GetUID() {
owned = append(owned, history[i])
}
@@ -292,7 +292,7 @@ func (rh *realHistory) DeleteControllerRevision(revision *apps.ControllerRevisio
func (rh *realHistory) AdoptControllerRevision(parent metav1.Object, parentKind schema.GroupVersionKind, revision *apps.ControllerRevision) (*apps.ControllerRevision, error) {
// Return an error if the parent does not own the revision
if owner := metav1.GetControllerOf(revision); owner != nil {
if owner := metav1.GetControllerOfNoCopy(revision); owner != nil {
return nil, fmt.Errorf("attempt to adopt revision owned by %v", owner)
}
// Use strategic merge patch to add an owner reference indicating a controller ref