bloblru: Upgrade to hashicorp/golang-lru/v2

The new genericized LRU cache no longer needs to have the IDs separately
allocated:

name   old time/op    new time/op    delta
Add-8     494ns ± 2%     388ns ± 2%  -21.46%  (p=0.000 n=10+9)

name   old alloc/op   new alloc/op   delta
Add-8      176B ± 0%      152B ± 0%  -13.64%  (p=0.000 n=10+10)

name   old allocs/op  new allocs/op  delta
Add-8      5.00 ± 0%      3.00 ± 0%  -40.00%  (p=0.000 n=10+10)
This commit is contained in:
greatroar 2022-11-27 09:58:19 +01:00
parent 05cebc1c4b
commit e5d597fd22
4 changed files with 38 additions and 16 deletions

View file

@ -1,6 +1,7 @@
package bloblru
import (
"math/rand"
"testing"
"github.com/restic/restic/internal/restic"
@ -50,3 +51,29 @@ func TestCache(t *testing.T) {
rtest.Equals(t, cacheSize, c.size)
rtest.Equals(t, cacheSize, c.free)
}
func BenchmarkAdd(b *testing.B) {
const (
MiB = 1 << 20
nblobs = 64
)
c := New(64 * MiB)
buf := make([]byte, 8*MiB)
ids := make([]restic.ID, nblobs)
sizes := make([]int, nblobs)
r := rand.New(rand.NewSource(100))
for i := range ids {
r.Read(ids[i][:])
sizes[i] = r.Intn(8 * MiB)
}
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
c.Add(ids[i%nblobs], buf[:sizes[i%nblobs]])
}
}