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

Add function barrier in rand! #1350

Merged
merged 1 commit into from
Jun 24, 2021
Merged

Add function barrier in rand! #1350

merged 1 commit into from
Jun 24, 2021

Conversation

devmotion
Copy link
Member

This PR is an alternative to #1281 that does not introduce any additional keyword arguments for rand!.

It introduces a function barrier in rand!(rng, s::Sampleable{Univariate}, ::AbstractArray) since sampler(s) is not type stable e.g. for Beta distributions.

On master:

julia> using BenchmarkTools, Distributions, Random

julia> x = zeros(100_000);

julia> f(x) = rand!(Beta(0.1, 1.0), x);

julia> @benchmark f($x)
BenchmarkTools.Trial: 
  memory estimate:  3.04 MiB
  allocs estimate:  199490
  --------------
  minimum time:     18.404 ms (0.00% GC)
  median time:      19.938 ms (0.00% GC)
  mean time:        20.054 ms (0.38% GC)
  maximum time:     23.241 ms (0.00% GC)
  --------------
  samples:          249
  evals/sample:     1

With this PR:

julia> using BenchmarkTools, Distributions, Random

julia> x = zeros(100_000);

julia> f(x) = rand!(Beta(0.1, 1.0), x);

julia> @benchmark f($x)
BenchmarkTools.Trial: 
  memory estimate:  64 bytes
  allocs estimate:  1
  --------------
  minimum time:     13.786 ms (0.00% GC)
  median time:      14.192 ms (0.00% GC)
  mean time:        14.275 ms (0.00% GC)
  maximum time:     17.123 ms (0.00% GC)
  --------------
  samples:          350
  evals/sample:     1

@devmotion
Copy link
Member Author

@andreasnoack Since you were involved in the discussion of the original PR, maybe you can review this PR?

@andreasnoack andreasnoack merged commit 7b3f5ac into master Jun 24, 2021
@andreasnoack andreasnoack deleted the dw/functionbarrier branch June 24, 2021 11:29
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