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:
@ -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"],
|
||||
)
|
||||
|
||||
@ -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.
|
||||
|
||||
26
pkg/util/fastrand/runtime.go
Normal file
26
pkg/util/fastrand/runtime.go
Normal 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
|
||||
26
pkg/util/fastrand/runtime_1.22.go
Normal file
26
pkg/util/fastrand/runtime_1.22.go
Normal 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
|
||||
Reference in New Issue
Block a user