From 3ee76b56abde53bde71ca73466c98e63cd1faa96 Mon Sep 17 00:00:00 2001 From: Junegunn Choi Date: Mon, 6 May 2024 19:15:36 +0900 Subject: [PATCH] Hold reference to the global variable --- src/cache.go | 4 ++-- src/pattern.go | 10 ++++++---- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/cache.go b/src/cache.go index df1a6ab7..ced2a26c 100644 --- a/src/cache.go +++ b/src/cache.go @@ -12,8 +12,8 @@ type ChunkCache struct { } // NewChunkCache returns a new ChunkCache -func NewChunkCache() ChunkCache { - return ChunkCache{sync.Mutex{}, make(map[*Chunk]*queryCache)} +func NewChunkCache() *ChunkCache { + return &ChunkCache{sync.Mutex{}, make(map[*Chunk]*queryCache)} } // Add adds the list to the cache diff --git a/src/pattern.go b/src/pattern.go index 71441b06..8f6ba0ae 100644 --- a/src/pattern.go +++ b/src/pattern.go @@ -60,12 +60,13 @@ type Pattern struct { delimiter Delimiter nth []Range procFun map[termType]algo.Algo + cache *ChunkCache } var ( _patternCache map[string]*Pattern _splitRegex *regexp.Regexp - _cache ChunkCache + _cache *ChunkCache ) func init() { @@ -157,6 +158,7 @@ func BuildPattern(fuzzy bool, fuzzyAlgo algo.Algo, extended bool, caseMode Case, cacheable: cacheable, nth: nth, delimiter: delimiter, + cache: _cache, procFun: make(map[termType]algo.Algo)} ptr.cacheKey = ptr.buildCacheKey() @@ -286,18 +288,18 @@ func (p *Pattern) Match(chunk *Chunk, slab *util.Slab) []Result { // ChunkCache: Exact match cacheKey := p.CacheKey() if p.cacheable { - if cached := _cache.Lookup(chunk, cacheKey); cached != nil { + if cached := p.cache.Lookup(chunk, cacheKey); cached != nil { return cached } } // Prefix/suffix cache - space := _cache.Search(chunk, cacheKey) + space := p.cache.Search(chunk, cacheKey) matches := p.matchChunk(chunk, space, slab) if p.cacheable { - _cache.Add(chunk, cacheKey, matches) + p.cache.Add(chunk, cacheKey, matches) } return matches }