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 math/rand.Rand.Read to generate slices of random data #1066

Merged
merged 2 commits into from
Nov 19, 2021

Conversation

mtrmac
Copy link
Collaborator

@mtrmac mtrmac commented Nov 19, 2021

… instead of open-coding loops, primarily to simplify.

In one case this is a speedup, in the other it’s a noticeable slowdown, a correctness improvement in theory at least.

This is simpler to read, and actually 30% faster.

Signed-off-by: Miloslav Trmač <mitr@redhat.com>
@mtrmac
Copy link
Collaborator Author

mtrmac commented Nov 19, 2021

(If you prefer the code as is, feel completely free to close this PR.)

The slice manipulation _is_ actually almost 50% faster,
but the implementation is only correct for sizes that are
a multiple of 8, the use of unsafe is somewhat scary, and
there's only exactly one caller, with the total difference
in the order of tens of milliseconds.

Let's do the simple thing instead.

Signed-off-by: Miloslav Trmač <mitr@redhat.com>
Copy link
Member

@vrothberg vrothberg left a comment

Choose a reason for hiding this comment

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

@mtrmac
Copy link
Collaborator Author

mtrmac commented Nov 19, 2021

FWIW

% go test -v -bench . -benchmem ./bench_test.go
goos: darwin
goarch: amd64
cpu: Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
BenchmarkStandard
BenchmarkStandard-12       	      28	  38833565 ns/op	52434254 B/op	       2 allocs/op
BenchmarkSourceOnly
BenchmarkSourceOnly-12     	      22	  51455058 ns/op	52428800 B/op	       1 allocs/op
BenchmarkSourceOnly8
BenchmarkSourceOnly8-12    	      22	  50682468 ns/op	52428903 B/op	       1 allocs/op
BenchmarkUnsafeSlice
BenchmarkUnsafeSlice-12    	      56	  20757283 ns/op	52428803 B/op	       1 allocs/op

on 50 MiB output, with

  • Standard: rand.New(rand.NewSource(seed)).Read
  • SourceOnly: Two nested loops turning Int63 into 7 bytes, like one of the previous implementations
  • SourceOnly8: Two nested loops turning Int63 into 8 bytes (it turns out the compiler doesn’t unroll that any better)
  • UnsafeSlice: A single loop storing Int63 into an int64, like the other previous implementation

Copy link
Member

@giuseppe giuseppe left a comment

Choose a reason for hiding this comment

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

LGTM

@giuseppe giuseppe merged commit bd1f856 into containers:main Nov 19, 2021
@mtrmac mtrmac deleted the random-read branch November 19, 2021 08:27
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