mirror of
https://github.com/Llewellynvdm/fzf.git
synced 2024-11-17 10:35:10 +00:00
2fe1e28220
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.
41 lines
907 B
Go
41 lines
907 B
Go
package fzf
|
|
|
|
import "testing"
|
|
|
|
func TestChunkCache(t *testing.T) {
|
|
cache := NewChunkCache()
|
|
chunk2 := make(Chunk, chunkSize)
|
|
chunk1p := &Chunk{}
|
|
chunk2p := &chunk2
|
|
items1 := []*Item{&Item{}}
|
|
items2 := []*Item{&Item{}, &Item{}}
|
|
cache.Add(chunk1p, "foo", items1)
|
|
cache.Add(chunk2p, "foo", items1)
|
|
cache.Add(chunk2p, "bar", items2)
|
|
|
|
{ // chunk1 is not full
|
|
cached, found := cache.Find(chunk1p, "foo")
|
|
if found {
|
|
t.Error("Cached disabled for non-empty chunks", found, cached)
|
|
}
|
|
}
|
|
{
|
|
cached, found := cache.Find(chunk2p, "foo")
|
|
if !found || len(cached) != 1 {
|
|
t.Error("Expected 1 item cached", found, cached)
|
|
}
|
|
}
|
|
{
|
|
cached, found := cache.Find(chunk2p, "bar")
|
|
if !found || len(cached) != 2 {
|
|
t.Error("Expected 2 items cached", found, cached)
|
|
}
|
|
}
|
|
{
|
|
cached, found := cache.Find(chunk1p, "foobar")
|
|
if found {
|
|
t.Error("Expected 0 item cached", found, cached)
|
|
}
|
|
}
|
|
}
|