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

Use fastrand instead of prands #4901

Merged
merged 1 commit into from Dec 19, 2023
Merged

Use fastrand instead of prands #4901

merged 1 commit into from Dec 19, 2023

Conversation

neilalexander
Copy link
Member

In order to avoid top-level lock contention in the math/rand package, we were previously initialising our own prand sources in various structs. However these still needed to be protected by locks and they also just weren't that fast.

This PR removes those prands and replaces those calls with calls to the Go runtime's fastrand, which is a couple hundred times faster and is, more importantly, lock-free:

goos: darwin
goarch: arm64
pkg: github.com/nats-io/nats-server/v2/server/fastrand
BenchmarkFastRand32
BenchmarkFastRand32-10          1000000000               0.2564 ns/op
BenchmarkFastRand64
BenchmarkFastRand64-10          1000000000               0.5019 ns/op
BenchmarkDefaultRand32
BenchmarkDefaultRand32-10       11997840               104.4 ns/op
BenchmarkDefaultRand64
BenchmarkDefaultRand64-10       11745747               105.8 ns/op
PASS
ok      github.com/nats-io/nats-server/v2/server/fastrand       3.768s

Signed-off-by: Neil Twigg neil@nats.io

@neilalexander neilalexander requested a review from a team as a code owner December 19, 2023 17:45
Signed-off-by: Neil Twigg <neil@nats.io>
Copy link
Member

@derekcollison derekcollison left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Member

@wallyqs wallyqs left a comment

Choose a reason for hiding this comment

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

LGTM

@derekcollison derekcollison merged commit 154c728 into main Dec 19, 2023
4 checks passed
@derekcollison derekcollison deleted the neil/fastrand branch December 19, 2023 19:13
wallyqs added a commit that referenced this pull request Dec 26, 2023
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.

None yet

3 participants