Skip to content

Commit

Permalink
replacing math/rand with math/rand/v2 (#27)
Browse files Browse the repository at this point in the history
There is no reason to use math/rand anymore. Also removed all of the `//nolint:gosec` comments throughout the code.
  • Loading branch information
dangermike authored May 9, 2024
1 parent 72f841e commit 80ff430
Show file tree
Hide file tree
Showing 12 changed files with 38 additions and 54 deletions.
10 changes: 5 additions & 5 deletions bench/lazylru_benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package main_test

import (
"fmt"
"math/rand"
"math/rand/v2"
"runtime"
"strconv"
"testing"
Expand Down Expand Up @@ -48,8 +48,8 @@ func (bc benchconfig) Generic(b *testing.B) {
runtime.GC()
b.ResetTimer()
for i := 0; i < b.N; i++ {
ix := rand.Intn(bc.keyCount) //nolint:gosec
if rand.Float64() < bc.readRate { //nolint:gosec
ix := rand.IntN(bc.keyCount)
if rand.Float64() < bc.readRate {
lru.Get(keys[ix])
} else {
lru.Set(keys[ix], ix)
Expand All @@ -67,8 +67,8 @@ func (bc benchconfig) GenInterface(b *testing.B) {
runtime.GC()
b.ResetTimer()
for i := 0; i < b.N; i++ {
ix := rand.Intn(bc.keyCount) //nolint:gosec
if rand.Float64() < bc.readRate { //nolint:gosec
ix := rand.IntN(bc.keyCount)
if rand.Float64() < bc.readRate {
lru.Get(keys[ix])
} else {
lru.Set(keys[ix], ix)
Expand Down
6 changes: 3 additions & 3 deletions bench/test_data.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package main

import (
"math/rand"
"math/rand/v2"
)

const validChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"

func randomString(size int) string {
retval := make([]byte, size)
for i := 0; i < len(retval); i++ {
retval[i] = validChars[rand.Intn(len(validChars))] //nolint:gosec
retval[i] = validChars[rand.IntN(len(validChars))]
}
return string(retval)
}
Expand Down Expand Up @@ -40,7 +40,7 @@ func NewTestData(count int) TestDataSimple {

// RandomKV retrieves a random key/value pair
func (td TestDataSimple) RandomKV() (string, string) {
kv := td[rand.Intn(len(td))] //nolint: gosec
kv := td[rand.IntN(len(td))]
return kv.Key, kv.Value
}

Expand Down
4 changes: 2 additions & 2 deletions containers/heap/heap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
package heap

import (
"math/rand"
"math/rand/v2"
"testing"
)

Expand Down Expand Up @@ -202,7 +202,7 @@ func TestFix(t *testing.T) {
h.verify(t, 0)

for i := 100; i > 0; i-- {
elem := rand.Intn(h.Len()) //nolint:gosec
elem := rand.IntN(h.Len())
if i&1 == 0 {
(*h)[elem] *= 2
} else {
Expand Down
4 changes: 2 additions & 2 deletions generic/containers/heap/heap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
package heap

import (
"math/rand"
"math/rand/v2"
"testing"
)

Expand Down Expand Up @@ -202,7 +202,7 @@ func TestFix(t *testing.T) {
h.verify(t, 0)

for i := 100; i > 0; i-- {
elem := rand.Intn(h.Len()) //nolint:gosec
elem := rand.IntN(h.Len())
if i&1 == 0 {
(*h)[elem] *= 2
} else {
Expand Down
4 changes: 2 additions & 2 deletions generic/lazylru.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package lazylru

import (
"errors"
"math/rand"
"math/rand/v2"
"sync"
"sync/atomic"
"time"
Expand Down Expand Up @@ -176,7 +176,7 @@ func (lru *LazyLRU[K, V]) reap(start int, deathList []*item[K, V]) {
break
}
if start < 0 {
start = rand.Intn(len(lru.items)) //nolint:gosec
start = rand.IntN(len(lru.items))
}
end := start + 100 // why 100? no idea
if end > len(lru.items) {
Expand Down
20 changes: 7 additions & 13 deletions generic/lazylru_benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package lazylru_test

import (
"fmt"
"math/rand"
"math/rand/v2"
"runtime"
"strconv"
"testing"
Expand Down Expand Up @@ -61,9 +61,8 @@ func (bc benchconfig) InterfaceArray(b *testing.B) {
runtime.GC()
b.ResetTimer()
for i := 0; i < b.N; i++ {
// ix := rand.Intn(bc.keyCount) //nolint:gosec
ix := i % bc.keyCount
if rand.Float64() < bc.readRate { //nolint:gosec
if rand.Float64() < bc.readRate {
// if true {
if iv, ok := lru.Get(keys[ix]); !ok {
continue
Expand Down Expand Up @@ -91,9 +90,8 @@ func (bc benchconfig) GenericArray(b *testing.B) {
runtime.GC()
b.ResetTimer()
for i := 0; i < b.N; i++ {
// ix := rand.Intn(bc.keyCount) //nolint:gosec
ix := i % bc.keyCount
if rand.Float64() < bc.readRate { //nolint:gosec
if rand.Float64() < bc.readRate {
// if true {
if v, ok := lru.Get(keys[ix]); !ok {
continue
Expand All @@ -115,9 +113,8 @@ func (bc benchconfig) InterfaceStructPtr(b *testing.B) {
runtime.GC()
b.ResetTimer()
for i := 0; i < b.N; i++ {
// ix := rand.Intn(bc.keyCount) //nolint:gosec
ix := i % bc.keyCount
if rand.Float64() < bc.readRate { //nolint:gosec
if rand.Float64() < bc.readRate {
// if true {
if iv, ok := lru.Get(keys[ix]); !ok {
continue
Expand Down Expand Up @@ -145,9 +142,8 @@ func (bc benchconfig) GenericStructPtr(b *testing.B) {
runtime.GC()
b.ResetTimer()
for i := 0; i < b.N; i++ {
// ix := rand.Intn(bc.keyCount) //nolint:gosec
ix := i % bc.keyCount
if rand.Float64() < bc.readRate { //nolint:gosec
if rand.Float64() < bc.readRate {
// if true {
if v, ok := lru.Get(keys[ix]); !ok {
continue
Expand All @@ -169,9 +165,8 @@ func (bc benchconfig) InterfaceValue(b *testing.B) {
runtime.GC()
b.ResetTimer()
for i := 0; i < b.N; i++ {
// ix := rand.Intn(bc.keyCount) //nolint:gosec
ix := i % bc.keyCount
if rand.Float64() < bc.readRate { //nolint:gosec
if rand.Float64() < bc.readRate {
// if true {
if iv, ok := lru.Get(keys[ix]); !ok {
continue
Expand Down Expand Up @@ -199,9 +194,8 @@ func (bc benchconfig) GenericValue(b *testing.B) {
runtime.GC()
b.ResetTimer()
for i := 0; i < b.N; i++ {
// ix := rand.Intn(bc.keyCount) //nolint:gosec
ix := i % bc.keyCount
if rand.Float64() < bc.readRate { //nolint:gosec
if rand.Float64() < bc.readRate {
// if true {
if v, ok := lru.Get(keys[ix]); !ok {
continue
Expand Down
8 changes: 3 additions & 5 deletions generic/sharded/lazylru_benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package sharded_test

import (
"fmt"
"math/rand"
"math/rand/v2"
"runtime"
"strconv"
"sync"
Expand Down Expand Up @@ -66,14 +66,12 @@ func (bc benchconfig) Run(b *testing.B) {
b.ResetTimer()
var wg sync.WaitGroup
wg.Add(bc.concurrency)
baseTime := time.Now().UnixNano()

for c := 0; c < bc.concurrency; c++ {
go func(c int) {
rnd := rand.New(rand.NewSource(baseTime + int64(c))) //nolint:gosec
for i := c; i < b.N; i += bc.concurrency {
ix := rnd.Intn(bc.keyCount)
if rnd.Float64() < bc.readRate {
ix := rand.IntN(bc.keyCount)
if rand.Float64() < bc.readRate {
lru.Get(keys[ix])
} else {
lru.Set(keys[ix], ix)
Expand Down
2 changes: 1 addition & 1 deletion generic/sharded/sharder_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package sharded_test

import (
"math/rand"
"math/rand/v2"
"sort"
"strconv"
"testing"
Expand Down
4 changes: 2 additions & 2 deletions lazylru.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package lazylru

import (
"errors"
"math/rand"
"math/rand/v2"
"sync"
"sync/atomic"
"time"
Expand Down Expand Up @@ -152,7 +152,7 @@ func (lru *LazyLRU[K, V]) reap(start int, deathList []*item[K, V]) {
break
}
if start < 0 {
start = rand.Intn(len(lru.items)) //nolint:gosec
start = rand.IntN(len(lru.items))
}
end := start + 100 // why 100? no idea
if end > len(lru.items) {
Expand Down
20 changes: 7 additions & 13 deletions lazylru_benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package lazylru_test

import (
"fmt"
"math/rand"
"math/rand/v2"
"runtime"
"strconv"
"testing"
Expand Down Expand Up @@ -61,9 +61,8 @@ func (bc benchconfig) InterfaceArray(b *testing.B) {
runtime.GC()
b.ResetTimer()
for i := 0; i < b.N; i++ {
// ix := rand.Intn(bc.keyCount) //nolint:gosec
ix := i % bc.keyCount
if rand.Float64() < bc.readRate { //nolint:gosec
if rand.Float64() < bc.readRate {
// if true {
if iv, ok := lru.Get(keys[ix]); !ok {
continue
Expand Down Expand Up @@ -91,9 +90,8 @@ func (bc benchconfig) GenericArray(b *testing.B) {
runtime.GC()
b.ResetTimer()
for i := 0; i < b.N; i++ {
// ix := rand.Intn(bc.keyCount) //nolint:gosec
ix := i % bc.keyCount
if rand.Float64() < bc.readRate { //nolint:gosec
if rand.Float64() < bc.readRate {
// if true {
if v, ok := lru.Get(keys[ix]); !ok {
continue
Expand All @@ -115,9 +113,8 @@ func (bc benchconfig) InterfaceStructPtr(b *testing.B) {
runtime.GC()
b.ResetTimer()
for i := 0; i < b.N; i++ {
// ix := rand.Intn(bc.keyCount) //nolint:gosec
ix := i % bc.keyCount
if rand.Float64() < bc.readRate { //nolint:gosec
if rand.Float64() < bc.readRate {
// if true {
if iv, ok := lru.Get(keys[ix]); !ok {
continue
Expand Down Expand Up @@ -145,9 +142,8 @@ func (bc benchconfig) GenericStructPtr(b *testing.B) {
runtime.GC()
b.ResetTimer()
for i := 0; i < b.N; i++ {
// ix := rand.Intn(bc.keyCount) //nolint:gosec
ix := i % bc.keyCount
if rand.Float64() < bc.readRate { //nolint:gosec
if rand.Float64() < bc.readRate {
// if true {
if v, ok := lru.Get(keys[ix]); !ok {
continue
Expand All @@ -169,9 +165,8 @@ func (bc benchconfig) InterfaceValue(b *testing.B) {
runtime.GC()
b.ResetTimer()
for i := 0; i < b.N; i++ {
// ix := rand.Intn(bc.keyCount) //nolint:gosec
ix := i % bc.keyCount
if rand.Float64() < bc.readRate { //nolint:gosec
if rand.Float64() < bc.readRate {
// if true {
if iv, ok := lru.Get(keys[ix]); !ok {
continue
Expand Down Expand Up @@ -199,9 +194,8 @@ func (bc benchconfig) GenericValue(b *testing.B) {
runtime.GC()
b.ResetTimer()
for i := 0; i < b.N; i++ {
// ix := rand.Intn(bc.keyCount) //nolint:gosec
ix := i % bc.keyCount
if rand.Float64() < bc.readRate { //nolint:gosec
if rand.Float64() < bc.readRate {
// if true {
if v, ok := lru.Get(keys[ix]); !ok {
continue
Expand Down
8 changes: 3 additions & 5 deletions sharded/lazylru_benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package sharded_test

import (
"fmt"
"math/rand"
"math/rand/v2"
"runtime"
"strconv"
"sync"
Expand Down Expand Up @@ -66,14 +66,12 @@ func (bc benchconfig) Run(b *testing.B) {
b.ResetTimer()
var wg sync.WaitGroup
wg.Add(bc.concurrency)
baseTime := time.Now().UnixNano()

for c := 0; c < bc.concurrency; c++ {
go func(c int) {
rnd := rand.New(rand.NewSource(baseTime + int64(c))) //nolint:gosec
for i := c; i < b.N; i += bc.concurrency {
ix := rnd.Intn(bc.keyCount)
if rnd.Float64() < bc.readRate {
ix := rand.IntN(bc.keyCount)
if rand.Float64() < bc.readRate {
lru.Get(keys[ix])
} else {
lru.Set(keys[ix], ix)
Expand Down
2 changes: 1 addition & 1 deletion sharded/sharder_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package sharded_test

import (
"math/rand"
"math/rand/v2"
"sort"
"strconv"
"testing"
Expand Down

0 comments on commit 80ff430

Please sign in to comment.