Skip to content

Commit

Permalink
Remove Sortable
Browse files Browse the repository at this point in the history
  • Loading branch information
ash2k committed Dec 11, 2023
1 parent 0405492 commit 5840497
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 21 deletions.
27 changes: 6 additions & 21 deletions attribute/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"encoding/json"
"reflect"
"sort"
"sync"
)

type (
Expand Down Expand Up @@ -56,12 +55,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 @@ -191,22 +184,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 for empty set.
if len(kvs) == 0 {
return empty()
}
s, _ := NewSetWithSortableFiltered(kvs, tmp, nil)
s, _ := NewSetWithSortableFiltered(kvs, nil, nil)
return s
}

Expand All @@ -220,9 +211,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 @@ -249,19 +238,15 @@ 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
sliceSortStable(kvs)

position := len(kvs) - 1
offset := position - 1
Expand Down
15 changes: 15 additions & 0 deletions attribute/set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,3 +225,18 @@ func args(m reflect.Method) []reflect.Value {
}
return out
}

var (
sinkSet attribute.Set
sinkAttrs []attribute.KeyValue
)

func BenchmarkNewSetWithSortableFiltered(b *testing.B) {
attrs := []attribute.KeyValue{attribute.Int("C", 3), attribute.Int("A", 1), attribute.Int("B", 2)}
srt := new(attribute.Sortable)
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
sinkSet, sinkAttrs = attribute.NewSetWithSortableFiltered(attrs, srt, nil)
}
}
11 changes: 11 additions & 0 deletions attribute/sort.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//go:build !go1.21

package attribute

import "sort"

func sliceSortStable(kvs []KeyValue) {
sort.SliceStable(kvs, func(i, j int) bool {
return kvs[i].Key < kvs[j].Key
})
}
17 changes: 17 additions & 0 deletions attribute/sort_1.21.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//go:build go1.21

package attribute

import "slices"

func sliceSortStable(kvs []KeyValue) {
slices.SortStableFunc(kvs, func(a, b KeyValue) int {
if a.Key == b.Key {
return 0
}
if a.Key < b.Key {
return -1
}
return 1
})
}

0 comments on commit 5840497

Please sign in to comment.