mirror of
https://github.com/junegunn/fzf.git
synced 2026-06-16 04:26:37 +08:00
2f27a3ede2
Replace the per-chunk query cache from []Result slices to fixed-size bitmaps (ChunkBitmap: [16]uint64 = 128 bytes per entry). Each bit indicates whether the corresponding item in the chunk matched. This reduces cache memory by 86 times in testing: - Old []Result cache: ~22KB per chunk per query (for 500 matches) - New bitmap cache: ~262 bytes per chunk per query (fixed) With the reduced per-entry cost, queryCacheMax is raised from chunkSize/5 to chunkSize/2, allowing broader queries (up to 50% match rate) to be cached while still using far less memory.
40 lines
847 B
Go
40 lines
847 B
Go
package fzf
|
|
|
|
import "testing"
|
|
|
|
func TestChunkCache(t *testing.T) {
|
|
cache := NewChunkCache()
|
|
chunk1p := &Chunk{}
|
|
chunk2p := &Chunk{count: chunkSize}
|
|
bm1 := ChunkBitmap{1}
|
|
bm2 := ChunkBitmap{1, 2}
|
|
cache.Add(chunk1p, "foo", bm1, 1)
|
|
cache.Add(chunk2p, "foo", bm1, 1)
|
|
cache.Add(chunk2p, "bar", bm2, 2)
|
|
|
|
{ // chunk1 is not full
|
|
cached := cache.Lookup(chunk1p, "foo")
|
|
if cached != nil {
|
|
t.Error("Cached disabled for non-full chunks", cached)
|
|
}
|
|
}
|
|
{
|
|
cached := cache.Lookup(chunk2p, "foo")
|
|
if cached == nil || cached[0] != 1 {
|
|
t.Error("Expected bitmap cached", cached)
|
|
}
|
|
}
|
|
{
|
|
cached := cache.Lookup(chunk2p, "bar")
|
|
if cached == nil || cached[1] != 2 {
|
|
t.Error("Expected bitmap cached", cached)
|
|
}
|
|
}
|
|
{
|
|
cached := cache.Lookup(chunk1p, "foobar")
|
|
if cached != nil {
|
|
t.Error("Expected nil cached", cached)
|
|
}
|
|
}
|
|
}
|