]> Cypherpunks repositories - gostls13.git/commitdiff
runtime: implement fastrand in go
authorSokolov Yura <funny.falcon@gmail.com>
Thu, 5 Jan 2017 06:36:27 +0000 (09:36 +0300)
committerJosh Bleecher Snyder <josharian@gmail.com>
Fri, 10 Feb 2017 19:16:29 +0000 (19:16 +0000)
So it could be inlined.

Using bit-tricks it could be implemented without condition
(improved trick version by Minux Ma).

Simple benchmark shows it is faster on i386 and x86_64, though
I don't know will it be faster on other architectures?

benchmark                       old ns/op     new ns/op     delta
BenchmarkFastrand-3             2.79          1.48          -46.95%
BenchmarkFastrandHashiter-3     25.9          24.9          -3.86%

Change-Id: Ie2eb6d0f598c0bb5fac7f6ad0f8b5e3eddaa361b
Reviewed-on: https://go-review.googlesource.com/34782
Reviewed-by: Minux Ma <minux@golang.org>
Run-TryBot: Minux Ma <minux@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>

12 files changed:
src/runtime/asm_386.s
src/runtime/asm_amd64.s
src/runtime/asm_amd64p32.s
src/runtime/asm_arm.s
src/runtime/asm_arm64.s
src/runtime/asm_mips64x.s
src/runtime/asm_mipsx.s
src/runtime/asm_ppc64x.s
src/runtime/asm_s390x.s
src/runtime/export_test.go
src/runtime/rand_test.go [new file with mode: 0644]
src/runtime/stubs.go

index 3d0b74ce926e34996dba96b611a8ac001691ac5d..2c265c9f6c3405cc7012fb2562c907f19f9bea51 100644 (file)
@@ -1595,20 +1595,6 @@ allsame:
        MOVL    BX, (AX)
        RET
 
-TEXT runtime·fastrand(SB), NOSPLIT, $0-4
-       get_tls(CX)
-       MOVL    g(CX), AX
-       MOVL    g_m(AX), AX
-       MOVL    m_fastrand(AX), DX
-       ADDL    DX, DX
-       MOVL    DX, BX
-       XORL    $0x88888eef, DX
-       JPL     2(PC)
-       MOVL    BX, DX
-       MOVL    DX, m_fastrand(AX)
-       MOVL    DX, ret+0(FP)
-       RET
-
 TEXT runtime·return0(SB), NOSPLIT, $0
        MOVL    $0, AX
        RET
index cb428d6de3cbf745d33cfb5b1dfd3cbca9eb6542..232c7c647d67e18d1102990eb347b703a40625fa 100644 (file)
@@ -2163,19 +2163,6 @@ eqret:
        MOVB    $0, ret+48(FP)
        RET
 
-TEXT runtime·fastrand(SB), NOSPLIT, $0-4
-       get_tls(CX)
-       MOVQ    g(CX), AX
-       MOVQ    g_m(AX), AX
-       MOVL    m_fastrand(AX), DX
-       ADDL    DX, DX
-       MOVL    DX, BX
-       XORL    $0x88888eef, DX
-       CMOVLMI BX, DX
-       MOVL    DX, m_fastrand(AX)
-       MOVL    DX, ret+0(FP)
-       RET
-
 TEXT runtime·return0(SB), NOSPLIT, $0
        MOVL    $0, AX
        RET
index c3c1c15f0c7c1b6d16860996a39f4d6b9c1565d8..ecc66647c7dab87d5adcc785aae39be816967e86 100644 (file)
@@ -991,19 +991,6 @@ eqret:
        MOVB    AX, ret+24(FP)
        RET
 
-TEXT runtime·fastrand(SB), NOSPLIT, $0-4
-       get_tls(CX)
-       MOVL    g(CX), AX
-       MOVL    g_m(AX), AX
-       MOVL    m_fastrand(AX), DX
-       ADDL    DX, DX
-       MOVL    DX, BX
-       XORL    $0x88888eef, DX
-       CMOVLMI BX, DX
-       MOVL    DX, m_fastrand(AX)
-       MOVL    DX, ret+0(FP)
-       RET
-
 TEXT runtime·return0(SB), NOSPLIT, $0
        MOVL    $0, AX
        RET
index 79c28a81789f37fea6c5cf04490bd86b609f9e00..a33fa033120a08db1b886d2fac8cbd6b82449f18 100644 (file)
@@ -971,15 +971,6 @@ _sib_notfound:
        MOVW    R0, ret+12(FP)
        RET
 
-TEXT runtime·fastrand(SB),NOSPLIT,$-4-4
-       MOVW    g_m(g), R1
-       MOVW    m_fastrand(R1), R0
-       ADD.S   R0, R0
-       EOR.MI  $0x88888eef, R0
-       MOVW    R0, m_fastrand(R1)
-       MOVW    R0, ret+0(FP)
-       RET
-
 TEXT runtime·return0(SB),NOSPLIT,$0
        MOVW    $0, R0
        RET
index 0e286d484fae169c8acc686ddf07ce67e376cae3..5f2d4a5681347a36c5c475c86d534b264896618f 100644 (file)
@@ -959,18 +959,6 @@ equal:
        MOVB    R0, ret+48(FP)
        RET
 
-TEXT runtime·fastrand(SB),NOSPLIT,$-8-4
-       MOVD    g_m(g), R1
-       MOVWU   m_fastrand(R1), R0
-       ADD     R0, R0
-       CMPW    $0, R0
-       BGE     notneg
-       EOR     $0x88888eef, R0
-notneg:
-       MOVW    R0, m_fastrand(R1)
-       MOVW    R0, ret+0(FP)
-       RET
-
 TEXT runtime·return0(SB), NOSPLIT, $0
        MOVW    $0, R0
        RET
index c2d991d36d3410cf4592c2e191d479a1f51e239d..1de8d71f67b11c9bb5abf4164320471da67fb45c 100644 (file)
@@ -831,16 +831,6 @@ notfound:
        MOVV    R1, ret+24(FP)
        RET
 
-TEXT runtime·fastrand(SB), NOSPLIT, $0-4
-       MOVV    g_m(g), R2
-       MOVWU   m_fastrand(R2), R1
-       ADDU    R1, R1
-       BGEZ    R1, 2(PC)
-       XOR     $0x88888eef, R1
-       MOVW    R1, m_fastrand(R2)
-       MOVW    R1, ret+0(FP)
-       RET
-
 TEXT runtime·return0(SB), NOSPLIT, $0
        MOVW    $0, R1
        RET
index 73da768897b8882d7339427025ea37db960d555f..4e05ab7f33a9ccc2309d9d681ae10f4f30f587f9 100644 (file)
@@ -904,16 +904,6 @@ cmp_ret:
        MOVW    R8, ret+24(FP)
        RET
 
-TEXT runtime·fastrand(SB),NOSPLIT,$0-4
-       MOVW    g_m(g), R2
-       MOVW    m_fastrand(R2), R1
-       ADDU    R1, R1
-       BGEZ    R1, 2(PC)
-       XOR     $0x88888eef, R1
-       MOVW    R1, m_fastrand(R2)
-       MOVW    R1, ret+0(FP)
-       RET
-
 TEXT runtime·return0(SB),NOSPLIT,$0
        MOVW    $0, R1
        RET
index 1d6adcc553c1c8ceb72aa55b6701897525e8041c..754c07a84cceaccab00e0486db898784713e12ce 100644 (file)
@@ -1224,17 +1224,6 @@ TEXT bytes·Compare(SB),NOSPLIT|NOFRAME,$0-56
        BR      cmpbodyBE<>(SB)
 #endif
 
-TEXT runtime·fastrand(SB), NOSPLIT, $0-4
-       MOVD    g_m(g), R4
-       MOVWZ   m_fastrand(R4), R3
-       ADD     R3, R3
-       CMPW    R3, $0
-       BGE     2(PC)
-       XOR     $0x88888eef, R3
-       MOVW    R3, m_fastrand(R4)
-       MOVW    R3, ret+0(FP)
-       RET
-
 TEXT runtime·return0(SB), NOSPLIT, $0
        MOVW    $0, R3
        RET
index c2212a5f214e9eb8d124531e63775a8f27c67643..50a29e89d2cb2b3517c5d893a3fc9ca7427a0d00 100644 (file)
@@ -851,17 +851,6 @@ TEXT runtime·memeqbodyclc(SB),NOSPLIT|NOFRAME,$0-0
        CLC     $1, 0(R3), 0(R5)
        RET
 
-TEXT runtime·fastrand(SB), NOSPLIT, $0-4
-       MOVD    g_m(g), R4
-       MOVWZ   m_fastrand(R4), R3
-       ADD     R3, R3
-       CMPW    R3, $0
-       BGE     2(PC)
-       XOR     $0x88888eef, R3
-       MOVW    R3, m_fastrand(R4)
-       MOVW    R3, ret+0(FP)
-       RET
-
 TEXT bytes·IndexByte(SB),NOSPLIT|NOFRAME,$0-40
        MOVD    s+0(FP), R3     // s => R3
        MOVD    s_len+8(FP), R4 // s_len => R4
index 9b765550ca2e3b806e54b0f16b4aedf1acb87c4e..5f85d91f5e42fcf42c36096afaaf9f2d5b0ad6bc 100644 (file)
@@ -245,3 +245,5 @@ func CountPagesInUse() (pagesInUse, counted uintptr) {
 
        return
 }
+
+func Fastrand() uint32 { return fastrand() }
diff --git a/src/runtime/rand_test.go b/src/runtime/rand_test.go
new file mode 100644 (file)
index 0000000..0f6ec0f
--- /dev/null
@@ -0,0 +1,32 @@
+// Copyright 2017 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package runtime_test
+
+import (
+       . "runtime"
+       "testing"
+)
+
+func BenchmarkFastrand(b *testing.B) {
+       b.RunParallel(func(pb *testing.PB) {
+               for pb.Next() {
+                       Fastrand()
+               }
+       })
+}
+
+func BenchmarkFastrandHashiter(b *testing.B) {
+       var m = make(map[int]int, 10)
+       for i := 0; i < 10; i++ {
+               m[i] = i
+       }
+       b.RunParallel(func(pb *testing.PB) {
+               for pb.Next() {
+                       for _ = range m {
+                               break
+                       }
+               }
+       })
+}
index e431b441b2371411678307be0e737cd9ad8e3f28..101c8cfd10884737709572f4220451a38168f72e 100644 (file)
@@ -93,8 +93,15 @@ func reflect_memmove(to, from unsafe.Pointer, n uintptr) {
 // exported value for testing
 var hashLoad = loadFactor
 
-// in asm_*.s
-func fastrand() uint32
+//go:nosplit
+func fastrand() uint32 {
+       mp := getg().m
+       fr := mp.fastrand
+       fr <<= 1
+       fr ^= uint32(int32(fr)>>31) & 0x88888eef
+       mp.fastrand = fr
+       return fr
+}
 
 //go:linkname sync_fastrand sync.fastrand
 func sync_fastrand() uint32 { return fastrand() }