Skip to content

Commit

Permalink
Use math/rand/v2.Uint32N instead of runtime.fastrand in v1.22
Browse files Browse the repository at this point in the history
  • Loading branch information
Aoang committed Mar 12, 2024
1 parent c0064a0 commit 5a0b0fc
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 11 deletions.
3 changes: 0 additions & 3 deletions bits.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,3 @@ func hasZeroByte(x uint64) bitset {
func castUint64(m *metadata) uint64 {
return *(*uint64)((unsafe.Pointer)(m))
}

//go:linkname fastrand runtime.fastrand
func fastrand() uint32
3 changes: 0 additions & 3 deletions bits_amd64.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,3 @@ func nextMatch(b *bitset) (s uint32) {
*b &= ^(1 << s) // clear bit |s|
return
}

//go:linkname fastrand runtime.fastrand
func fastrand() uint32
29 changes: 29 additions & 0 deletions fastrand.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright 2023 Dolthub, 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 swiss

import (
_ "unsafe"
)

//go:linkname fastrand runtime.fastrand
func fastrand() uint32

// randIntN returns a random number in the interval [0, n).
func randIntN(n int) uint32 {
return fastModN(fastrand(), uint32(n))
}
26 changes: 26 additions & 0 deletions fastrand_1.22.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright 2023 Dolthub, 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 swiss

import (
"math/rand/v2"
)

// randIntN returns a random number in the interval [0, n).
func randIntN(n int) uint32 {
return rand.Uint32N(uint32(n))
}
5 changes: 0 additions & 5 deletions map.go
Original file line number Diff line number Diff line change
Expand Up @@ -352,8 +352,3 @@ func probeStart(hi h1, groups int) uint32 {
func fastModN(x, n uint32) uint32 {
return uint32((uint64(x) * uint64(n)) >> 32)
}

// randIntN returns a random number in the interval [0, n).
func randIntN(n int) uint32 {
return fastModN(fastrand(), uint32(n))
}

0 comments on commit 5a0b0fc

Please sign in to comment.