Skip to content

Commit

Permalink
Don't take sublist write lock in match if sublist cache disabled (#…
Browse files Browse the repository at this point in the history
…4594)

We may be creating unnecessary lock contention on the sublist when the
cache is disabled by taking the write lock anyway.

Signed-off-by: Neil Twigg <neil@nats.io>
  • Loading branch information
derekcollison committed Sep 27, 2023
2 parents 4c17eeb + 02d48dd commit cbc490a
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions server/sublist.go
Expand Up @@ -540,6 +540,7 @@ func (s *Sublist) match(subject string, doLock bool) *SublistResult {
if doLock {
s.RLock()
}
cacheEnabled := s.cache != nil
r, ok := s.cache[subject]
if doLock {
s.RUnlock()
Expand Down Expand Up @@ -574,24 +575,32 @@ func (s *Sublist) match(subject string, doLock bool) *SublistResult {
var n int

if doLock {
s.Lock()
if cacheEnabled {
s.Lock()
} else {
s.RLock()
}
}

matchLevel(s.root, tokens, result)
// Check for empty result.
if len(result.psubs) == 0 && len(result.qsubs) == 0 {
result = emptyResult
}
if s.cache != nil {
if cacheEnabled {
s.cache[subject] = result
n = len(s.cache)
}
if doLock {
s.Unlock()
if cacheEnabled {
s.Unlock()
} else {
s.RUnlock()
}
}

// Reduce the cache count if we have exceeded our set maximum.
if n > slCacheMax && atomic.CompareAndSwapInt32(&s.ccSweep, 0, 1) {
if cacheEnabled && n > slCacheMax && atomic.CompareAndSwapInt32(&s.ccSweep, 0, 1) {
go s.reduceCacheCount()
}

Expand Down

0 comments on commit cbc490a

Please sign in to comment.