fastrand icon indicating copy to clipboard operation
fastrand copied to clipboard

Is this per CPU or per goroutine?

Open qdm12 opened this issue 3 years ago • 0 comments

Hello, first thanks for that small code, it's interesting!

I'm trying to solve this with it, for the fun of it.

Now regarding the sync.Pool, I'm not sure this is so much per CPU than it is per goroutine.

If you have 1000 goroutines at the same time, there will be 1000 RNG objects (well just 1000x 1 uint32 really so that's fine memory wise).

Indeed if you have 4 CPU intensive goroutines on a 4 core CPU, there will be one RNG per core but that's a particular case.

Should we mention that in the readme?

There is also another problem: if you launch 1000 goroutines at start, it 'could' create 1000 RNG initialized with the same time (unlikely, but it can happen on certain systems with bad time resolution) and that can lead to a quite bad pseudo randomness.

Maybe a solution would be to use crypto/rand the first time when creating the RNG object via the sync.Pool's New field function?

qdm12 avatar Mar 21 '21 23:03 qdm12