From 5076ca600832c4884c807ff25f7546505ce11ac4 Mon Sep 17 00:00:00 2001 From: zhangyunhao Date: Thu, 30 May 2024 06:34:20 +0000 Subject: [PATCH] remove linkname for version >= 1.22 --- readme.md | 4 ++++ runtime_go122.go | 17 ++++++++++------- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/readme.md b/readme.md index a0bb79d..073d26f 100644 --- a/readme.md +++ b/readme.md @@ -1,5 +1,9 @@ # fastrand +> As of Go1.22, the fastrand is just a wrapper of `math/rand/v2`, please use the standard library instead. +> +> The fastrand is developed from the background that `math/rand` is unscalable. Because `math/rand` and `math/rand/v2` become scalable since Go1.22, there is no need to use this library. + `fastrand` is the fastest pseudo-random number generator in Go. Support most common APIs of `math/rand`. This generator base on the Go runtime per-M structure, and the init-seed provided by the Go runtime, which means you can't add your seed, but these methods scale very well on multiple cores. diff --git a/runtime_go122.go b/runtime_go122.go index c3c356f..9b6914d 100644 --- a/runtime_go122.go +++ b/runtime_go122.go @@ -4,14 +4,17 @@ package fastrand import ( - _ "unsafe" + "math/rand/v2" ) -//go:linkname runtimefastrand runtime.cheaprand -func runtimefastrand() uint32 +func runtimefastrand() uint32 { + return rand.Uint32() +} -//go:linkname runtimefastrand64 runtime.cheaprand64 -func runtimefastrand64() uint64 +func runtimefastrand64() uint64 { + return rand.Uint64() +} -//go:linkname runtimefastrandu runtime.cheaprandu -func runtimefastrandu() uint +func runtimefastrandu() uint { + return uint(rand.Uint64()) +}