diff options
author | Junegunn Choi <junegunn.c@gmail.com> | 2015-04-17 22:23:52 +0900 |
---|---|---|
committer | Junegunn Choi <junegunn.c@gmail.com> | 2015-04-17 22:23:52 +0900 |
commit | 2fe1e28220c543ddbf4e12ee7396e44ee85ad8e0 (patch) | |
tree | 82c77b5e639cd66b941356788bcb8e0e053949be /src/cache.go | |
parent | 288131ac5a895ba335681339d85ee039557490da (diff) |
Improvements in performance and memory usage
I profiled fzf and it turned out that it was spending significant amount
of time repeatedly converting character arrays into Unicode codepoints.
This commit greatly improves search performance after the initial scan
by memoizing the converted results.
This commit also addresses the problem of unbounded memory usage of fzf.
fzf is a short-lived process that usually processes small input, so it
was implemented to cache the intermediate results very aggressively with
no notion of cache expiration/eviction. I still think a proper
implementation of caching scheme is definitely an overkill. Instead this
commit introduces limits to the maximum size (or minimum selectivity) of
the intermediate results that can be cached.
Diffstat (limited to 'src/cache.go')
-rw-r--r-- | src/cache.go | 12 |
1 files changed, 6 insertions, 6 deletions
diff --git a/src/cache.go b/src/cache.go index f2f84a0a..d2ec00b4 100644 --- a/src/cache.go +++ b/src/cache.go @@ -2,23 +2,23 @@ package fzf import "sync" -// QueryCache associates strings to lists of items -type QueryCache map[string][]*Item +// queryCache associates strings to lists of items +type queryCache map[string][]*Item // ChunkCache associates Chunk and query string to lists of items type ChunkCache struct { mutex sync.Mutex - cache map[*Chunk]*QueryCache + cache map[*Chunk]*queryCache } // NewChunkCache returns a new ChunkCache func NewChunkCache() ChunkCache { - return ChunkCache{sync.Mutex{}, make(map[*Chunk]*QueryCache)} + return ChunkCache{sync.Mutex{}, make(map[*Chunk]*queryCache)} } // Add adds the list to the cache func (cc *ChunkCache) Add(chunk *Chunk, key string, list []*Item) { - if len(key) == 0 || !chunk.IsFull() { + if len(key) == 0 || !chunk.IsFull() || len(list) > queryCacheMax { return } @@ -27,7 +27,7 @@ func (cc *ChunkCache) Add(chunk *Chunk, key string, list []*Item) { qc, ok := cc.cache[chunk] if !ok { - cc.cache[chunk] = &QueryCache{} + cc.cache[chunk] = &queryCache{} qc = cc.cache[chunk] } (*qc)[key] = list |