pkg: fix update calls to runtime.fastrand to use cheaprand for compatibility with Go 1.22 (#51672)

ref pingcap/tidb#50765
This commit is contained in:
Aoang
2024-03-12 23:48:39 +08:00
committed by GitHub
parent 05a63f73a2
commit cd6dbc2af9
4 changed files with 57 additions and 7 deletions

View File

@ -2,7 +2,11 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
go_library(
name = "fastrand",
srcs = ["random.go"],
srcs = [
"random.go",
"runtime.go",
"runtime_1.22.go",
],
importpath = "github.com/pingcap/tidb/pkg/util/fastrand",
visibility = ["//visibility:public"],
)

View File

@ -16,7 +16,6 @@ package fastrand
import (
"math/bits"
_ "unsafe" // required by go:linkname
)
// wyrand is a fast PRNG. See https://github.com/wangyi-fudan/wyhash
@ -48,11 +47,6 @@ func Buf(size int) []byte {
return buf
}
// Uint32 returns a lock free uint32 value.
//
//go:linkname Uint32 runtime.fastrand
func Uint32() uint32
// Uint32N returns, as an uint32, a pseudo-random number in [0,n).
func Uint32N(n uint32) uint32 {
// This is similar to Uint32() % n, but faster.

View File

@ -0,0 +1,26 @@
// Copyright 2024 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//go:build !go1.22
package fastrand
import (
_ "unsafe" // required by go:linkname
)
// Uint32 returns a lock free uint32 value.
//
//go:linkname Uint32 runtime.fastrand
func Uint32() uint32

View File

@ -0,0 +1,26 @@
// Copyright 2024 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//go:build go1.22
package fastrand
import (
_ "unsafe" // required by go:linkname
)
// Uint32 returns a lock free uint32 value.
//
//go:linkname Uint32 runtime.cheaprand
func Uint32() uint32