From cf61946a7f46235812d4aa4af2efa67eb26bd987 Mon Sep 17 00:00:00 2001 From: zhangyunhao Date: Mon, 21 Mar 2022 16:05:27 +0800 Subject: [PATCH 1/4] fastrand: optimize Read MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit name old time/op new time/op delta SingleCore/math/rand-Uint32()-16 10.8ns ± 0% 10.8ns ± 0% ~ (p=0.913 n=7+10) SingleCore/fast-rand-Uint32()-16 2.26ns ± 0% 2.25ns ± 0% ~ (p=0.015 n=10+10) SingleCore/math/rand-Uint64()-16 11.1ns ± 0% 11.1ns ± 0% ~ (p=0.014 n=10+8) SingleCore/fast-rand-Uint64()-16 5.03ns ± 0% 4.75ns ± 0% -5.50% (p=0.000 n=10+9) SingleCore/math/rand-Read1000-16 682ns ± 0% 682ns ± 0% ~ (p=0.927 n=10+10) SingleCore/fast-rand-Read1000-16 298ns ± 1% 150ns ± 0% -49.69% (p=0.000 n=10+9) MultipleCore/math/rand-Uint32()-16 114ns ± 3% 113ns ± 4% ~ (p=0.306 n=10+10) MultipleCore/fast-rand-Uint32()-16 0.18ns ± 1% 0.18ns ± 2% ~ (p=0.006 n=9+10) MultipleCore/math/rand-Uint64()-16 115ns ± 6% 118ns ± 3% ~ (p=0.018 n=10+9) MultipleCore/fast-rand-Uint64()-16 0.39ns ± 1% 0.38ns ± 0% -1.55% (p=0.000 n=10+8) MultipleCore/math/rand-Read1000-16 1.02µs ± 3% 1.03µs ± 4% ~ (p=0.644 n=10+10) MultipleCore/fast-rand-Read1000-16 112ns ± 0% 102ns ± 1% -9.38% (p=0.000 n=10+10) --- lang/fastrand/fastrand.go | 58 ++++++++++++++++++++++++--------------- 1 file changed, 36 insertions(+), 22 deletions(-) diff --git a/lang/fastrand/fastrand.go b/lang/fastrand/fastrand.go index 58a7cc72..265fbd3e 100644 --- a/lang/fastrand/fastrand.go +++ b/lang/fastrand/fastrand.go @@ -16,15 +16,14 @@ package fastrand import ( + "math/bits" "unsafe" "github.com/bytedance/gopkg/internal/runtimex" ) // Uint32 returns a pseudo-random 32-bit value as a uint32. -func Uint32() uint32 { - return runtimex.Fastrand() -} +var Uint32 = runtimex.Fastrand // Uint64 returns a pseudo-random 64-bit value as a uint64. func Uint64() uint64 { @@ -124,6 +123,33 @@ func Uint64n(n uint64) uint64 { return Uint64() % n } +// wyrand: https://github.com/wangyi-fudan/wyhash +type wyrand uint64 + +func _wymix(a, b uint64) uint64 { + hi, lo := bits.Mul64(a, b) + return hi ^ lo +} + +func (r *wyrand) Uint64() uint64 { + *r += wyrand(0xa0761d6478bd642f) + return _wymix(uint64(*r), uint64(*r^wyrand(0xe7037ed1a0b428db))) +} + +func (r *wyrand) Uint64n(n uint64) uint64 { + return r.Uint64() % n +} + +func (r *wyrand) Uint32() uint32 { + return uint32(Uint64()) +} + +func (r *wyrand) Uint32n(n int) uint32 { + // This is similar to Uint32() % n, but faster. + // See https://lemire.me/blog/2016/06/27/a-fast-alternative-to-the-modulo-reduction/ + return uint32(uint64(r.Uint32()) * uint64(n) >> 32) +} + // Read generates len(p) random bytes and writes them into p. // It always returns len(p) and a nil error. // It is safe for concurrent use. @@ -133,33 +159,21 @@ func Read(p []byte) (int, error) { return 0, nil } - // Used for local XORSHIFT. - // Xorshift paper: https://www.jstatsoft.org/article/view/v008i14/xorshift.pdf - s0, s1 := Uint32(), Uint32() + r := wyrand(Uint32()) - if l >= 4 { + if l >= 8 { var i int - uint32p := *(*[]uint32)(unsafe.Pointer(&p)) - for l >= 4 { - // Local XORSHIFT. - s1 ^= s1 << 17 - s1 = s1 ^ s0 ^ s1>>7 ^ s0>>16 - s0, s1 = s1, s0 - - uint32p[i] = s0 + s1 + uint64p := *(*[]uint64)(unsafe.Pointer(&p)) + for l >= 8 { + uint64p[i] = r.Uint64() i++ - l -= 4 + l -= 8 } } if l > 0 { - // Local XORSHIFT. - s1 ^= s1 << 17 - s1 = s1 ^ s0 ^ s1>>7 ^ s0>>16 - - r := s0 + s1 for l > 0 { - p[len(p)-l] = byte(r >> (l * 8)) + p[len(p)-l] = byte(r.Uint64() >> (l * 8)) l-- } } From acde17263ec92928281db63df726416843d5fa95 Mon Sep 17 00:00:00 2001 From: zhangyunhao Date: Mon, 21 Mar 2022 16:43:17 +0800 Subject: [PATCH 2/4] fix feishu-notify --- .github/workflows/feishu-notify.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/feishu-notify.yml b/.github/workflows/feishu-notify.yml index cd5d3252..e7aa9228 100644 --- a/.github/workflows/feishu-notify.yml +++ b/.github/workflows/feishu-notify.yml @@ -26,7 +26,7 @@ jobs: See: https://github.com/${{ github.repository }}/pull/${{ github.event.pull_request.number }} run: | - sudo apt update + sudo apt update -y sudo apt install wget wget -q https://github.com/xiachufang/actions-feishu/releases/download/${{ env.ACTIONS_FEISHU_TAG }}/linux-amd64-actions-feishu.tar.gz tar zxf linux-amd64-actions-feishu.tar.gz feishu @@ -51,7 +51,7 @@ jobs: See: https://github.com/${{ github.repository }}/issues/${{ github.event.issue.number }} run: | - sudo apt update + sudo apt update -y sudo apt install wget wget -q https://github.com/xiachufang/actions-feishu/releases/download/${{ env.ACTIONS_FEISHU_TAG }}/linux-amd64-actions-feishu.tar.gz tar zxf linux-amd64-actions-feishu.tar.gz feishu From dfd2703482c18fd10726e3a1e0151735a68ea49a Mon Sep 17 00:00:00 2001 From: zhangyunhao Date: Mon, 21 Mar 2022 18:01:27 +0800 Subject: [PATCH 3/4] fix feishu-notify --- .github/workflows/feishu-notify.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/feishu-notify.yml b/.github/workflows/feishu-notify.yml index e7aa9228..ee1184f7 100644 --- a/.github/workflows/feishu-notify.yml +++ b/.github/workflows/feishu-notify.yml @@ -26,8 +26,8 @@ jobs: See: https://github.com/${{ github.repository }}/pull/${{ github.event.pull_request.number }} run: | - sudo apt update -y - sudo apt install wget + sudo apt update + sudo apt install wget -y wget -q https://github.com/xiachufang/actions-feishu/releases/download/${{ env.ACTIONS_FEISHU_TAG }}/linux-amd64-actions-feishu.tar.gz tar zxf linux-amd64-actions-feishu.tar.gz feishu ./feishu @@ -52,7 +52,7 @@ jobs: run: | sudo apt update -y - sudo apt install wget + sudo apt install wget -y wget -q https://github.com/xiachufang/actions-feishu/releases/download/${{ env.ACTIONS_FEISHU_TAG }}/linux-amd64-actions-feishu.tar.gz tar zxf linux-amd64-actions-feishu.tar.gz feishu ./feishu From c0b1d0b2e7a70e0c6ab0e658db2398b499f602df Mon Sep 17 00:00:00 2001 From: zhangyunhao Date: Mon, 21 Mar 2022 18:02:14 +0800 Subject: [PATCH 4/4] fix feishu-notify --- .github/workflows/feishu-notify.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/feishu-notify.yml b/.github/workflows/feishu-notify.yml index ee1184f7..ed089420 100644 --- a/.github/workflows/feishu-notify.yml +++ b/.github/workflows/feishu-notify.yml @@ -51,7 +51,7 @@ jobs: See: https://github.com/${{ github.repository }}/issues/${{ github.event.issue.number }} run: | - sudo apt update -y + sudo apt update sudo apt install wget -y wget -q https://github.com/xiachufang/actions-feishu/releases/download/${{ env.ACTIONS_FEISHU_TAG }}/linux-amd64-actions-feishu.tar.gz tar zxf linux-amd64-actions-feishu.tar.gz feishu