Join GitHub today
GitHub is home to over 20 million developers working together to host and review code, manage projects, and build software together.
proposal: math/rand: speed up Int31n with multiply/shift instead of modulo #16213
Comments
josharian
added
the
Go2
label
Jun 29, 2016
josharian
added this to the Unplanned milestone
Jun 29, 2016
josharian
self-assigned this
Jun 29, 2016
|
If we are going to change this package, it has more severe problems. https://go-review.googlesource.com/#/c/10161/ isn't ready but it is based on a much better generator, and we should use the Go 2 opportunity, should it arise, to replace the core algorithm. |
|
Here's the obvious fastrandn for the runtime package func fastrandn(n uint32) uint32 {
// See http://lemire.me/blog/2016/06/27/a-fast-alternative-to-the-modulo-reduction/.
// Note that we use >> 31 instead of >> 32 because fastrand only produces 31 bit numbers.
return uint32((uint64(n) * uint64(fastrand())) >> 31)
}Unfortunately, the additional function call (instead of an inline Results on amd64, with an assembly implementation: name old time/op new time/op delta Worth doing for the non-constant case. This might get delayed going in, though, since I lack hardware (ppc, mips, s390x) to test asm implementations on other platforms. |
lemire
commented
Sep 9, 2016
|
TensorFlow adopted this technique for better hashing performance : tensorflow/tensorflow@a47a300 |
|
@josharian Well, I could prioritize the replacement package that's already started and we can put it somewhere supported and encourage people to go there instead. |
|
Using this technique in the runtime will be a lot nicer once CL 34782 lands in 1.9. |
gopherbot
commented
Feb 13, 2017
|
CL https://golang.org/cl/36932 mentions this issue. |
pushed a commit
that referenced
this issue
Feb 14, 2017
added a commit
to josharian/go
that referenced
this issue
Feb 14, 2017
rsc
changed the title from
math/rand: speed up Int31n with multiply/shift instead of modulo
to
proposal: math/rand: speed up Int31n with multiply/shift instead of modulo
Jun 17, 2017
gopherbot
added
the
Proposal
label
Jun 17, 2017
gopherbot
commented
Jul 29, 2017
|
Change https://golang.org/cl/51891 mentions this issue: |
added a commit
to josharian/go
that referenced
this issue
Aug 12, 2017
gopherbot
commented
Aug 15, 2017
|
Change https://golang.org/cl/55972 mentions this issue: |
josharian commentedJun 29, 2016
See http://lemire.me/blog/2016/06/27/a-fast-alternative-to-the-modulo-reduction/ for details.
Marking as Go2, since the compatibility guarantee prevents us from changing the output of Int31n given a seed.
We can still use this in the runtime, though; there are several places where we do
fastrand1() % n. I will send a CL for this for Go 1.8 and have it update this issue.cc @dgryski