Skip to content

Commit

Permalink
Remove Sortable
Browse files Browse the repository at this point in the history
  • Loading branch information
ash2k committed Mar 6, 2024
1 parent da2949b commit bf7dd0d
Showing 1 changed file with 15 additions and 21 deletions.
36 changes: 15 additions & 21 deletions attribute/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ package attribute // import "go.opentelemetry.io/otel/attribute"
import (
"encoding/json"
"reflect"
"slices"
"sort"
"sync"
)

type (
Expand Down Expand Up @@ -45,12 +45,6 @@ var (
iface: [0]KeyValue{},
},
}

// sortables is a pool of Sortables used to create Sets with a user does
// not provide one.
sortables = sync.Pool{
New: func() interface{} { return new(Sortable) },
}
)

// EmptySet returns a reference to a Set with no elements.
Expand Down Expand Up @@ -180,22 +174,20 @@ func NewSet(kvs ...KeyValue) Set {
if len(kvs) == 0 {
return empty()
}
srt := sortables.Get().(*Sortable)
s, _ := NewSetWithSortableFiltered(kvs, srt, nil)
sortables.Put(srt)
s, _ := NewSetWithSortableFiltered(kvs, nil, nil)
return s
}

// NewSetWithSortable returns a new Set. See the documentation for
// NewSetWithSortableFiltered for more details.
//
// This call includes a Sortable option as a memory optimization.
func NewSetWithSortable(kvs []KeyValue, tmp *Sortable) Set {
func NewSetWithSortable(kvs []KeyValue, _ *Sortable) Set {

Check warning on line 185 in attribute/set.go

View check run for this annotation

Codecov / codecov/patch

attribute/set.go#L185

Added line #L185 was not covered by tests
// Check for empty set.
if len(kvs) == 0 {
return empty()
}
s, _ := NewSetWithSortableFiltered(kvs, tmp, nil)
s, _ := NewSetWithSortableFiltered(kvs, nil, nil)

Check warning on line 190 in attribute/set.go

View check run for this annotation

Codecov / codecov/patch

attribute/set.go#L190

Added line #L190 was not covered by tests
return s
}

Expand All @@ -209,9 +201,7 @@ func NewSetWithFiltered(kvs []KeyValue, filter Filter) (Set, []KeyValue) {
if len(kvs) == 0 {
return empty(), nil
}
srt := sortables.Get().(*Sortable)
s, filtered := NewSetWithSortableFiltered(kvs, srt, filter)
sortables.Put(srt)
s, filtered := NewSetWithSortableFiltered(kvs, nil, filter)
return s, filtered
}

Expand All @@ -238,19 +228,23 @@ func NewSetWithFiltered(kvs []KeyValue, filter Filter) (Set, []KeyValue) {
//
// The second []KeyValue return value is a list of attributes that were
// excluded by the Filter (if non-nil).
func NewSetWithSortableFiltered(kvs []KeyValue, tmp *Sortable, filter Filter) (Set, []KeyValue) {
func NewSetWithSortableFiltered(kvs []KeyValue, _ *Sortable, filter Filter) (Set, []KeyValue) {
// Check for empty set.
if len(kvs) == 0 {
return empty(), nil
}

*tmp = kvs

// Stable sort so the following de-duplication can implement
// last-value-wins semantics.
sort.Stable(tmp)

*tmp = nil
slices.SortStableFunc(kvs, func(a, b KeyValue) int {
if a.Key == b.Key {
return 0
}
if a.Key < b.Key {
return -1
}
return 1
})

position := len(kvs) - 1
offset := position - 1
Expand Down

0 comments on commit bf7dd0d

Please sign in to comment.