-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reimplement pkg/random on top of math/rand/v2
Now that math/rand no longer has a global Seed() method, it's become safe for us to let FastThreadSafeGenerator call into
- Loading branch information
1 parent
8abc648
commit b818f9d
Showing
6 changed files
with
47 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,27 @@ | ||
package random | ||
|
||
import ( | ||
"math/rand" | ||
"math/rand/v2" | ||
) | ||
|
||
type fastSingleThreadedGenerator struct { | ||
*rand.Rand | ||
} | ||
|
||
// NewFastSingleThreadedGenerator creates a new SingleThreadedGenerator | ||
// that is not suitable for cryptographic purposes. The generator is | ||
// randomly seeded. | ||
func NewFastSingleThreadedGenerator() SingleThreadedGenerator { | ||
return rand.New( | ||
rand.NewSource( | ||
int64(CryptoThreadSafeGenerator.Uint64()))) | ||
return fastSingleThreadedGenerator{ | ||
Rand: rand.New( | ||
rand.NewPCG( | ||
CryptoThreadSafeGenerator.Uint64(), | ||
CryptoThreadSafeGenerator.Uint64(), | ||
), | ||
), | ||
} | ||
} | ||
|
||
func (fastSingleThreadedGenerator) Read(p []byte) (int, error) { | ||
return mustCryptoRandRead(p) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,39 @@ | ||
package random | ||
|
||
import ( | ||
"sync" | ||
"math/rand/v2" | ||
) | ||
|
||
type fastThreadSafeGenerator struct { | ||
lock sync.Mutex | ||
generator SingleThreadedGenerator | ||
} | ||
|
||
func (g *fastThreadSafeGenerator) IsThreadSafe() {} | ||
func (fastThreadSafeGenerator) IsThreadSafe() {} | ||
|
||
func (g *fastThreadSafeGenerator) Float64() float64 { | ||
g.lock.Lock() | ||
defer g.lock.Unlock() | ||
return g.generator.Float64() | ||
func (fastThreadSafeGenerator) Float64() float64 { | ||
return rand.Float64() | ||
} | ||
|
||
func (g *fastThreadSafeGenerator) Int63n(n int64) int64 { | ||
g.lock.Lock() | ||
defer g.lock.Unlock() | ||
return g.generator.Int63n(n) | ||
func (fastThreadSafeGenerator) Int64N(n int64) int64 { | ||
return rand.Int64N(n) | ||
} | ||
|
||
func (g *fastThreadSafeGenerator) Intn(n int) int { | ||
g.lock.Lock() | ||
defer g.lock.Unlock() | ||
return g.generator.Intn(n) | ||
func (fastThreadSafeGenerator) IntN(n int) int { | ||
return rand.IntN(n) | ||
} | ||
|
||
func (g *fastThreadSafeGenerator) Read(p []byte) (int, error) { | ||
g.lock.Lock() | ||
defer g.lock.Unlock() | ||
return g.generator.Read(p) | ||
func (fastThreadSafeGenerator) Read(p []byte) (int, error) { | ||
return mustCryptoRandRead(p) | ||
} | ||
|
||
func (g *fastThreadSafeGenerator) Shuffle(n int, swap func(i, j int)) { | ||
g.lock.Lock() | ||
defer g.lock.Unlock() | ||
g.generator.Shuffle(n, swap) | ||
func (fastThreadSafeGenerator) Shuffle(n int, swap func(i, j int)) { | ||
rand.Shuffle(n, swap) | ||
} | ||
|
||
func (g *fastThreadSafeGenerator) Uint64() uint64 { | ||
g.lock.Lock() | ||
defer g.lock.Unlock() | ||
return g.generator.Uint64() | ||
func (fastThreadSafeGenerator) Uint64() uint64 { | ||
return rand.Uint64() | ||
} | ||
|
||
// FastThreadSafeGenerator is an instance of ThreadSafeGenerator that is | ||
// not suitable for cryptographic purposes. The generator is randomly | ||
// seeded on startup. | ||
var FastThreadSafeGenerator ThreadSafeGenerator = &fastThreadSafeGenerator{ | ||
generator: NewFastSingleThreadedGenerator(), | ||
} | ||
var FastThreadSafeGenerator ThreadSafeGenerator = fastThreadSafeGenerator{} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters