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

sdk/trace: use slices.Grow() to avoid excessive runtime.growslice() #4818

Merged
merged 18 commits into from
Jan 12, 2024
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
- Improve `go.opentelemetry.io/otel/baggage` performance. (#4743)
- Improve performance of the `(*Set).Filter` method in `go.opentelemetry.io/otel/attribute` when the passed filter does not filter out any attributes from the set. (#4774)
- `Member.String` in `go.opentelemetry.io/otel/baggage` percent-encodes only when necessary. (#4775)
- Improve `go.opentelemetry.io/otel/trace.Span`'s performance when adding multiple attributes. (#4818)

### Fixed

Expand Down
13 changes: 13 additions & 0 deletions sdk/trace/span.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,15 @@ func (s *recordingSpan) SetStatus(code codes.Code, description string) {
s.status = status
}

// ensureAttributesCapacity inlines functionality from golang.org/x/exp/slices.Grow
lizthegrey marked this conversation as resolved.
Show resolved Hide resolved
// so that we can avoid needing to import golang.org/x/exp for go1.20.
// Once support for go1.20 is dropped, we can use slices.Grow in 1.21+ instead.
lizthegrey marked this conversation as resolved.
Show resolved Hide resolved
func (s *recordingSpan) ensureAttributesCapacity(minCapacity int) {
if n := minCapacity - cap(s.attributes); n > 0 {
s.attributes = append(s.attributes[:cap(s.attributes)], make([]attribute.KeyValue, n)...)[:len(s.attributes)]
}
}

// SetAttributes sets attributes of this span.
//
// If a key from attributes already exists the value associated with that key
Expand Down Expand Up @@ -242,6 +251,7 @@ func (s *recordingSpan) SetAttributes(attributes ...attribute.KeyValue) {

// Otherwise, add without deduplication. When attributes are read they
// will be deduplicated, optimizing the operation.
s.ensureAttributesCapacity(len(s.attributes) + len(attributes))
for _, a := range attributes {
if !a.Valid() {
// Drop all invalid attributes.
Expand Down Expand Up @@ -277,6 +287,9 @@ func (s *recordingSpan) addOverCapAttrs(limit int, attrs []attribute.KeyValue) {

// Now that s.attributes is deduplicated, adding unique attributes up to
// the capacity of s will not over allocate s.attributes.
if limit-len(s.attributes) > 0 {
lizthegrey marked this conversation as resolved.
Show resolved Hide resolved
s.ensureAttributesCapacity(limit)
}
for _, a := range attrs {
if !a.Valid() {
// Drop all invalid attributes.
Expand Down
18 changes: 18 additions & 0 deletions sdk/trace/span_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package trace

import (
"bytes"
"context"
"fmt"
"testing"

Expand Down Expand Up @@ -243,3 +244,20 @@ func TestTruncateAttr(t *testing.T) {
})
}
}

func BenchmarkRecordingSpanSetAttributes(b *testing.B) {
lizthegrey marked this conversation as resolved.
Show resolved Hide resolved
tp := NewTracerProvider(WithSampler(AlwaysSample()))
tracer := tp.Tracer("tracer")
var attrs []attribute.KeyValue
for i := 0; i < 100; i++ {
attr := attribute.String(fmt.Sprintf("hello.attrib%d", i), fmt.Sprintf("goodbye.attrib%d", i))
attrs = append(attrs, attr)
}

ctx := context.Background()
lizthegrey marked this conversation as resolved.
Show resolved Hide resolved
for n := 0; n < b.N; n++ {
_, span := tracer.Start(ctx, "span")
span.SetAttributes(attrs...)
span.End()
}
}