From 9543f9047f1e9f81e495c2f2dcaa722576273130 Mon Sep 17 00:00:00 2001 From: phuslu Date: Sun, 31 Dec 2023 21:37:13 +0800 Subject: [PATCH] update bench result to readme --- README.md | 33 +++++++++++++++++++-------------- cache.go | 8 ++++---- 2 files changed, 23 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 711663a..2871ddb 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# lru - a high-performance and gc-friendly LRU cache +# a high-performance and gc-friendly LRU cache [![godoc][godoc-img]][godoc] [![release][release-img]][release] [![goreport][goreport-img]][goreport] @@ -6,15 +6,15 @@ * Simple - No Dependency - - Less than 1000 sloc + - Less 500 lines of Go(excluding tests) * Fast - - Faster than all well-known LRU caches + - Faster than all well-known **LRU** caches - Zero memory allocs * GC friendly - Pointerless data structs - Continuous memory layout * Memory efficient - - Uses 24 extra bytes per cache object + - Uses only 24 extra bytes per cache object ### Getting Started @@ -44,23 +44,27 @@ func main() { ### Benchmarks -A Performance result on keysize=16, cachesize=1000000, parallelism=32 +A Performance result on keysize=16, cachesize=1000000, parallelism=32. Check [actions][actions] for more results and details. ``` goos: linux -goarch: arm64 -pkg: bench +goarch: amd64 +cpu: AMD EPYC 7763 64-Core Processor BenchmarkCloudflareGet -BenchmarkCloudflareGet-4 32207244 191.4 ns/op 16 B/op 1 allocs/op +BenchmarkCloudflareGet-8 43232113 145.9 ns/op 16 B/op 1 allocs/op BenchmarkCcacheGet -BenchmarkCcacheGet-4 32112368 183.4 ns/op 20 B/op 2 allocs/op -BenchmarkRistrettoGet -BenchmarkRistrettoGet-4 39304964 156.9 ns/op 16 B/op 1 allocs/op +BenchmarkCcacheGet-8 48490944 135.8 ns/op 20 B/op 2 allocs/op BenchmarkEcacheGet -BenchmarkEcacheGet-4 35331213 166.2 ns/op 0 B/op 0 allocs/op +BenchmarkEcacheGet-8 51344246 115.7 ns/op 0 B/op 0 allocs/op +BenchmarkRistrettoGet +BenchmarkRistrettoGet-8 56852104 103.4 ns/op 16 B/op 1 allocs/op +BenchmarkTheineGet +BenchmarkTheineGet-8 51490969 108.8 ns/op 0 B/op 0 allocs/op +BenchmarkOtterGet +BenchmarkOtterGet-8 75847165 74.34 ns/op 0 B/op 0 allocs/op BenchmarkPhusluGet -BenchmarkPhusluGet-4 45333045 132.9 ns/op 0 B/op 0 allocs/op +BenchmarkPhusluGet-8 72334320 87.05 ns/op 0 B/op 0 allocs/op PASS -ok bench 42.942s +ok command-line-arguments 58.332s ``` [godoc-img]: http://img.shields.io/badge/godoc-reference-blue.svg @@ -69,3 +73,4 @@ ok bench 42.942s [release]: https://github.com/phuslu/lru/releases [goreport-img]: https://goreportcard.com/badge/github.com/phuslu/lru [goreport]: https://goreportcard.com/report/github.com/phuslu/lru +[actions]: https://github.com/phuslu/lru/actions/workflows/benchmark.yml diff --git a/cache.go b/cache.go index e44f1eb..b293596 100644 --- a/cache.go +++ b/cache.go @@ -69,25 +69,25 @@ func (c *Cache[K, V]) Get(key K) (value V, ok bool) { return c.shards[hash&c.mask].Get(hash, key) } -// Peek returns value for key (if key was in cache), but does not modify its recency. +// Peek returns value for key, but does not modify its recency. func (c *Cache[K, V]) Peek(key K) (value V, ok bool) { hash := c.hash(key) return c.shards[hash&c.mask].Peek(hash, key) } -// Set inserts key value pair and returns evicted value, if cache was full. +// Set inserts key value pair and returns previous value, if cache was full. func (c *Cache[K, V]) Set(key K, value V) (prev V, replaced bool) { hash := c.hash(key) return c.shards[hash&c.mask].Set(hash, c.hash, key, value, 0) } -// SetWithTTL inserts key value pair with ttl and returns evicted value, if cache was full. +// SetWithTTL inserts key value pair with ttl and returns previous value, if cache was full. func (c *Cache[K, V]) SetWithTTL(key K, value V, ttl time.Duration) (prev V, replaced bool) { hash := c.hash(key) return c.shards[hash&c.mask].Set(hash, c.hash, key, value, ttl) } -// Delete method deletes entry associated with key and returns pointer to deleted value (or nil if entry was not in cache). +// Delete method deletes value associated with key and returns deleted value (or empty value if key was not in cache). func (c *Cache[K, V]) Delete(key K) (prev V) { hash := c.hash(key) return c.shards[hash&c.mask].Delete(hash, key)