Skip to content
Myr

Refactor cache lookup

Before adoption: this commit came in with the history the project adopted, so no landing records it.

- Remove multiple mutex locks in partial cache lookup - Simplify return values

authored by Junegunn Choi · committed by Junegunn Choi · parent 6725151 · Browse files · 2 files+34 added−20 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+34 added−20 removed

  • other:+34 added−20 removed

2 files+34 added−20 removed

modified:src/cache.go+32 added-4 removed
32 unmodified lines
33 (*qc)[key] = list34}3536// Find is called to lookup ChunkCache37func (cc *ChunkCache) Find(chunk *Chunk, key string) ([]*Result, bool) {37func (cc *ChunkCache) Find(chunk *Chunk, key string) []*Result {38 if len(key) == 0 || !chunk.IsFull() {39 return nil, false39 return nil40 }4142 cc.mutex.Lock()43 defer cc.mutex.Unlock()
1 unmodified line
45 qc, ok := cc.cache[chunk]46 if ok {47 list, ok := (*qc)[key]48 if ok {49 return list, true49 return list50 }51 }52 return nil, false52 return nil53}5455func (cc *ChunkCache) Search(chunk *Chunk, key string) []*Result {56 if len(key) == 0 || !chunk.IsFull() {57 return nil58 }5960 cc.mutex.Lock()61 defer cc.mutex.Unlock()6263 qc, ok := cc.cache[chunk]64 if !ok {65 return nil66 }6768 for idx := 1; idx < len(key); idx++ {69 // [---------| ] | [ |---------]70 // [--------| ] | [ |--------]71 // [-------| ] | [ |-------]72 prefix := key[:len(key)-idx]73 suffix := key[idx:]74 for _, substr := range [2]string{prefix, suffix} {75 if cached, found := (*qc)[substr]; found {76 return cached77 }78 }79 }80 return nil81}
modified:src/pattern.go+2 added-16 removed
245 unmodified lines
246func (p *Pattern) Match(chunk *Chunk, slab *util.Slab) []*Result {247 // ChunkCache: Exact match248 cacheKey := p.CacheKey()249 if p.cacheable {250 if cached, found := _cache.Find(chunk, cacheKey); found {250 if cached := _cache.Find(chunk, cacheKey); cached != nil {251 return cached252 }253 }254255 // Prefix/suffix cache256 var space []*Result257Loop:258 for idx := 1; idx < len(cacheKey); idx++ {259 // [---------| ] | [ |---------]260 // [--------| ] | [ |--------]261 // [-------| ] | [ |-------]262 prefix := cacheKey[:len(cacheKey)-idx]263 suffix := cacheKey[idx:]264 for _, substr := range [2]*string{&prefix, &suffix} {265 if cached, found := _cache.Find(chunk, *substr); found {266 space = cached267 break Loop268 }269 }270 }256 space := _cache.Search(chunk, cacheKey)257258 matches := p.matchChunk(chunk, space, slab)259260 if p.cacheable {
124 unmodified lines