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/chunklist.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/chunklist.go')
-rw-r--r-- | src/chunklist.go | 11 |
1 files changed, 4 insertions, 7 deletions
diff --git a/src/chunklist.go b/src/chunklist.go index 571a59af..52084f2f 100644 --- a/src/chunklist.go +++ b/src/chunklist.go @@ -2,10 +2,7 @@ package fzf import "sync" -// Capacity of each chunk -const ChunkSize int = 100 - -// Chunk is a list of Item pointers whose size has the upper limit of ChunkSize +// Chunk is a list of Item pointers whose size has the upper limit of chunkSize type Chunk []*Item // >>> []Item // ItemBuilder is a closure type that builds Item object from a pointer to a @@ -35,7 +32,7 @@ func (c *Chunk) push(trans ItemBuilder, data *string, index int) { // IsFull returns true if the Chunk is full func (c *Chunk) IsFull() bool { - return len(*c) == ChunkSize + return len(*c) == chunkSize } func (cl *ChunkList) lastChunk() *Chunk { @@ -47,7 +44,7 @@ func CountItems(cs []*Chunk) int { if len(cs) == 0 { return 0 } - return ChunkSize*(len(cs)-1) + len(*(cs[len(cs)-1])) + return chunkSize*(len(cs)-1) + len(*(cs[len(cs)-1])) } // Push adds the item to the list @@ -56,7 +53,7 @@ func (cl *ChunkList) Push(data string) { defer cl.mutex.Unlock() if len(cl.chunks) == 0 || cl.lastChunk().IsFull() { - newChunk := Chunk(make([]*Item, 0, ChunkSize)) + newChunk := Chunk(make([]*Item, 0, chunkSize)) cl.chunks = append(cl.chunks, &newChunk) } |