Skip to content

Commit 6c22cff

Browse files
authored
Merge pull request kubernetes#75755 from smarterclayton/save_one_allocate
Avoid allocating the watch shim object more than once
2 parents f6c51d6 + 316a87c commit 6c22cff

File tree

1 file changed

+3
-1
lines changed
  • staging/src/k8s.io/apiserver/pkg/endpoints/handlers

1 file changed

+3
-1
lines changed

staging/src/k8s.io/apiserver/pkg/endpoints/handlers/watch.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@ func (s *WatchServer) ServeHTTP(w http.ResponseWriter, req *http.Request) {
216216

217217
var unknown runtime.Unknown
218218
internalEvent := &metav1.InternalEvent{}
219+
outEvent := &metav1.WatchEvent{}
219220
buf := &bytes.Buffer{}
220221
ch := s.Watching.ResultChan()
221222
for {
@@ -242,10 +243,11 @@ func (s *WatchServer) ServeHTTP(w http.ResponseWriter, req *http.Request) {
242243
unknown.Raw = buf.Bytes()
243244
event.Object = &unknown
244245

246+
*outEvent = metav1.WatchEvent{}
247+
245248
// create the external type directly and encode it. Clients will only recognize the serialization we provide.
246249
// The internal event is being reused, not reallocated so its just a few extra assignments to do it this way
247250
// and we get the benefit of using conversion functions which already have to stay in sync
248-
outEvent := &metav1.WatchEvent{}
249251
*internalEvent = metav1.InternalEvent(event)
250252
err := metav1.Convert_v1_InternalEvent_To_v1_WatchEvent(internalEvent, outEvent, nil)
251253
if err != nil {

0 commit comments

Comments
 (0)