Skip to content

Commit 46a7587

Browse files
committed
runtime: speed up fastrand() % n
This occurs a fair amount in the runtime for non-power-of-two n. Use an alternative, faster formulation. name old time/op new time/op delta Fastrandn/2-8 4.45ns ± 2% 2.09ns ± 3% -53.12% (p=0.000 n=14+14) Fastrandn/3-8 4.78ns ±11% 2.06ns ± 2% -56.94% (p=0.000 n=15+15) Fastrandn/4-8 4.76ns ± 9% 1.99ns ± 3% -58.28% (p=0.000 n=15+13) Fastrandn/5-8 4.96ns ±13% 2.03ns ± 6% -59.14% (p=0.000 n=15+15) name old time/op new time/op delta SelectUncontended-8 33.7ns ± 2% 33.9ns ± 2% +0.70% (p=0.000 n=49+50) SelectSyncContended-8 1.68µs ± 4% 1.65µs ± 4% -1.54% (p=0.000 n=50+45) SelectAsyncContended-8 282ns ± 1% 277ns ± 1% -1.50% (p=0.000 n=48+43) SelectNonblock-8 5.31ns ± 1% 5.32ns ± 1% ~ (p=0.275 n=45+44) SelectProdCons-8 585ns ± 3% 577ns ± 2% -1.35% (p=0.000 n=50+50) GoroutineSelect-8 1.59ms ± 2% 1.59ms ± 1% ~ (p=0.084 n=49+48) Updates #16213 Change-Id: Ib555a4d7da2042a25c3976f76a436b536487d5b7 Reviewed-on: https://go-review.googlesource.com/36932 Run-TryBot: Josh Bleecher Snyder <[email protected]> Reviewed-by: Brad Fitzpatrick <[email protected]> Reviewed-by: Keith Randall <[email protected]> TryBot-Result: Gobot Gobot <[email protected]>
1 parent 83c58ac commit 46a7587

File tree

7 files changed

+26
-5
lines changed

7 files changed

+26
-5
lines changed

src/runtime/export_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -246,4 +246,5 @@ func CountPagesInUse() (pagesInUse, counted uintptr) {
246246
return
247247
}
248248

249-
func Fastrand() uint32 { return fastrand() }
249+
func Fastrand() uint32 { return fastrand() }
250+
func Fastrandn(n uint32) uint32 { return fastrandn(n) }

src/runtime/mgc.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -648,7 +648,7 @@ func (c *gcControllerState) enlistWorker() {
648648
}
649649
myID := gp.m.p.ptr().id
650650
for tries := 0; tries < 5; tries++ {
651-
id := int32(fastrand() % uint32(gomaxprocs-1))
651+
id := int32(fastrandn(uint32(gomaxprocs - 1)))
652652
if id >= myID {
653653
id++
654654
}

src/runtime/proc.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -4280,7 +4280,7 @@ func runqputslow(_p_ *p, gp *g, h, t uint32) bool {
42804280

42814281
if randomizeScheduler {
42824282
for i := uint32(1); i <= n; i++ {
4283-
j := fastrand() % (i + 1)
4283+
j := fastrandn(i + 1)
42844284
batch[i], batch[j] = batch[j], batch[i]
42854285
}
42864286
}

src/runtime/rand_test.go

+13
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package runtime_test
66

77
import (
88
. "runtime"
9+
"strconv"
910
"testing"
1011
)
1112

@@ -30,3 +31,15 @@ func BenchmarkFastrandHashiter(b *testing.B) {
3031
}
3132
})
3233
}
34+
35+
var sink32 uint32
36+
37+
func BenchmarkFastrandn(b *testing.B) {
38+
for n := uint32(2); n <= 5; n++ {
39+
b.Run(strconv.Itoa(int(n)), func(b *testing.B) {
40+
for i := 0; i < b.N; i++ {
41+
sink32 = Fastrandn(n)
42+
}
43+
})
44+
}
45+
}

src/runtime/select.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ func selectgoImpl(sel *hselect) (uintptr, uint16) {
270270
pollslice := slice{unsafe.Pointer(sel.pollorder), int(sel.ncase), int(sel.ncase)}
271271
pollorder := *(*[]uint16)(unsafe.Pointer(&pollslice))
272272
for i := 1; i < int(sel.ncase); i++ {
273-
j := fastrand() % uint32(i+1)
273+
j := fastrandn(uint32(i + 1))
274274
pollorder[i] = pollorder[j]
275275
pollorder[j] = uint16(i)
276276
}

src/runtime/stubs.go

+7
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,13 @@ func fastrand() uint32 {
103103
return fr
104104
}
105105

106+
//go:nosplit
107+
func fastrandn(n uint32) uint32 {
108+
// This is similar to fastrand() % n, but faster.
109+
// See http://lemire.me/blog/2016/06/27/a-fast-alternative-to-the-modulo-reduction/
110+
return uint32(uint64(fastrand()) * uint64(n) >> 32)
111+
}
112+
106113
//go:linkname sync_fastrand sync.fastrand
107114
func sync_fastrand() uint32 { return fastrand() }
108115

src/runtime/symtab.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -549,7 +549,7 @@ func pcvalue(f *_func, off int32, targetpc uintptr, cache *pcvalueCache, strict
549549
// a recursive stack's cycle is slightly
550550
// larger than the cache.
551551
if cache != nil {
552-
ci := fastrand() % uint32(len(cache.entries))
552+
ci := fastrandn(uint32(len(cache.entries)))
553553
cache.entries[ci] = pcvalueCacheEnt{
554554
targetpc: targetpc,
555555
off: off,

0 commit comments

Comments
 (0)