Skip to content
Merged
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
27 changes: 3 additions & 24 deletions server/gsl/gsl.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ package gsl

import (
"errors"
"strings"
"sync"
"unsafe"

Expand Down Expand Up @@ -87,24 +88,13 @@ func NewSublist[T comparable]() *GenericSublist[T] {

// Insert adds a subscription into the sublist
func (s *GenericSublist[T]) Insert(subject string, value T) error {
tsa := [32]string{}
tokens := tsa[:0]
start := 0
for i := 0; i < len(subject); i++ {
if subject[i] == btsep {
tokens = append(tokens, subject[start:i])
start = i + 1
}
}
tokens = append(tokens, subject[start:])

s.Lock()

var sfwc bool
var n *node[T]
l := s.root

for _, t := range tokens {
for t := range strings.SplitSeq(subject, tsep) {
Comment thread
derekcollison marked this conversation as resolved.
lt := len(t)
if lt == 0 || sfwc {
s.Unlock()
Expand Down Expand Up @@ -312,17 +302,6 @@ type lnt[T comparable] struct {

// Raw low level remove, can do batches with lock held outside.
func (s *GenericSublist[T]) remove(subject string, value T, shouldLock bool) error {
tsa := [32]string{}
tokens := tsa[:0]
start := 0
for i := 0; i < len(subject); i++ {
if subject[i] == btsep {
tokens = append(tokens, subject[start:i])
start = i + 1
}
}
tokens = append(tokens, subject[start:])

if shouldLock {
s.Lock()
defer s.Unlock()
Expand All @@ -336,7 +315,7 @@ func (s *GenericSublist[T]) remove(subject string, value T, shouldLock bool) err
var lnts [32]lnt[T]
levels := lnts[:0]

for _, t := range tokens {
for t := range strings.SplitSeq(subject, tsep) {
Comment thread
derekcollison marked this conversation as resolved.
lt := len(t)
if lt == 0 || sfwc {
return ErrInvalidSubject
Expand Down
7 changes: 4 additions & 3 deletions server/subject_transform.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"math"
"math/rand"
"regexp"
"slices"
"strconv"
"strings"
)
Expand Down Expand Up @@ -378,7 +379,7 @@ func transformTokenize(subject string) string {
// We need to make the appropriate markers for the wildcards etc.
i := 1
var nda []string
for _, token := range strings.Split(subject, tsep) {
for token := range strings.SplitSeq(subject, tsep) {
if token == pwcs {
nda = append(nda, fmt.Sprintf("$%d", i))
i++
Expand All @@ -399,7 +400,7 @@ func transformUntokenize(subject string) (string, []string) {
var phs []string
var nda []string

for _, token := range strings.Split(subject, tsep) {
for token := range strings.SplitSeq(subject, tsep) {
if args := getMappingFunctionArgs(wildcardMappingFunctionRegEx, token); (len(token) > 1 && token[0] == '$' && token[1] >= '1' && token[1] <= '9') || (len(args) == 1 && args[0] != _EMPTY_) {
phs = append(phs, token)
nda = append(nda, pwcs)
Expand Down Expand Up @@ -439,7 +440,7 @@ func (tr *subjectTransform) Match(subject string) (string, error) {
tts := tokenizeSubject(subject)

// TODO(jnm): optimization -> not sure this is actually needed but was there in initial code
if !isValidLiteralSubject(tts) {
if !isValidLiteralSubject(slices.Values(tts)) {
return _EMPTY_, ErrBadSubject
}

Expand Down
37 changes: 8 additions & 29 deletions server/sublist.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package server
import (
"bytes"
"errors"
"iter"
"strings"
"sync"
"sync/atomic"
Expand Down Expand Up @@ -357,24 +358,14 @@ func (s *Sublist) chkForRemoveNotification(subject, queue string) {
func (s *Sublist) Insert(sub *subscription) error {
// copy the subject since we hold this and this might be part of a large byte slice.
subject := string(sub.subject)
tsa := [32]string{}
tokens := tsa[:0]
start := 0
for i := 0; i < len(subject); i++ {
if subject[i] == btsep {
tokens = append(tokens, subject[start:i])
start = i + 1
}
}
tokens = append(tokens, subject[start:])

s.Lock()

var sfwc, haswc, isnew bool
var n *node
l := s.root

for _, t := range tokens {
for t := range strings.SplitSeq(subject, tsep) {
lt := len(t)
if lt == 0 || sfwc {
s.Unlock()
Expand Down Expand Up @@ -851,16 +842,6 @@ type lnt struct {
// Raw low level remove, can do batches with lock held outside.
func (s *Sublist) remove(sub *subscription, shouldLock bool, doCacheUpdates bool) error {
subject := string(sub.subject)
tsa := [32]string{}
tokens := tsa[:0]
start := 0
for i := 0; i < len(subject); i++ {
if subject[i] == btsep {
tokens = append(tokens, subject[start:i])
start = i + 1
}
}
tokens = append(tokens, subject[start:])

if shouldLock {
s.Lock()
Expand All @@ -875,7 +856,7 @@ func (s *Sublist) remove(sub *subscription, shouldLock bool, doCacheUpdates bool
var lnts [32]lnt
levels := lnts[:0]

for _, t := range tokens {
for t := range strings.SplitSeq(subject, tsep) {
lt := len(t)
if lt == 0 || sfwc {
return ErrInvalidSubject
Expand Down Expand Up @@ -1230,8 +1211,7 @@ func isValidSubject(subject string, checkRunes bool) bool {
}
}
sfwc := false
tokens := strings.Split(subject, tsep)
for _, t := range tokens {
for t := range strings.SplitSeq(subject, tsep) {
length := len(t)
if length == 0 || sfwc {
return false
Expand All @@ -1254,12 +1234,12 @@ func isValidSubject(subject string, checkRunes bool) bool {

// IsValidLiteralSubject returns true if a subject is valid and literal (no wildcards), false otherwise
func IsValidLiteralSubject(subject string) bool {
return isValidLiteralSubject(strings.Split(subject, tsep))
return isValidLiteralSubject(strings.SplitSeq(subject, tsep))
}

// isValidLiteralSubject returns true if the tokens are valid and literal (no wildcards), false otherwise
func isValidLiteralSubject(tokens []string) bool {
for _, t := range tokens {
func isValidLiteralSubject(tokens iter.Seq[string]) bool {
for t := range tokens {
if len(t) == 0 {
return false
}
Expand All @@ -1279,9 +1259,8 @@ func ValidateMapping(src string, dest string) error {
if dest == _EMPTY_ {
return nil
}
subjectTokens := strings.Split(dest, tsep)
sfwc := false
for _, t := range subjectTokens {
for t := range strings.SplitSeq(dest, tsep) {
length := len(t)
if length == 0 || sfwc {
return &mappingDestinationErr{t, ErrInvalidMappingDestinationSubject}
Expand Down