From eb28cf0eda59434ee96bdf3e907114ef02e95c07 Mon Sep 17 00:00:00 2001 From: Ivan Kozlovic Date: Sat, 10 Mar 2018 08:35:15 -0700 Subject: [PATCH] Do the copy only of the array of subscriptions that we add to the results Instead of making a copy of the whole results, make sure that we don't pass a sublist array to the result but its copy. --- server/sublist.go | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/server/sublist.go b/server/sublist.go index 62bfdb8b..ffa185a4 100644 --- a/server/sublist.go +++ b/server/sublist.go @@ -236,13 +236,8 @@ func (s *Sublist) Match(subject string) *SublistResult { s.Lock() matchLevel(s.root, tokens, result) - // Store in cache and return to caller a copy of the results to avoid - // race when sub is removed from sublist and caller walks through the - // results. - cr := copyResult(result) - // Add to our cache - s.cache[subject] = cr + s.cache[subject] = result // Bound the number of entries to sublistMaxCache if len(s.cache) > slCacheMax { for k := range s.cache { @@ -252,7 +247,7 @@ func (s *Sublist) Match(subject string) *SublistResult { } s.Unlock() - return cr + return result } // This will add in a node's results to the total results. @@ -266,7 +261,9 @@ func addNodeToResults(n *node, results *SublistResult) { if i := findQSliceForSub(qr[0], results.qsubs); i >= 0 { results.qsubs[i] = append(results.qsubs[i], qr...) } else { - results.qsubs = append(results.qsubs, qr) + copyqr := make([]*subscription, len(qr)) + copy(copyqr, qr) + results.qsubs = append(results.qsubs, copyqr) } } }