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 ourterms of serviceand privacy statement.We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stats/opentelemetry: Optimize slice allocations #7525

Merged
merged 2 commits into from
Sep 3, 2024

Conversation

ash2k
Copy link
Contributor

@ash2k ash2k commented Aug 17, 2024

First commit moves attribute construction underifto only construct them when they are used.

Second commit usesWithAttributeSet()instead ofWithAttributes().This avoids copying the slice, which is not necessary for this code. Seehttps://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 callingRecord()each time.

varsink[]string

funcabc(vals...string) {
sink=vals
}

funcBenchmarkFuncCall(b*testing.B) {
b.ReportAllocs()
b.ResetTimer()
arg:=[]string{"a","b","c"}
fori:=0;i<b.N;i++{
abc(arg...)
}
}

The above benchmark gives me these results:

// Pass "a", "b", "c"
BenchmarkFuncCall-10 43171615 27.14 ns/op 48 B/op 1 allocs/op

// Pass arg...
BenchmarkFuncCall-10 1000000000 0.9530 ns/op 0 B/op 0 allocs/op

Seehttps://go.dev/ref/spec#Passing_arguments_to_..._parameters.

RELEASE NOTES: None

@aranjans aranjans added this to the1.67 Releasemilestone Aug 19, 2024
@arjan-bal arjan-bal self-assigned this Aug 20, 2024
@arjan-bal arjan-bal added the Type: Performance Performance improvements (CPU, network, memory, etc) label Aug 20, 2024
@arjan-bal arjan-bal changed the title Optimize stats/opentelemetry stats/opentelemetry: Optimize slice allocations Aug 20, 2024
Copy link
Contributor

@arjan-bal arjan-bal left a 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.

Copy link

codecov bot commented Aug 20, 2024

CodecovReport

All modified and coverable lines are covered by tests ✅

Project coverage is 81.85%. Comparing base(005b092)to head(52792d2).
Report is 8 commits behind head on master.

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

see 12 files with indirect coverage changes

Copy link
Member

@dfawley dfawley left a 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 renamedWithAttributeSettoWithAttributesand made some other function behave the wayWithAttributesbehaves. That is unexpected. I might even go so far as to markWithAttributesdeprecated if I was them, to make it more clear that it's probably not what you ever want to use.

stats/opentelemetry/client_metrics.go Outdated Show resolved Hide resolved
@dfawley dfawley assignedash2kand unassigneddfawley Aug 28, 2024
WithAttributes() makes a defensive copy of the passed slice, which is unnecessary overhead in this code.
@dfawley dfawley merged commit6147c81 into grpc:master Sep 3, 2024
14 checks passed
@ash2k ash2k deleted the optimize-otel branch September 3, 2024 22:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Performance Performance improvements (CPU, network, memory, etc)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants