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

Optimize stats/opentelemetry #7525

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

Optimize stats/opentelemetry #7525

wants to merge 2 commits into from

Conversation

ash2k
Copy link
Contributor

@ash2k ash2k commented Aug 17, 2024

First commit moves attribute construction under if to only construct them when they are used.

Second commit uses WithAttributeSet() instead of WithAttributes(). 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.

var sink []string

func abc(vals ...string) {
	sink = vals
}

func BenchmarkFuncCall(b *testing.B) {
	b.ReportAllocs()
	b.ResetTimer()
	arg := []string{"a", "b", "c"}
	for i := 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

See https://go.dev/ref/spec#Passing_arguments_to_..._parameters.

WithAttributes() makes a defensive copy of the passed slice, which is unnecessary overhead in this code.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant