Skip to content

Commit

Permalink
Use filteredToFront to unique
Browse files Browse the repository at this point in the history
  • Loading branch information
MrAlias committed Jan 2, 2024
1 parent e24eb19 commit 06cd155
Showing 1 changed file with 17 additions and 21 deletions.
38 changes: 17 additions & 21 deletions attribute/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,8 +250,8 @@ 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) {
// Check for empty set.
if len(kvs) == 0 {
n := len(kvs)
if n == 0 {
return empty(), nil
}

Expand All @@ -263,28 +263,24 @@ func NewSetWithSortableFiltered(kvs []KeyValue, tmp *Sortable, filter Filter) (S

*tmp = nil

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

// The requirements stated above require that the stable
// result be placed in the end of the input slice, while
// overwritten values are swapped to the beginning.
//
// De-duplicate with last-value-wins semantics. Preserve
// duplicate values at the beginning of the input slice.
for ; offset >= 0; offset-- {
if kvs[offset].Key == kvs[position].Key {
continue
// Use filteredToFront here for last-value-wins semantics.
prev := kvs[n-1].Key
uniq := filteredToFront(kvs[:n-1], func(kv KeyValue) bool {
if kv.Key == prev {
return false
}
position--
kvs[offset], kvs[position] = kvs[position], kvs[offset]
}
prev = kv.Key
return true
})
kvs = kvs[uniq:]

if filter != nil {
return filterSet(kvs[position:], filter)
// TODO (MrAlias): there is a potential optimization here where the
// filtering and uniquing are all done in the same step. For example:
// https://github.com/open-telemetry/opentelemetry-go/blob/228cc878a0f5de19290e8b2035dc4d0019f2a249/attribute/set.go#L266-L300
return filterSet(kvs, filter)
}
return Set{
equivalent: computeDistinct(kvs[position:]),
}, nil
return Set{equivalent: computeDistinct(kvs)}, nil
}

// filterSet reorders kvs so that included keys are contiguous at the end of
Expand Down

0 comments on commit 06cd155

Please sign in to comment.