-
Notifications
You must be signed in to change notification settings - Fork 4.4k
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
stats/opentelemetry: Optimize slice allocations #7525
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, thanks for the contribution!
Adding another reviewer for a second approval.
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## master #7525 +/- ##
==========================================
- Coverage 81.91% 81.85% -0.07%
==========================================
Files 361 361
Lines 27825 27825
==========================================
- Hits 22794 22776 -18
- Misses 3841 3853 +12
- Partials 1190 1196 +6 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wow, I wish otel had renamed WithAttributeSet
to WithAttributes
and made some other function behave the way WithAttributes
behaves. That is unexpected. I might even go so far as to mark WithAttributes
deprecated if I was them, to make it more clear that it's probably not what you ever want to use.
WithAttributes() makes a defensive copy of the passed slice, which is unnecessary overhead in this code.
First commit moves attribute construction under
if
to only construct them when they are used.Second commit uses
WithAttributeSet()
instead ofWithAttributes()
. This avoids copying the slice, which is not necessary for this code. See https://github.com/open-telemetry/opentelemetry-go/blob/metric/v1.28.0/metric/instrument.go#L349-L368.Also, a bit of allocations can be removed by constructing the vararg slice one time vs when calling
Record()
each time.The above benchmark gives me these results:
See https://go.dev/ref/spec#Passing_arguments_to_..._parameters.
RELEASE NOTES: None