Skip to content

client-go: add a benchmark for the watch event decode path#140930

Open
ashishpatel26 wants to merge 1 commit into
kubernetes:masterfrom
ashishpatel26:fix/issue-129705-streamwatcher-buffer-reuse
Open

client-go: add a benchmark for the watch event decode path#140930
ashishpatel26 wants to merge 1 commit into
kubernetes:masterfrom
ashishpatel26:fix/issue-129705-streamwatcher-buffer-reuse

Conversation

@ashishpatel26

Copy link
Copy Markdown

What this PR does / why we need it

#129705 reports that the client-side watch decode path dominates kube-apiserver memory under high pod churn, backed by heap profiles from a large cluster. There was no in-tree benchmark covering that path, so the cost couldn't be measured locally or protected against regression.

I added one. It mirrors the exact stack rest.Request.newStreamWatcher builds for a protobuf watch - length-delimited framer -> streaming.Decoder -> restclientwatch.Decoder - decoding a realistically-shaped Pod event.

Current cost on my machine:

BenchmarkDecoderProtobuf-16    4241 B/op    33 allocs/op

This PR only adds the benchmark. It does not change decode behavior.

Note on the fix direction discussed in the issue

While writing this I looked into the "single buffer per watcher for deserializing data" idea from the issue thread, and I don't think it works at this layer. Recording what I found so nobody repeats it:

  1. The frame-read buffer is already reused per watcher - streaming.decoder keeps a single d.buf and grows it in place (streaming.go). So that part is done.

  2. Reusing the per-event metav1.WatchEvent in Decoder.Decode() doesn't help either, because both protobuf decode paths call unmarshaler.Reset() before unmarshalling, and WatchEvent.Reset() is *m = WatchEvent{} - which drops the Object.Raw backing array every time, so its capacity can never be recycled.

I measured that rather than assuming it: reusing the struct saves exactly 1 allocation out of 33 (4241 -> 4176 B/op, ~1.5%), with no ns/op improvement. That's not worth adding hidden state to a path every informer in every client depends on, so I dropped it and kept only the benchmark.

The remaining 32 allocations are inside the embedded object unmarshal itself - materializing the Pod's maps, slices and strings. Avoiding those would mean aliasing the frame buffer into decoded objects, which isn't safe here: d.buf is overwritten on the next frame read, so anything retained by an informer cache would be corrupted. A real fix looks like it needs a change to the serializer's Reset contract, which is a cross-cutting design decision I'd want a sig-api-machinery opinion on before attempting.

Which issue(s) this PR is related to

Relates to #129705 (adds measurement; does not fix)

Type of change

/kind cleanup
/sig api-machinery
/sig scalability

Does this PR introduce a user-facing change?

NONE

Testing

  • go vet ./rest/watch/ - clean
  • go test ./rest/watch/ - all existing tests pass
  • go test ./rest/watch/ -run XXX -bench BenchmarkDecoderProtobuf - runs and reports stable allocation numbers across repeated runs

Test-only addition, so there's no production code path to regress.

Issue kubernetes#129705 reports that StreamWatcher's decode path dominates
kube-apiserver memory under high pod churn, backed by heap profiles from
a large cluster. There was no in-tree benchmark for that path, so the
cost could not be measured or regression-tested locally.

I added one that mirrors the stack rest.Request.newStreamWatcher builds
for a protobuf watch: length-delimited framer -> streaming decoder ->
restclientwatch.Decoder, decoding a realistically-shaped Pod event.

On my machine the current cost is 4241 B/op and 33 allocs/op per event.

This only adds the benchmark; it does not change decode behavior. My
findings on why the buffer-reuse direction suggested on the issue does
not help are in a comment there.
@kubernetes-prow kubernetes-prow Bot added release-note-none Denotes a PR that doesn't merit a release note. kind/cleanup Categorizes issue or PR as related to cleaning up code, process, or technical debt. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Jul 25, 2026
@kubernetes-prow

Copy link
Copy Markdown
Contributor

Please note that we're already in Code Freeze for the upcoming v1.37.0 release.

Adding the milestone to this PR is strictly prohibited without proper approval. If this PR needs to be included in the v1.37.0 release:

  1. Technical review: get the PR reviewed and approved as usual (/lgtm and /approve)
  2. Inclusion in release: ping @sig-release-leads on the #sig-release Slack channel and suggest to add the v1.37.0 milestone to the PR

@kubernetes-prow kubernetes-prow Bot added sig/api-machinery Categorizes an issue or PR as relevant to SIG API Machinery. sig/scalability Categorizes an issue or PR as relevant to SIG Scalability. labels Jul 25, 2026
@kubernetes-prow

Copy link
Copy Markdown
Contributor

This issue is currently awaiting triage.

If a SIG or subproject determines this is a relevant issue, they will accept it by applying the triage/accepted label and provide further guidance.

The triage/accepted label can be added by org members by writing /triage accepted in a comment.

Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

@kubernetes-prow kubernetes-prow Bot added needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. needs-priority Indicates a PR lacks a `priority/foo` label and requires one. labels Jul 25, 2026
@kubernetes-prow

Copy link
Copy Markdown
Contributor

Hi @ashishpatel26. Thanks for your PR.

I'm waiting for a kubernetes member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work.

Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

@kubernetes-prow kubernetes-prow Bot added the needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. label Jul 25, 2026
@kubernetes-prow

Copy link
Copy Markdown
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: ashishpatel26
Once this PR has been reviewed and has the lgtm label, please assign smarterclayton for approval. For more information see the Code Review Process.

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@xigang

xigang commented Jul 25, 2026

Copy link
Copy Markdown
Member

/cc @liggitt @jpbetz

@kubernetes-prow
kubernetes-prow Bot requested review from jpbetz and liggitt July 25, 2026 13:15
@xigang

xigang commented Jul 25, 2026

Copy link
Copy Markdown
Member

/ok-to-test

@kubernetes-prow kubernetes-prow Bot added ok-to-test Indicates a non-member PR verified by an org member that is safe to test. and removed needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Jul 25, 2026
@ashishpatel26

Copy link
Copy Markdown
Author

/retest

@kubernetes-prow

Copy link
Copy Markdown
Contributor

@ashishpatel26: The following test failed, say /retest to rerun all failed tests or /retest-required to rerun all mandatory failed tests:

Test name Commit Details Required Rerun command
pull-kubernetes-verify 08544bb link true /test pull-kubernetes-verify

Full PR test history. Your PR dashboard. Please help us cut down on flakes by linking to an open issue when you hit one in your PR.

Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository. I understand the commands that are listed here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. kind/cleanup Categorizes issue or PR as related to cleaning up code, process, or technical debt. needs-priority Indicates a PR lacks a `priority/foo` label and requires one. needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. ok-to-test Indicates a non-member PR verified by an org member that is safe to test. release-note-none Denotes a PR that doesn't merit a release note. sig/api-machinery Categorizes an issue or PR as relevant to SIG API Machinery. sig/scalability Categorizes an issue or PR as relevant to SIG Scalability. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants