Skip to content

Commit

Permalink
Reduce allocations in isMatch in filestore/memstore
Browse files Browse the repository at this point in the history
Otherwise long subjects could cause us to make the same reallocations
on each call to `tokenizeSubjectIntoSlice` without reusing the newly
sized underlying array for the next call.

Signed-off-by: Neil Twigg <[email protected]>
  • Loading branch information
neilalexander committed Jun 20, 2024
1 parent 5749c41 commit f6f45e9
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 11 deletions.
11 changes: 5 additions & 6 deletions server/filestore.go
Original file line number Diff line number Diff line change
Expand Up @@ -2325,16 +2325,15 @@ func (mb *msgBlock) firstMatching(filter string, wc bool, start uint64, sm *Stor
lseq := atomic.LoadUint64(&mb.last.seq)

// Optionally build the isMatch for wildcard filters.
tsa := [32]string{}
fsa := [32]string{}
var fts []string
_tsa, _fsa := [32]string{}, [32]string{}
tsa, fsa := _tsa[:0], _fsa[:0]
var isMatch func(subj string) bool
// Decide to build.
if wc {
fts = tokenizeSubjectIntoSlice(fsa[:0], filter)
fsa = tokenizeSubjectIntoSlice(fsa[:0], filter)
isMatch = func(subj string) bool {
tts := tokenizeSubjectIntoSlice(tsa[:0], subj)
return isSubsetMatchTokenized(tts, fts)
tsa = tokenizeSubjectIntoSlice(tsa[:0], subj)
return isSubsetMatchTokenized(tsa, fsa)
}
}

Expand Down
10 changes: 5 additions & 5 deletions server/memstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -389,9 +389,9 @@ func (ms *memStore) filteredStateLocked(sseq uint64, filter string, lastPerSubje
}
}

tsa := [32]string{}
fsa := [32]string{}
fts := tokenizeSubjectIntoSlice(fsa[:0], filter)
_tsa, _fsa := [32]string{}, [32]string{}
tsa, fsa := _tsa[:0], _fsa[:0]
fsa = tokenizeSubjectIntoSlice(fsa[:0], filter)
wc := subjectHasWildcard(filter)

// 1. See if we match any subs from fss.
Expand All @@ -405,8 +405,8 @@ func (ms *memStore) filteredStateLocked(sseq uint64, filter string, lastPerSubje
if !wc {
return subj == filter
}
tts := tokenizeSubjectIntoSlice(tsa[:0], subj)
return isSubsetMatchTokenized(tts, fts)
tsa = tokenizeSubjectIntoSlice(tsa[:0], subj)
return isSubsetMatchTokenized(tsa, fsa)
}

update := func(fss *SimpleState) {
Expand Down

0 comments on commit f6f45e9

Please sign in to comment.