-
Notifications
You must be signed in to change notification settings - Fork 772
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
Metrics: Avoid defensive copies of MetricPoint struct #3065
Metrics: Avoid defensive copies of MetricPoint struct #3065
Conversation
Codecov Report
@@ Coverage Diff @@
## main #3065 +/- ##
==========================================
+ Coverage 84.70% 84.71% +0.01%
==========================================
Files 259 259
Lines 9119 9121 +2
==========================================
+ Hits 7724 7727 +3
+ Misses 1395 1394 -1
|
Why are the allocation numbers changing randomly? Is this change supposed to affect allocation as well? If yes, why does it go up for WriteMetric with 1000 calls but go down for WriteMetric with 10000 calls? |
@utpilla That is a good question I do not have an answer for. Should be no change at all to the allocations for this work. Even more then that, there should no allocations at all ever shown with this benchmark! Prometheus reuses a buffer so that it doesn't need allocations. Seems to be noise coming from somewhere. Something I would like to get to the bottom of, but prefer to not block this PR for that. |
Description
We have this pattern in our metric exporters where we do...
MetricPoint
is a mutable struct:opentelemetry-dotnet/src/OpenTelemetry/Metrics/MetricPoint.cs
Line 27 in a1e7167
When the compiler sees the
GetSumLong
invocation (for example) it doesn't know if that call is going to mutate the state of the localmetricPoint
so it will create a defensive copy. This can be avoided by declaring members which do not modify the state of the struct asreadonly
.Here's a doc which goes over this in more detail: https://docs.microsoft.com/en-us/dotnet/csharp/write-safe-efficient-code#declare-readonly-members-for-mutable-structs
Benchmarks
(I used
PrometheusSerializerBenchmarks
but this will be universal whereverMetricPoint
is used as aref readonly
.)Before
After
TODOs
CHANGELOG.md
updated for non-trivial changes