Skip to content

Commit f6f45e9

Browse files
committed
Reduce allocations in isMatch in filestore/memstore
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]>
1 parent 5749c41 commit f6f45e9

File tree

2 files changed

+10
-11
lines changed

2 files changed

+10
-11
lines changed

server/filestore.go

+5-6
Original file line numberDiff line numberDiff line change
@@ -2325,16 +2325,15 @@ func (mb *msgBlock) firstMatching(filter string, wc bool, start uint64, sm *Stor
23252325
lseq := atomic.LoadUint64(&mb.last.seq)
23262326

23272327
// Optionally build the isMatch for wildcard filters.
2328-
tsa := [32]string{}
2329-
fsa := [32]string{}
2330-
var fts []string
2328+
_tsa, _fsa := [32]string{}, [32]string{}
2329+
tsa, fsa := _tsa[:0], _fsa[:0]
23312330
var isMatch func(subj string) bool
23322331
// Decide to build.
23332332
if wc {
2334-
fts = tokenizeSubjectIntoSlice(fsa[:0], filter)
2333+
fsa = tokenizeSubjectIntoSlice(fsa[:0], filter)
23352334
isMatch = func(subj string) bool {
2336-
tts := tokenizeSubjectIntoSlice(tsa[:0], subj)
2337-
return isSubsetMatchTokenized(tts, fts)
2335+
tsa = tokenizeSubjectIntoSlice(tsa[:0], subj)
2336+
return isSubsetMatchTokenized(tsa, fsa)
23382337
}
23392338
}
23402339

server/memstore.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -389,9 +389,9 @@ func (ms *memStore) filteredStateLocked(sseq uint64, filter string, lastPerSubje
389389
}
390390
}
391391

392-
tsa := [32]string{}
393-
fsa := [32]string{}
394-
fts := tokenizeSubjectIntoSlice(fsa[:0], filter)
392+
_tsa, _fsa := [32]string{}, [32]string{}
393+
tsa, fsa := _tsa[:0], _fsa[:0]
394+
fsa = tokenizeSubjectIntoSlice(fsa[:0], filter)
395395
wc := subjectHasWildcard(filter)
396396

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

412412
update := func(fss *SimpleState) {

0 commit comments

Comments
 (0)