Skip to content
Myr

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

7 files+32 added−17 removed

modified:src/cache.go+8 added
20 unmodified lines
21 cc.cache = make(map[*Chunk]*queryCache)22 cc.mutex.Unlock()23}2425func (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}3233// Add adds the list to the cache34func (cc *ChunkCache) Add(chunk *Chunk, key string, list []Result) {35 if len(key) == 0 || !chunk.IsFull() || len(list) > queryCacheMax {36 return
59 unmodified lines
modified:src/chunklist_test.go+2 added-2 removed
9 unmodified lines
10func TestChunkList(t *testing.T) {11 // FIXME global12 sortCriteria = []criterion{byScore, byLength}1314 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 lines
79 }80}8182func 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/2
29 unmodified lines
modified:src/chunklist.go+8 added-3 removed
14 unmodified lines
15type ChunkList struct {16 chunks []*Chunk17 mutex sync.Mutex18 trans ItemBuilder19 cache *ChunkCache20}2122// NewChunkList returns a new ChunkList22func NewChunkList(trans ItemBuilder) *ChunkList {23func 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}3031func (c *Chunk) push(trans ItemBuilder, data []byte) bool {32 if trans(&c.items[c.count], data) {
60 unmodified lines
93 }9495 // 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:])100101 for left, i := tail, len(ret)-1; i >= 0; i-- {102 chunk := ret[i]103 if chunk.count > left {
3 unmodified lines
107 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 lines
modified:src/core.go+3 added-7 removed
30 unmodified lines
31func (r *revision) bumpMinor() {32 r.minor++33}3435func (r revision) equals(other revision) bool {36 return r.major == other.major && r.minor == other.minor37}3835func (r revision) compatible(other revision) bool {36 return r.major == other.major37}38
50 unmodified lines
89 }90 }9192 // 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 false
3 unmodified lines
106 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 lines
166 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 lines
modified:src/matcher.go+6 added-4 removed
85 unmodified lines
86 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 }9596 // Restart search
97 unmodified lines
194 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 lines
235 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}241242// Reset is called to interrupt/signal the ongoing search243func (m *Matcher) Reset(chunks []*Chunk, patternRunes []rune, cancel bool, final bool, sort bool, revision revision) {
4 unmodified lines
248 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}254255func (m *Matcher) Stop() {256 m.reqBox.Set(reqQuit, nil)
1 unmodified line
modified:src/terminal.go+1 added-1 removed
1172 unmodified lines
1173 }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 lines
modified:CHANGELOG.md+4 added
1CHANGELOG2=========340.53.15------6- Bug fixes and minor improvements780.53.09------10- Multi-line display11 - See [Processing multi-line items](https://junegunn.github.io/fzf/tips/processing-multi-line-items/)
2144 unmodified lines