Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 23 additions & 5 deletions server/gsl/gsl.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package gsl
import (
"errors"
"sync"
"unsafe"

"github.com/nats-io/nats-server/v2/server/stree"
)
Expand Down Expand Up @@ -479,13 +480,20 @@ func IntersectStree[T1 any, T2 comparable](st *stree.SubjectTree[T1], sl *Generi
}

func intersectStree[T1 any, T2 comparable](st *stree.SubjectTree[T1], r *level[T2], subj []byte, cb func(subj []byte, entry *T1)) {
// This level could potentially match literals, despite being followed up by
// additional wildcards. For literals we can use Find since it is considerably
// faster. Then we can carry on checking for further matches in the usual way.
wc := subjectHasWildcard(bytesToString(subj))
if !wc {
if e, ok := st.Find(subj); ok {
cb(subj, e)
}
}
if r.numNodes() == 0 {
// For wildcards we can't avoid Match, but if it's a literal subject at
// this point, using Find is considerably cheaper.
if subjectHasWildcard(string(subj)) {
// No further recursions to be made at this point but there's still a wildcard
// to match, so let the subject tree work it out.
if wc {
st.Match(subj, cb)
} else if e, ok := st.Find(subj); ok {
cb(subj, e)
}
return
}
Expand Down Expand Up @@ -530,3 +538,13 @@ func subjectHasWildcard(subject string) bool {
}
return false
}

// Note this will avoid a copy of the data used for the string, but it will also reference the existing slice's data pointer.
// So this should be used sparingly when we know the encompassing byte slice's lifetime is the same.
func bytesToString(b []byte) string {
if len(b) == 0 {
return _EMPTY_
}
p := unsafe.SliceData(b)
return unsafe.String(p, len(b))
}
19 changes: 17 additions & 2 deletions server/gsl/gsl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,8 +301,11 @@ func TestGenericSublistInterestBasedIntersection(t *testing.T) {
st.Insert([]byte("one.two.six"), struct{}{})
st.Insert([]byte("one.two.seven"), struct{}{})
st.Insert([]byte("eight.nine"), struct{}{})
st.Insert([]byte("stream.A"), struct{}{})
st.Insert([]byte("stream.A.child"), struct{}{})

require_NoDuplicates := func(t *testing.T, got map[string]int) {
t.Helper()
for _, c := range got {
require_Equal(t, c, 1)
}
Expand Down Expand Up @@ -355,7 +358,7 @@ func TestGenericSublistInterestBasedIntersection(t *testing.T) {
IntersectStree(st, sl, func(subj []byte, entry *struct{}) {
got[string(subj)]++
})
require_Len(t, len(got), 5)
require_Len(t, len(got), 7)
require_NoDuplicates(t, got)
})

Expand All @@ -382,14 +385,26 @@ func TestGenericSublistInterestBasedIntersection(t *testing.T) {
require_NoDuplicates(t, got)
})

t.Run("FWCExtended", func(t *testing.T) {
got := map[string]int{}
sl := NewSublist[int]()
require_NoError(t, sl.Insert("stream.A.>", 11))
require_NoError(t, sl.Insert("stream.A", 22))
IntersectStree(st, sl, func(subj []byte, entry *struct{}) {
got[string(subj)]++
})
require_Len(t, len(got), 2)
require_NoDuplicates(t, got)
})

t.Run("FWCAll", func(t *testing.T) {
got := map[string]int{}
sl := NewSublist[int]()
require_NoError(t, sl.Insert(">", 11))
IntersectStree(st, sl, func(subj []byte, entry *struct{}) {
got[string(subj)]++
})
require_Len(t, len(got), 5)
require_Len(t, len(got), 7)
require_NoDuplicates(t, got)
})

Expand Down