Skip to content

Commit

Permalink
Add FreeLRU to the GC comparison
Browse files Browse the repository at this point in the history
  • Loading branch information
rockdaboot committed Jan 8, 2024
1 parent e2eb9f8 commit 1dd118e
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
29 changes: 29 additions & 0 deletions caches_gc_overhead_comparison.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (

"github.com/allegro/bigcache/v3"
"github.com/coocood/freecache"
"github.com/elastic/go-freelru"
"github.com/zeebo/xxh3"
)

var previousPause time.Duration
Expand Down Expand Up @@ -50,6 +52,8 @@ func main() {
benchFunc = bigCache
case "stdmap":
benchFunc = stdMap
case "freelru":
benchFunc = freeLRU
default:
fmt.Printf("unknown cache: %s", c)
os.Exit(1)
Expand Down Expand Up @@ -89,6 +93,31 @@ func freeCache(kv *keyValueStore) {
}
}

func freeLRU(kv *keyValueStore) {
// Using NewSynced() here to stay fair with concurrency.
// Using New() would be faster, but not thread-safe.
freeLRU, err := freelru.NewSynced[string, []byte](uint32(kv.Size()), hashString)
if err != nil {
fmt.Println("Failed to create freeLRU: ", err.Error())
return
}

for i := 0; i < kv.Size(); i++ {
freeLRU.Add(kv.Key(i), kv.Value(i))
}

v, ok := freeLRU.Get(kv.Key(1))
if !ok {
fmt.Println("First item not found")
return
}
checkFirstElement(kv.Value(1), v, nil)
}

func hashString(s string) uint32 {
return uint32(xxh3.HashString(s))
}

func bigCache(kv *keyValueStore) {
config := bigcache.Config{
Shards: 256,
Expand Down
7 changes: 6 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@ go 1.19
require (
github.com/allegro/bigcache/v3 v3.1.0
github.com/coocood/freecache v1.2.4
github.com/elastic/go-freelru v0.9.0
github.com/orcaman/concurrent-map/v2 v2.0.1
github.com/zeebo/xxh3 v1.0.2
)

require github.com/cespare/xxhash/v2 v2.2.0 // indirect
require (
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/klauspost/cpuid/v2 v2.0.9 // indirect
)
7 changes: 7 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,12 @@ github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj
github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/coocood/freecache v1.2.4 h1:UdR6Yz/X1HW4fZOuH0Z94KwG851GWOSknua5VUbb/5M=
github.com/coocood/freecache v1.2.4/go.mod h1:RBUWa/Cy+OHdfTGFEhEuE1pMCMX51Ncizj7rthiQ3vk=
github.com/elastic/go-freelru v0.9.0 h1:s9K5Q4xBoQC96XogjymtKDYfcABkoyDUoSIG4vprywg=
github.com/elastic/go-freelru v0.9.0/go.mod h1:bSdWT4M0lW79K8QbX6XY2heQYSCqD7THoYf82pT/H3I=
github.com/klauspost/cpuid/v2 v2.0.9 h1:lgaqFMSdTdQYdZ04uHyN2d/eKdOMyi2YLSvlQIBFYa4=
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
github.com/orcaman/concurrent-map/v2 v2.0.1 h1:jOJ5Pg2w1oeB6PeDurIYf6k9PQ+aTITr/6lP/L/zp6c=
github.com/orcaman/concurrent-map/v2 v2.0.1/go.mod h1:9Eq3TG2oBe5FirmYWQfYO5iH1q0Jv47PLaNK++uCdOM=
github.com/zeebo/assert v1.3.0 h1:g7C04CbJuIDKNPFHmsk4hwZDO5O+kntRxzaUoNXj+IQ=
github.com/zeebo/xxh3 v1.0.2 h1:xZmwmqxHZA8AI603jOQ0tMqmBr9lPeFwGg6d+xy9DC0=
github.com/zeebo/xxh3 v1.0.2/go.mod h1:5NWz9Sef7zIDm2JHfFlcQvNekmcEl9ekUZQQKCYaDcA=

0 comments on commit 1dd118e

Please sign in to comment.