Less aggressive chunk cache invalidation for --tail
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Junegunn Choi · committed by Junegunn Choi · parent dfee7af · Browse files · 7 files·+32 added−17 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
7 files+32 added−17 removed
- other:+32 added−17 removed
Provenance
- Commit
7fc13c5- Tree
aaeddb5- Author
Junegunn Choi <junegunn.c@gmail.com>- Committer
Junegunn Choi <junegunn.c@gmail.com>- Parent
dfee7af
7 files changed+32 added−17 removed
modified:src/cache.go+8 added
20 unmodified lines21 cc.cache = make(map[*Chunk]*queryCache)22 cc.mutex.Unlock()23 }24 25+func (cc *ChunkCache) retire(chunk ...*Chunk) {26+ cc.mutex.Lock()27+ for _, c := range chunk {28+ delete(cc.cache, c)29+ }30+ cc.mutex.Unlock()31+}32+33 // Add adds the list to the cache34 func (cc *ChunkCache) Add(chunk *Chunk, key string, list []Result) {35 if len(key) == 0 || !chunk.IsFull() || len(list) > queryCacheMax {36 return59 unmodified linesmodified:src/chunklist_test.go+2 added-2 removed
9 unmodified lines10 func TestChunkList(t *testing.T) {11 // FIXME global12 sortCriteria = []criterion{byScore, byLength}13 14- cl := NewChunkList(func(item *Item, s []byte) bool {14+ cl := NewChunkList(NewChunkCache(), func(item *Item, s []byte) bool {15 item.text = util.ToChars(s)16 return true17 })18 60 unmodified lines79 }80 }81 82 func TestChunkListTail(t *testing.T) {83- cl := NewChunkList(func(item *Item, s []byte) bool {83+ cl := NewChunkList(NewChunkCache(), func(item *Item, s []byte) bool {84 item.text = util.ToChars(s)85 return true86 })87 total := chunkSize*2 + chunkSize/229 unmodified linesmodified:src/chunklist.go+8 added-3 removed
14 unmodified lines15 type ChunkList struct {16 chunks []*Chunk17 mutex sync.Mutex18 trans ItemBuilder19+ cache *ChunkCache20 }21 22 // NewChunkList returns a new ChunkList22-func NewChunkList(trans ItemBuilder) *ChunkList {23+func NewChunkList(cache *ChunkCache, trans ItemBuilder) *ChunkList {24 return &ChunkList{25 chunks: []*Chunk{},26 mutex: sync.Mutex{},26- trans: trans}27+ trans: trans,28+ cache: cache}29 }30 31 func (c *Chunk) push(trans ItemBuilder, data []byte) bool {32 if trans(&c.items[c.count], data) {60 unmodified lines93 }94 95 // Copy the chunks to keep96 ret := make([]*Chunk, numChunks)95- copy(ret, cl.chunks[len(cl.chunks)-numChunks:])97+ minIndex := len(cl.chunks) - numChunks98+ cl.cache.retire(cl.chunks[:minIndex]...)99+ copy(ret, cl.chunks[minIndex:])100 101 for left, i := tail, len(ret)-1; i >= 0; i-- {102 chunk := ret[i]103 if chunk.count > left {3 unmodified lines107 for i := 0; i < left; i++ {108 newChunk.items[i] = chunk.items[oldCount-left+i]109 }110 ret[i] = &newChunk111+ cl.cache.retire(chunk)112 break113 }114 left -= chunk.count115 }19 unmodified linesmodified:src/core.go+3 added-7 removed
30 unmodified lines31 func (r *revision) bumpMinor() {32 r.minor++33 }34 35-func (r revision) equals(other revision) bool {36- return r.major == other.major && r.minor == other.minor37-}38-35 func (r revision) compatible(other revision) bool {36 return r.major == other.major37 }38 50 unmodified lines89 }90 }91 92 // Chunk list93+ cache := NewChunkCache()94 var chunkList *ChunkList95 var itemIndex int3296 header := make([]string, 0, opts.HeaderLines)97 if len(opts.WithNth) == 0 {101- chunkList = NewChunkList(func(item *Item, data []byte) bool {98+ chunkList = NewChunkList(cache, func(item *Item, data []byte) bool {99 if len(header) < opts.HeaderLines {100 header = append(header, byteString(data))101 eventBox.Set(EvtHeader, header)102 return false3 unmodified lines106 itemIndex++107 return true108 })109 } else {113- chunkList = NewChunkList(func(item *Item, data []byte) bool {110+ chunkList = NewChunkList(cache, func(item *Item, data []byte) bool {111 tokens := Tokenize(byteString(data), opts.Delimiter)112 if opts.Ansi && opts.Theme.Colored && len(tokens) > 1 {113 var ansiState *ansiState114 if prevLineAnsiState != nil {51 unmodified lines166 case byBegin:167 forward = true168 }169 }173- cache := NewChunkCache()170 patternCache := make(map[string]*Pattern)171 patternBuilder := func(runes []rune) *Pattern {172 return BuildPattern(cache, patternCache,173 opts.Fuzzy, opts.FuzzyAlgo, opts.Extended, opts.Case, opts.Normalize, forward, withPos,274 unmodified linesmodified:src/matcher.go+6 added-4 removed
85 unmodified lines86 if request.sort != m.sort || request.revision != m.revision {87 m.sort = request.sort88 m.revision = request.revision89 m.mergerCache = make(map[string]*Merger)90+ if !request.revision.compatible(m.revision) {90- m.cache.Clear()91+ m.cache.Clear()92+ }93 cacheCleared = true94 }95 96 // Restart search97 unmodified lines194 sliceMatches := make([]Result, 0, count)195 for _, matches := range allMatches {196 sliceMatches = append(sliceMatches, matches...)197 }196- if m.sort {198+ if m.sort && request.pattern.sortable {199 if m.tac {200 sort.Sort(ByRelevanceTac(sliceMatches))201 } else {202 sort.Sort(ByRelevance(sliceMatches))32 unmodified lines235 for range slices {236 partialResult := <-resultChan237 partialResults[partialResult.index] = partialResult.matches238 }237- return NewMerger(pattern, partialResults, m.sort, m.tac, request.revision, minIndex), false239+ return NewMerger(pattern, partialResults, m.sort && request.pattern.sortable, m.tac, request.revision, minIndex), false240 }241 242 // Reset is called to interrupt/signal the ongoing search243 func (m *Matcher) Reset(chunks []*Chunk, patternRunes []rune, cancel bool, final bool, sort bool, revision revision) {4 unmodified lines248 event = reqReset249 } else {250 event = reqRetry251 }250- m.reqBox.Set(event, MatchRequest{chunks, pattern, final, sort && pattern.sortable, revision})252+ m.reqBox.Set(event, MatchRequest{chunks, pattern, final, sort, revision})253 }254 255 func (m *Matcher) Stop() {256 m.reqBox.Set(reqQuit, nil)1 unmodified linemodified:src/terminal.go+1 added-1 removed
1172 unmodified lines1173 }1174 }1175 t.progress = 1001176 t.merger = merger1177- if !t.revision.equals(newRevision) {1177+ if t.revision != newRevision {1178 if !t.revision.compatible(newRevision) {1179 // Reloaded: clear selection1180 t.selected = make(map[int32]selectedItem)1181 } else {3563 unmodified linesmodified:CHANGELOG.md+4 added
1 CHANGELOG2 =========3 4+0.53.15+------6+- Bug fixes and minor improvements7+8 0.53.09 ------10 - Multi-line display11 - See [Processing multi-line items](https://junegunn.github.io/fzf/tips/processing-multi-line-items/)2144 unmodified lines