Skip to content
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 excessive allocation in the resource metrics #1964

Merged
merged 6 commits into from
Dec 15, 2020

Conversation

markusthoemmes
Copy link
Contributor

@markusthoemmes markusthoemmes commented Dec 15, 2020

Changes

Through a bit of cleanup work in Knative Serving, it got kinda apparent that our metric recording calls are kinda expensive. I triaged it down to the resourceToKey being quite expensive.

Discussing with @evankanderson, we came up with this quick fix to make the allocations of the functions much more reasonable without coming up with a different way of handling the keys here altogether.

Benchmark results

benchmark                               old ns/op     new ns/op     delta
BenchmarkResourceToKey/0-labels-16      139           2.38          -98.29%
BenchmarkResourceToKey/1-labels-16      677           168           -75.18%
BenchmarkResourceToKey/5-labels-16      2344          802           -65.78%
BenchmarkResourceToKey/10-labels-16     4625          1645          -64.43%

benchmark                               old allocs     new allocs     delta
BenchmarkResourceToKey/0-labels-16      2              0              -100.00%
BenchmarkResourceToKey/1-labels-16      6              1              -83.33%
BenchmarkResourceToKey/5-labels-16      18             3              -83.33%
BenchmarkResourceToKey/10-labels-16     33             3              -90.91%

benchmark                               old bytes     new bytes     delta
BenchmarkResourceToKey/0-labels-16      40            0             -100.00%
BenchmarkResourceToKey/1-labels-16      128           32            -75.00%
BenchmarkResourceToKey/5-labels-16      432           192           -55.56%
BenchmarkResourceToKey/10-labels-16     800           320           -60.00%

/assign @evankanderson

@google-cla google-cla bot added the cla: yes Indicates the PR's author has signed the CLA. label Dec 15, 2020
@knative-prow-robot knative-prow-robot added the size/S Denotes a PR that changes 10-29 lines, ignoring generated files. label Dec 15, 2020
@knative-prow-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: markusthoemmes

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

The pull request process is described here

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

@knative-prow-robot knative-prow-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Dec 15, 2020
@codecov
Copy link

codecov bot commented Dec 15, 2020

Codecov Report

Merging #1964 (8517a77) into master (af53027) will increase coverage by 0.01%.
The diff coverage is 88.23%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1964      +/-   ##
==========================================
+ Coverage   68.92%   68.94%   +0.01%     
==========================================
  Files         209      209              
  Lines        8778     8790      +12     
==========================================
+ Hits         6050     6060      +10     
- Misses       2453     2454       +1     
- Partials      275      276       +1     
Impacted Files Coverage Δ
metrics/resource_view.go 86.54% <88.23%> (-0.25%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update af53027...b8c14da. Read the comment docs.

@knative-prow-robot knative-prow-robot added size/M Denotes a PR that changes 30-99 lines, ignoring generated files. and removed size/S Denotes a PR that changes 10-29 lines, ignoring generated files. labels Dec 15, 2020
Comment on lines 324 to 332
var s strings.Builder
writeKV := func(key, value string) {
// We use byte values 1 and 2 to avoid colliding with valid resource labels
// and to make unpacking easy
s.WriteByte('\x01')
s.WriteString(key)
s.WriteByte('\x02')
s.WriteString(value)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure if at this point func writeKV(Builder, string, string) isn't better, or definitely more readable.
Shouldn't it save an allocation, since otherwise it'll dynamically create the lambda to bind it to s?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The lambda doesn't escape apparently. Moving it out works too though.

metrics/resource_view.go Outdated Show resolved Hide resolved
Copy link
Contributor

@vagababov vagababov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/lgtm

metrics/resource_view.go Outdated Show resolved Hide resolved
@knative-prow-robot knative-prow-robot added the lgtm Indicates that a PR is ready to be merged. label Dec 15, 2020
@knative-prow-robot knative-prow-robot removed the lgtm Indicates that a PR is ready to be merged. label Dec 15, 2020
Copy link
Contributor

@vagababov vagababov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/lgtm

@knative-prow-robot knative-prow-robot added the lgtm Indicates that a PR is ready to be merged. label Dec 15, 2020
@knative-prow-robot knative-prow-robot merged commit ba2137c into knative:master Dec 15, 2020
@evankanderson
Copy link
Member

/lgtm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. cla: yes Indicates the PR's author has signed the CLA. lgtm Indicates that a PR is ready to be merged. size/M Denotes a PR that changes 30-99 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants