Skip to content

Commit

Permalink
Migrate math/rand to math/rand/v2
Browse files Browse the repository at this point in the history
Signed-off-by: Erik Westra <[email protected]>
  • Loading branch information
webstradev committed Mar 2, 2025
1 parent c9fc4b8 commit 8cb200b
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 52 deletions.
28 changes: 0 additions & 28 deletions benchmark_results.txt

This file was deleted.

6 changes: 4 additions & 2 deletions ulid.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
"io"
"math"
"math/bits"
"math/rand"
"math/rand/v2"
"sync"
"time"
)
Expand Down Expand Up @@ -133,7 +133,9 @@ func MustNewDefault(t time.Time) ULID {
}

var defaultEntropy = func() io.Reader {
rng := rand.New(rand.NewSource(time.Now().UnixNano()))
seed := [32]byte{}
binary.LittleEndian.PutUint64(seed[:8], uint64(time.Now().UnixNano()))
rng := rand.NewChaCha8(seed)
return &LockedMonotonicReader{MonotonicReader: Monotonic(rng, 0)}
}()

Expand Down
40 changes: 18 additions & 22 deletions ulid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ import (
"fmt"
"io"
"math"
"math/rand"
randV2 "math/rand/v2"
"math/rand/v2"
"strings"
"testing"
"testing/iotest"
Expand All @@ -33,9 +32,11 @@ import (

func ExampleULID() {
t := time.Unix(1000000, 0)
entropy := ulid.Monotonic(rand.New(rand.NewSource(t.UnixNano())), 0)
rng := createChaCha8RNG(t)

entropy := ulid.Monotonic(rng, 0)
fmt.Println(ulid.MustNew(ulid.Timestamp(t), entropy))
// Output: 0000XSNJG0MQJHBF4QX1EFD6Y3
// Output: 0000XSNJG02VZD8JHMTREXTNAH
}

func TestNew(t *testing.T) {
Expand Down Expand Up @@ -420,9 +421,8 @@ func TestULIDTime(t *testing.T) {
t.Errorf("got err %v, want %v", got, want)
}

rng := rand.New(rand.NewSource(time.Now().UnixNano()))
for i := 0; i < 1e6; i++ {
ms := uint64(rng.Int63n(int64(maxTime)))
ms := uint64(rand.Int64N(int64(maxTime)))

var id ulid.ULID
if err := id.SetTime(ms); err != nil {
Expand Down Expand Up @@ -589,13 +589,12 @@ func TestScan(t *testing.T) {
}

func TestMonotonic(t *testing.T) {
now := ulid.Now()
for _, e := range []struct {
name string
mk func() io.Reader
}{
{"cryptorand", func() io.Reader { return crand.Reader }},
{"mathrand", func() io.Reader { return rand.New(rand.NewSource(int64(now))) }},
{"mathrand", func() io.Reader { return createChaCha8RNG(time.Now()) }},
} {
for _, inc := range []uint64{
0,
Expand Down Expand Up @@ -657,7 +656,7 @@ func TestMonotonicSafe(t *testing.T) {
t.Parallel()

var (
rng = rand.New(rand.NewSource(time.Now().UnixNano()))
rng = createChaCha8RNG(time.Now())
safe = &ulid.LockedMonotonicReader{MonotonicReader: ulid.Monotonic(rng, 0)}
t0 = ulid.Timestamp(time.Now())
)
Expand Down Expand Up @@ -691,7 +690,7 @@ func TestMonotonicSafe(t *testing.T) {

func TestULID_Bytes(t *testing.T) {
tt := time.Unix(1000000, 0)
entropy := ulid.Monotonic(rand.New(rand.NewSource(tt.UnixNano())), 0)
entropy := ulid.Monotonic(createChaCha8RNG(tt), 0)
id := ulid.MustNew(ulid.Timestamp(tt), entropy)
bid := id.Bytes()
bid[len(bid)-1]++
Expand All @@ -700,6 +699,12 @@ func TestULID_Bytes(t *testing.T) {
}
}

func createChaCha8RNG(t time.Time) *rand.ChaCha8 {
seed := [32]byte{}
binary.LittleEndian.PutUint64(seed[:8], uint64(t.UnixNano()))
return rand.NewChaCha8(seed)
}

func BenchmarkNew(b *testing.B) {
benchmarkMakeULID(b, func(timestamp uint64, entropy io.Reader) {
_, _ = ulid.New(timestamp, entropy)
Expand All @@ -716,11 +721,7 @@ func benchmarkMakeULID(b *testing.B, f func(uint64, io.Reader)) {
b.ReportAllocs()
b.SetBytes(int64(len(ulid.ULID{})))

nanoNow := time.Now().UnixNano()
rng := rand.New(rand.NewSource(nanoNow))
seed := [32]byte{}
binary.BigEndian.PutUint64(seed[:8], uint64(nanoNow))
chachaRng := randV2.NewChaCha8(seed)
rng := createChaCha8RNG(time.Now())

for _, tc := range []struct {
name string
Expand All @@ -729,15 +730,10 @@ func benchmarkMakeULID(b *testing.B, f func(uint64, io.Reader)) {
}{
{"WithCrypoEntropy", []uint64{123}, crand.Reader},
{"WithEntropy", []uint64{123}, rng},
{"WithChaChaEntropy", []uint64{123}, chachaRng},
{"WithMonotonicEntropy_SameTimestamp_Inc0", []uint64{123}, ulid.Monotonic(rng, 0)},
{"WithMonotonicEntropy_SameTimestamp_Inc0_ChaCha", []uint64{123}, ulid.Monotonic(chachaRng, 0)},
{"WithMonotonicEntropy_DifferentTimestamp_Inc0", []uint64{122, 123}, ulid.Monotonic(rng, 0)},
{"WithMonotonicEntropy_DifferentTimestamp_Inc0_ChaCha", []uint64{122, 123}, ulid.Monotonic(chachaRng, 0)},
{"WithMonotonicEntropy_SameTimestamp_Inc1", []uint64{123}, ulid.Monotonic(rng, 1)},
{"WithMonotonicEntropy_SameTimestamp_Inc1_ChaCha", []uint64{123}, ulid.Monotonic(chachaRng, 1)},
{"WithMonotonicEntropy_DifferentTimestamp_Inc1", []uint64{122, 123}, ulid.Monotonic(rng, 1)},
{"WithMonotonicEntropy_DifferentTimestamp_Inc1_ChaCha", []uint64{122, 123}, ulid.Monotonic(chachaRng, 1)},
{"WithCryptoMonotonicEntropy_SameTimestamp_Inc1", []uint64{123}, ulid.Monotonic(crand.Reader, 1)},
{"WithCryptoMonotonicEntropy_DifferentTimestamp_Inc1", []uint64{122, 123}, ulid.Monotonic(crand.Reader, 1)},
{"WithoutEntropy", []uint64{123}, nil},
Expand Down Expand Up @@ -779,7 +775,7 @@ func BenchmarkMustParse(b *testing.B) {
}

func BenchmarkString(b *testing.B) {
entropy := rand.New(rand.NewSource(time.Now().UnixNano()))
entropy := createChaCha8RNG(time.Now())
id := ulid.MustNew(123456, entropy)
b.SetBytes(int64(len(id)))
b.ResetTimer()
Expand All @@ -789,7 +785,7 @@ func BenchmarkString(b *testing.B) {
}

func BenchmarkMarshal(b *testing.B) {
entropy := rand.New(rand.NewSource(time.Now().UnixNano()))
entropy := createChaCha8RNG(time.Now())
buf := make([]byte, ulid.EncodedSize)
id := ulid.MustNew(123456, entropy)

Expand Down

0 comments on commit 8cb200b

Please sign in to comment.