util/rands: add Shuffle and Perm functions with on-stack RNG state

The new math/rand/v2 package includes an m-local global random number
generator that can not be reseeded by the user, which is suitable for
most uses without the RNG pools we have in a number of areas of the code
base.

The new API still does not have an allocation-free way of performing a
seeded operations, due to the long term compiler bug around interface
parameter escapes, and the Source interface.

This change introduces the two APIs that math/rand/v2 can not yet
replace efficiently: seeded Perm() and Shuffle() operations. This
implementation chooses to use the PCG random source from math/rand/v2,
as with sufficient compiler optimization, this source should boil down
to only two on-stack registers for random state under ideal conditions.

Updates #17243

Signed-off-by: James Tucker <james@tailscale.com>
This commit is contained in:
James Tucker
2024-02-06 14:31:31 -08:00
committed by James Tucker
parent 2bb837a9cf
commit 24bac27632
3 changed files with 179 additions and 0 deletions

View File

@@ -519,6 +519,7 @@ tailscale.com/cmd/tailscaled dependencies: (generated by github.com/tailscale/de
math/big from crypto/dsa+
math/bits from compress/flate+
math/rand from github.com/mdlayher/netlink+
math/rand/v2 from tailscale.com/util/rands
mime from github.com/tailscale/xnet/webdav+
mime/multipart from net/http
mime/quotedprintable from mime/multipart