Skip to content

Commit

Permalink
more faster rand
Browse files Browse the repository at this point in the history
  • Loading branch information
phuslu committed Mar 19, 2024
1 parent 62f23e4 commit db790b1
Showing 1 changed file with 59 additions and 41 deletions.
100 changes: 59 additions & 41 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ import (
"crypto/sha1"
"fmt"
"math/rand"
"math/bits"
"os"
"runtime"
"strconv"
Expand Down Expand Up @@ -98,6 +99,20 @@ var threshold = func() uint32 {
return uint32(float64(^uint32(0)) * writeratio)
}()

type CheapRand struct {
Seed uint64
}

func (rand *CheapRand) Uint32() uint32 {
rand.Seed += 0xa0761d6478bd642f
hi, lo := bits.Mul64(rand.Seed, rand.Seed^0xe7037ed1a0b428db)
return uint32(hi ^ lo)
}

func (rand *CheapRand) Uint32n(n uint32) uint32 {
return uint32((uint64(rand.Uint32()) * uint64(n)) >> 32)
}

var zipfian = func() (zipf func() uint64) {
ok, _ := strconv.ParseBool(os.Getenv("zipf"))
if !ok {
Expand All @@ -123,14 +138,6 @@ var keys = func() (x []string) {
return
}()

//go:noescape
//go:linkname cheaprandn runtime.cheaprandn
func cheaprandn(x uint32) uint32

//go:noescape
//go:linkname cheaprand runtime.cheaprand
func cheaprand() uint32

func BenchmarkHashicorpSetGet(b *testing.B) {
c := perfbench.Open(b)
cache := hashicorp.NewLRU[string, int](cachesize, nil, time.Hour)
Expand All @@ -141,13 +148,14 @@ func BenchmarkHashicorpSetGet(b *testing.B) {
b.ResetTimer()
c.Reset()
b.RunParallel(func(pb *testing.PB) {
cheaprand := &CheapRand{uint64(time.Now().UnixNano())}
zipf := zipfian()
for pb.Next() {
if threshold > 0 && cheaprand() <= threshold {
i := int(cheaprandn(cachesize))
if threshold > 0 && cheaprand.Uint32() <= threshold {
i := int(cheaprand.Uint32n(cachesize))
cache.Add(keys[i], i)
} else if zipf == nil {
cache.Get(keys[cheaprandn(cachesize)])
cache.Get(keys[cheaprand.Uint32n(cachesize)])
} else {
cache.Get(keys[zipf()])
}
Expand All @@ -166,13 +174,14 @@ func BenchmarkCloudflareSetGet(b *testing.B) {
b.ResetTimer()
c.Reset()
b.RunParallel(func(pb *testing.PB) {
cheaprand := &CheapRand{uint64(time.Now().UnixNano())}
zipf := zipfian()
for pb.Next() {
if threshold > 0 && cheaprand() <= threshold {
i := int(cheaprandn(cachesize))
if threshold > 0 && cheaprand.Uint32() <= threshold {
i := int(cheaprand.Uint32n(cachesize))
cache.Set(keys[i], i, expires)
} else if zipf == nil {
cache.Get(keys[cheaprandn(cachesize)])
cache.Get(keys[cheaprand.Uint32n(cachesize)])
} else {
cache.Get(keys[zipf()])
}
Expand All @@ -190,13 +199,14 @@ func BenchmarkEcacheSetGet(b *testing.B) {
b.ResetTimer()
c.Reset()
b.RunParallel(func(pb *testing.PB) {
cheaprand := &CheapRand{uint64(time.Now().UnixNano())}
zipf := zipfian()
for pb.Next() {
if threshold > 0 && cheaprand() <= threshold {
i := int(cheaprandn(cachesize))
if threshold > 0 && cheaprand.Uint32() <= threshold {
i := int(cheaprand.Uint32n(cachesize))
cache.Put(keys[i], i)
} else if zipf == nil {
cache.Get(keys[cheaprandn(cachesize)])
cache.Get(keys[cheaprand.Uint32n(cachesize)])
} else {
cache.Get(keys[zipf()])
}
Expand All @@ -218,13 +228,14 @@ func BenchmarkLxzanSetGet(b *testing.B) {
b.ResetTimer()
c.Reset()
b.RunParallel(func(pb *testing.PB) {
cheaprand := &CheapRand{uint64(time.Now().UnixNano())}
zipf := zipfian()
for pb.Next() {
if threshold > 0 && cheaprand() <= threshold {
i := int(cheaprandn(cachesize))
if threshold > 0 && cheaprand.Uint32() <= threshold {
i := int(cheaprand.Uint32n(cachesize))
cache.Set(keys[i], i, time.Hour)
} else if zipf == nil {
cache.Get(keys[cheaprandn(cachesize)])
cache.Get(keys[cheaprand.Uint32n(cachesize)])
} else {
cache.Get(keys[zipf()])
}
Expand All @@ -246,13 +257,14 @@ func BenchmarkFreelruSetGet(b *testing.B) {
b.ResetTimer()
c.Reset()
b.RunParallel(func(pb *testing.PB) {
cheaprand := &CheapRand{uint64(time.Now().UnixNano())}
zipf := zipfian()
for pb.Next() {
if threshold > 0 && cheaprand() <= threshold {
i := int(cheaprandn(cachesize))
if threshold > 0 && cheaprand.Uint32() <= threshold {
i := int(cheaprand.Uint32n(cachesize))
cache.AddWithLifetime(keys[i], i, time.Hour)
} else if zipf == nil {
cache.Get(keys[cheaprandn(cachesize)])
cache.Get(keys[cheaprand.Uint32n(cachesize)])
} else {
cache.Get(keys[zipf()])
}
Expand All @@ -270,13 +282,14 @@ func BenchmarkPhusluSetGet(b *testing.B) {
b.ResetTimer()
c.Reset()
b.RunParallel(func(pb *testing.PB) {
cheaprand := &CheapRand{uint64(time.Now().UnixNano())}
zipf := zipfian()
for pb.Next() {
if threshold > 0 && cheaprand() <= threshold {
i := int(cheaprandn(cachesize))
if threshold > 0 && cheaprand.Uint32() <= threshold {
i := int(cheaprand.Uint32n(cachesize))
cache.Set(keys[i], i, time.Hour)
} else if zipf == nil {
cache.Get(keys[cheaprandn(cachesize)])
cache.Get(keys[cheaprand.Uint32n(cachesize)])
} else {
cache.Get(keys[zipf()])
}
Expand All @@ -294,13 +307,14 @@ func BenchmarkNoTTLSetGet(b *testing.B) {
b.ResetTimer()
c.Reset()
b.RunParallel(func(pb *testing.PB) {
cheaprand := &CheapRand{uint64(time.Now().UnixNano())}
zipf := zipfian()
for pb.Next() {
if threshold > 0 && cheaprand() <= threshold {
i := int(cheaprandn(cachesize))
if threshold > 0 && cheaprand.Uint32() <= threshold {
i := int(cheaprand.Uint32n(cachesize))
cache.Set(keys[i], i)
} else if zipf == nil {
cache.Get(keys[cheaprandn(cachesize)])
cache.Get(keys[cheaprand.Uint32n(cachesize)])
} else {
cache.Get(keys[zipf()])
}
Expand All @@ -318,13 +332,14 @@ func BenchmarkCcacheSetGet(b *testing.B) {
b.ResetTimer()
c.Reset()
b.RunParallel(func(pb *testing.PB) {
cheaprand := &CheapRand{uint64(time.Now().UnixNano())}
zipf := zipfian()
for pb.Next() {
if threshold > 0 && cheaprand() <= threshold {
i := int(cheaprandn(cachesize))
if threshold > 0 && cheaprand.Uint32() <= threshold {
i := int(cheaprand.Uint32n(cachesize))
cache.Set(keys[i], i, time.Hour)
} else if zipf == nil {
cache.Get(keys[cheaprandn(cachesize)])
cache.Get(keys[cheaprand.Uint32n(cachesize)])
} else {
cache.Get(keys[zipf()])
}
Expand All @@ -346,13 +361,14 @@ func BenchmarkRistrettoSetGet(b *testing.B) {
b.ResetTimer()
c.Reset()
b.RunParallel(func(pb *testing.PB) {
cheaprand := &CheapRand{uint64(time.Now().UnixNano())}
zipf := zipfian()
for pb.Next() {
if threshold > 0 && cheaprand() <= threshold {
i := int(cheaprandn(cachesize))
if threshold > 0 && cheaprand.Uint32() <= threshold {
i := int(cheaprand.Uint32n(cachesize))
cache.SetWithTTL(keys[i], i, 1, time.Hour)
} else if zipf == nil {
cache.Get(keys[cheaprandn(cachesize)])
cache.Get(keys[cheaprand.Uint32n(cachesize)])
} else {
cache.Get(keys[zipf()])
}
Expand All @@ -370,13 +386,14 @@ func BenchmarkTheineSetGet(b *testing.B) {
b.ResetTimer()
c.Reset()
b.RunParallel(func(pb *testing.PB) {
cheaprand := &CheapRand{uint64(time.Now().UnixNano())}
zipf := zipfian()
for pb.Next() {
if threshold > 0 && cheaprand() <= threshold {
i := int(cheaprandn(cachesize))
if threshold > 0 && cheaprand.Uint32() <= threshold {
i := int(cheaprand.Uint32n(cachesize))
cache.SetWithTTL(keys[i], i, 1, time.Hour)
} else if zipf == nil {
cache.Get(keys[cheaprandn(cachesize)])
cache.Get(keys[cheaprand.Uint32n(cachesize)])
} else {
cache.Get(keys[zipf()])
}
Expand All @@ -394,13 +411,14 @@ func BenchmarkOtterSetGet(b *testing.B) {
b.ResetTimer()
c.Reset()
b.RunParallel(func(pb *testing.PB) {
cheaprand := &CheapRand{uint64(time.Now().UnixNano())}
zipf := zipfian()
for pb.Next() {
if threshold > 0 && cheaprand() <= threshold {
i := int(cheaprandn(cachesize))
if threshold > 0 && cheaprand.Uint32() <= threshold {
i := int(cheaprand.Uint32n(cachesize))
cache.Set(keys[i], i, time.Hour)
} else if zipf == nil {
cache.Get(keys[cheaprandn(cachesize)])
cache.Get(keys[cheaprand.Uint32n(cachesize)])
} else {
cache.Get(keys[zipf()])
}
Expand Down

0 comments on commit db790b1

Please sign in to comment.