Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(lang/fastrand): add Read([]byte) function #90

Merged
merged 1 commit into from
Oct 12, 2021

Conversation

ziposcar
Copy link
Contributor

What

This PR add a new function func Read([]byte) (n int, err error) for fastrand package. It has the same signature with math/rand.Read and math/rand.(*Rand).Read.

Why

fastrand is so good that I want to use it everywhere~
And in my benchmark, it's at least 30% faster than math/rand.(*Rand).Read in single goroutine.

How

Just like math/rand.Read.

@ziposcar ziposcar force-pushed the feature-fastrand-read branch from f158a7a to 48e4053 Compare September 30, 2021 11:52
@ziposcar ziposcar marked this pull request as ready for review September 30, 2021 11:54
@ziposcar ziposcar force-pushed the feature-fastrand-read branch from b750805 to c75ec65 Compare October 9, 2021 07:26
@zhangyunhao116 zhangyunhao116 self-assigned this Oct 9, 2021
@zhangyunhao116 zhangyunhao116 changed the title feat[lang/fastrand]: add Read([]byte) function feat(lang/fastrand): add Read([]byte) function Oct 9, 2021
@zhangyunhao116
Copy link
Member

Thank you!

Guess we could use the unsafe to accelerate this function, 32-bit memory copy will be much faster.

Here is an example.

// Read generates len(p) random bytes and writes them into p.
// It always returns len(p) and a nil error. And it is safe
// for concurrent use.
func Read(p []byte) (n int, err error) {
	l := len(p)

	if l >= 4 {
		i := 0
		uint32p := *(*[]uint32)(unsafe.Pointer(&p))
		for l >= 4 {
			uint32p[i] = Uint32()
			i++
			l -= 4
		}
	}

	if l > 0 {
		r := Uint32()
		for l > 0 {
			p[len(p)-l] = byte(r >> (l * 8))
			l--
		}
	}

	return len(p), nil
}

Performance comparison. Read2 is the new one.

name           time/op
10Read-16      8.66ns ± 0%
10Read2-16     7.29ns ± 0%
100Read-16     68.6ns ± 0%
100Read2-16    42.7ns ± 0%
1000Read-16     652ns ± 0%
1000Read2-16    362ns ± 0%
10000Read-16   6.42µs ± 0%
10000Read2-16  3.60µs ± 0%

Use local XORSHIFT looks like a better idea, but it is too complicated, we could implement it in the next PR.
Let me know if anything can help :)

@ziposcar
Copy link
Contributor Author

Great! So what should I do? Close this MR or just update my commit ?

@zhangyunhao116
Copy link
Member

Just update this PR please, I can implement the complicated version in the next PR.

@ziposcar ziposcar force-pushed the feature-fastrand-read branch from c75ec65 to 8161583 Compare October 11, 2021 10:26
@ziposcar
Copy link
Contributor Author

Updated. Thanks for review

Copy link
Member

@zhangyunhao116 zhangyunhao116 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@zhangyunhao116 zhangyunhao116 merged commit 9968b6c into bytedance:develop Oct 12, 2021
zhangyunhao116 added a commit that referenced this pull request Jan 18, 2022
* release: 20210913 (#83) (#88)

* feat: circuitbreaker.panel use skipmap (#78)

* fix(metainfo): fix misuse of append (#79)

* fix(lscq): add write barrier for LSCQ Pointer (#80)

* feat(metainfo): improve backward APIs (#81)

* release: 20210913 (#83) (#93)

* feat: circuitbreaker.panel use skipmap (#78)

* fix(metainfo): fix misuse of append (#79)

* fix(lscq): add write barrier for LSCQ Pointer (#80)

* feat(metainfo): improve backward APIs (#81)

* chore(skipmap,skipset): remove duplicated code generation declaration (#87)

* fix(lscq): cas2 use runtime.noescape (#94)

* feat: add fastrand.Read() (#90)

Co-authored-by: liyichao <[email protected]>

* doc: add badge to link to godoc (#99)

* ci: skip golang related workflows when there are only doc changes (#101)

* ci: add workflow for feishu/lark notification (#100)

* ci: add workflow for feishu/lark notification

* ci: fix typo

* ci: also send feishu notification on issue opened

Co-authored-by: Jonathan Lu <[email protected]>

* feat(metainfo): define standard for backward prefix (#102)

* feat(fastrand): support Read,Shuffle,Perm (#103)

* feat(fastrand): Read remove temp buffer (#104)

* ci: add github workflow for performance regression check (#95)

* ci: add github workflow for performance regression check

Comment "/benchdiff" to trigger this check

* ci: add comments to pr-benchdiff.yml

* ci: report benchdiff result to PR_HEAD's check run

* ci: various changes

- support "pull_reqeust" event
- filter deleted go packages
- post a comment on job started
- post a comment on job failed

* ci: let xargs ignore empty line

* ci: skip benchdiff when there are only doc changes

* ci: set -x for debugging

* ci: make sure we are using github default branch as baseline

* ci: set benchtime=1s for benchdiff

* chore: add github issue forms (#105)

* test(lang/syncx): add benchmark for RWMutex (#89)

* fix(xxhash3): add fallback to fix panic occurs on non avx2, non sse2 machine (#108)

Co-authored-by: Ye Li <[email protected]>

* feat: add zset (#98)

* feat: add zset

* chore(zset): update comment

* docs(zset): add readme

* chore: some code style fixes

* chore(zset): rename Float64RangeOpt -> RangeOpt

* chore(zset): comment style fixes

* chore(zset): add a todo about maxLevel

* chore(zset): another comment fix

* chore(zset): move skiplist impl to another file

* chore(zset): add license header for opt.go

* chore: add myself as zset's CODEOWNER

* zset: don't use z.Range(0, z.Len()-1)

* doc: remove redundant section

* fix(zset): break when key is not exist

* chore(zset): simplify func name

* chore(zset): update cheatsheet

* chore(zset): meaningful const

* refactor(zset): optionalArray.init()

* docs: update zset readme

* docs(zset): also add @zhangyunhao116 as code owner

* docs(zset): some grammar fixes

* docs: fix docs typo (#109)

* feat: auto tuning gc (#112)

* feat: auto tuning gc

* doc: gctuner

* fix: gctuner tests data race (#113)

* fix: gctuner tests data race

* chore: add owner

* chore(ci): use self-hosted runner (#114)

* chore(ci): use self-hosted runner

* fix lint

Co-authored-by: Shengyu Zhang <[email protected]>
Co-authored-by: ziposcar <[email protected]>
Co-authored-by: liyichao <[email protected]>
Co-authored-by: Shengyu Zhang <[email protected]>
Co-authored-by: Jonathan Lu <[email protected]>
Co-authored-by: Pure White <[email protected]>
Co-authored-by: lyeeeeee <[email protected]>
Co-authored-by: Ye Li <[email protected]>
Co-authored-by: chyroc <[email protected]>
Co-authored-by: Joway <[email protected]>
Co-authored-by: Joway <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants