Skip to content

Commit

Permalink
Fix: update calls to runtime.fastrand to use cheaprand for compat…
Browse files Browse the repository at this point in the history
…ibility with Go 1.22

In Golang 1.22, the `runtime.fastrand` method was renamed to `cheaprand`.
This commit updates all occurrences of `runtime.fastrand` in the project
to use the new `cheaprand` method to maintain compatibility with the latest
Golang version and address performance concerns.

Refer: https://gist.github.com/Aoang/3dc06f127f3f54507b7e06b7b6550c28
  • Loading branch information
Aoang committed Mar 11, 2024
1 parent 89a77b0 commit 20b4255
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 16 deletions.
18 changes: 2 additions & 16 deletions internal/xruntime/runtime.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build !go1.22

// Copyright (c) 2023 Alexey Mayshev. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -15,25 +17,9 @@
package xruntime

import (
"runtime"
_ "unsafe"
)

const (
// CacheLineSize is useful for preventing false sharing.
CacheLineSize = 64
)

// Parallelism returns the maximum possible number of concurrently running goroutines.
func Parallelism() uint32 {
maxProcs := uint32(runtime.GOMAXPROCS(0))
numCPU := uint32(runtime.NumCPU())
if maxProcs < numCPU {
return maxProcs
}
return numCPU
}

//go:noescape
//go:linkname Fastrand runtime.fastrand
func Fastrand() uint32
25 changes: 25 additions & 0 deletions internal/xruntime/runtime_1.22.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//go:build go1.22

// Copyright (c) 2023 Alexey Mayshev. All rights reserved.
//
// 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.

package xruntime

import (
_ "unsafe"
)

//go:noescape
//go:linkname Fastrand runtime.cheaprand
func Fastrand() uint32
34 changes: 34 additions & 0 deletions internal/xruntime/xruntime.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright (c) 2023 Alexey Mayshev. All rights reserved.
//
// 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.

package xruntime

import (
"runtime"
)

const (
// CacheLineSize is useful for preventing false sharing.
CacheLineSize = 64
)

// Parallelism returns the maximum possible number of concurrently running goroutines.
func Parallelism() uint32 {
maxProcs := uint32(runtime.GOMAXPROCS(0))
numCPU := uint32(runtime.NumCPU())
if maxProcs < numCPU {
return maxProcs
}
return numCPU
}

0 comments on commit 20b4255

Please sign in to comment.