Use generics for workqueue metrics

The workqueue implementation was recently updated to be strongly typed,
using Go generics. However the metrics implementation was not updated,
and continued using interface{}. This translated to unnecessary memory
allocations when invoking the queueMetrics interface methods to track
queue operation. We can avoid these extra heap allocations by using
generics for the metrics implementation as well.

Signed-off-by: Antonin Bas <antonin.bas@broadcom.com>

Kubernetes-commit: 1aec7568e111f5855121e3afacacf431e5f95948
This commit is contained in:
Antonin Bas
2024-09-25 14:35:03 -07:00
committed by Kubernetes Publisher
parent ca4a13f6de
commit 5b31113588
4 changed files with 55 additions and 52 deletions

View File

@@ -17,6 +17,7 @@ limitations under the License.
package workqueue_test
import (
"fmt"
"runtime"
"sync"
"sync/atomic"
@@ -460,3 +461,20 @@ func mustGarbageCollect(t *testing.T, i interface{}) {
}
})
}
func BenchmarkQueue(b *testing.B) {
keys := make([]string, 100)
for idx := range keys {
keys[idx] = fmt.Sprintf("key-%d", idx)
}
for i := 0; i < b.N; i++ {
b.StopTimer()
q := workqueue.NewTypedWithConfig(workqueue.TypedQueueConfig[string]{})
b.StartTimer()
for j := 0; j < 100; j++ {
q.Add(keys[j])
key, _ := q.Get()
q.Done(key)
}
}
}