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 Dec 21, 2023
1 parent 9e756d2 commit 95a1100
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 18 deletions.
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/v2"
"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
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ go 1.19
require (
github.com/allegro/bigcache/v2 v2.2.5
github.com/coocood/freecache v1.2.3
github.com/elastic/go-freelru v0.8.0
github.com/orcaman/concurrent-map/v2 v2.0.1
github.com/zeebo/xxh3 v1.0.2
)

require (
github.com/cespare/xxhash v1.1.0 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/klauspost/cpuid/v2 v2.0.9 // indirect
)
24 changes: 7 additions & 17 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,26 +1,16 @@
github.com/OneOfOne/xxhash v1.2.2 h1:KMrpdQIwFcEqXDklaen+P1axHaj9BSKzvpUUfnHldSE=
github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
github.com/allegro/bigcache/v2 v2.1.3 h1:8g6YBowSY3qw5+7OFy41EPNmM58slrMrX39Di41XE0M=
github.com/allegro/bigcache/v2 v2.1.3/go.mod h1:NE2LouLwhvFzJ55Wtx2x8uaOHfj4G19RRGPC8Kz3UUA=
github.com/allegro/bigcache/v2 v2.2.5 h1:mRc8r6GQjuJsmSKQNPsR5jQVXc8IJ1xsW5YXUYMLfqI=
github.com/allegro/bigcache/v2 v2.2.5/go.mod h1:FppZsIO+IZk7gCuj5FiIDHGygD9xvWQcqg1uIPMb6tY=
github.com/cespare/xxhash v1.1.0 h1:a6HrQnmkObjyL+Gs60czilIUGqrzKutQD6XZog3p+ko=
github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc=
github.com/cespare/xxhash/v2 v2.1.2/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44=
github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/coocood/freecache v1.1.0 h1:ENiHOsWdj1BrrlPwblhbn4GdAsMymK3pZORJ+bJGAjA=
github.com/coocood/freecache v1.1.0/go.mod h1:ePwxCDzOYvARfHdr1pByNct1at3CoKnsipOHwKlNbzI=
github.com/coocood/freecache v1.2.3 h1:lcBwpZrwBZRZyLk/8EMyQVXRiFl663cCuMOrjCALeto=
github.com/coocood/freecache v1.2.3/go.mod h1:RBUWa/Cy+OHdfTGFEhEuE1pMCMX51Ncizj7rthiQ3vk=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/elastic/go-freelru v0.8.0 h1:T4N1cAnrMIHvN3UmZ15wKT/yaVi9C9FgHeJqLoy+1lc=
github.com/elastic/go-freelru v0.8.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/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72 h1:qLC7fQah7D6K1B0ujays3HV9gkFtllcxhzImRR7ArPQ=
github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
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 95a1100

Please sign in to comment.