Skip to content

Commit

Permalink
Fix mutable stringset memory usage (thanos-io#6669)
Browse files Browse the repository at this point in the history
This commit fixes the Insert function for the mutable stringset
to only insert unique labels instead of adding every label to the set.

Signed-off-by: Filip Petkovski <[email protected]>
  • Loading branch information
fpetkovski authored and saswatamcode committed Aug 28, 2023
1 parent 450c4ad commit 7fbc950
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
1 change: 1 addition & 0 deletions pkg/store/bucket_e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -799,6 +799,7 @@ func TestBucketStore_LabelNamesSet_e2e(t *testing.T) {
for _, n := range []string{"a", "b", "c"} {
testutil.Assert(t, filter.Has(n), "expected filter to have %s", n)
}
testutil.Equals(t, 3, filter.Count())
})
}

Expand Down
18 changes: 17 additions & 1 deletion pkg/stringset/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ import (
type Set interface {
Has(string) bool
HasAny([]string) bool
// Count returns the number of elements in the set.
// A value of -1 indicates infinite size and can be returned by a
// set representing all possible string values.
Count() int
}

type fixedSet struct {
Expand Down Expand Up @@ -38,6 +42,10 @@ func (f fixedSet) Has(s string) bool {
return f.cuckoo.Lookup([]byte(s))
}

func (f fixedSet) Count() int {
return int(f.cuckoo.Count())
}

type mutableSet struct {
cuckoo *cuckoo.ScalableCuckooFilter
}
Expand All @@ -54,7 +62,7 @@ func New() MutableSet {
}

func (e mutableSet) Insert(s string) {
e.cuckoo.Insert([]byte(s))
e.cuckoo.InsertUnique([]byte(s))
}

func (e mutableSet) Has(s string) bool {
Expand All @@ -70,6 +78,10 @@ func (e mutableSet) HasAny(strings []string) bool {
return false
}

func (e mutableSet) Count() int {
return int(e.cuckoo.Count())
}

type allStringsSet struct{}

func (e allStringsSet) HasAny(_ []string) bool {
Expand All @@ -83,3 +95,7 @@ func AllStrings() *allStringsSet {
func (e allStringsSet) Has(_ string) bool {
return true
}

func (e allStringsSet) Count() int {
return -1
}

0 comments on commit 7fbc950

Please sign in to comment.