Skip to content

faster rand(TaskLocalRNG(), 1:n) by outlining throw #58306

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

Merged
merged 1 commit into from
May 9, 2025

Conversation

rfourquet
Copy link
Member

@rfourquet rfourquet commented May 2, 2025

In #58089, this method took a small performance hit in some contexts. It turns out that by outlining the unlikely branch which throws on empty ranges, this hit can be recovered.

In #50509 (comment), a graph of the performance improvement of the "speed-up randperm by using our current rand(1:n)" was posted, but I realized it was only true when calls to rand(1:n) were prefixed by @inline; without @inline it was overall slower for TaskLocalRNG() for very big arrays (but still faster otherwise).

An alternative to these @inline annotation is to outline throw like here, for equivalent benefits as @inline in that randperm PR.

Assuming that PR is merged, this PR improves roughly performance by 2x for TaskLocalRNG() (no change for other RNGs):

new-shuffle-outlinethrow

While at it, I outlined a bunch of other unliky throwing branches.

After that, #50509 can probably be merged, finally!

In #58089, this method took a small performance hit in some contexts.
It turns out that by outlining unlikely branch which throw on empty
ranges, his hit can be recovered.

In #50509 (comment),
a graph of the performance improvement of the "speed-up randperm by using
our current rand(1:n)" was posted, but I realized it was only true when
calls to `rand(1:n)` were prefixed by `@inline`; without `@inline` it was
overall slower for `TaskLocalRNG()` for very big arrays (but still faster
otherwise).

An alternative to these `@inline` annotation is to outline `throw` like here,
for equivalent benefits as `@inline` in that `randperm` PR.

Assuming that PR is merged, this PR improves roughly performance by 2x for
`TaskLocalRNG()` (no change for other RNGs).
@rfourquet rfourquet added performance Must go faster randomness Random number generation and the Random stdlib labels May 2, 2025
@rfourquet rfourquet merged commit 5af63ff into master May 9, 2025
8 of 11 checks passed
@rfourquet rfourquet deleted the rf/outline-throw branch May 9, 2025 10:31
charleskawczynski pushed a commit to charleskawczynski/julia that referenced this pull request May 12, 2025
)

In JuliaLang#58089, this method took a small performance hit in some contexts. It
turns out that by outlining the unlikely branch which throws on empty
ranges, this hit can be recovered.

In
JuliaLang#50509 (comment), a
graph of the performance improvement of the "speed-up randperm by using
our current rand(1:n)" was posted, but I realized it was only true when
calls to `rand(1:n)` were prefixed by `@inline`; without `@inline` it
was overall slower for `TaskLocalRNG()` for very big arrays (but still
faster otherwise).

An alternative to these `@inline` annotation is to outline `throw` like
here, for equivalent benefits as `@inline` in that `randperm` PR.

Assuming that PR is merged, this PR improves roughly performance by 2x
for `TaskLocalRNG()` (no change for other RNGs):


![new-shuffle-outlinethrow](https://github.com/user-attachments/assets/8c0d4740-3bb4-4bcf-a49d-9af09426bec7)

While at it, I outlined a bunch of other unliky throwing branches.

After that, JuliaLang#50509 can probably be merged, finally!
charleskawczynski pushed a commit to charleskawczynski/julia that referenced this pull request May 12, 2025
)

In JuliaLang#58089, this method took a small performance hit in some contexts. It
turns out that by outlining the unlikely branch which throws on empty
ranges, this hit can be recovered.

In
JuliaLang#50509 (comment), a
graph of the performance improvement of the "speed-up randperm by using
our current rand(1:n)" was posted, but I realized it was only true when
calls to `rand(1:n)` were prefixed by `@inline`; without `@inline` it
was overall slower for `TaskLocalRNG()` for very big arrays (but still
faster otherwise).

An alternative to these `@inline` annotation is to outline `throw` like
here, for equivalent benefits as `@inline` in that `randperm` PR.

Assuming that PR is merged, this PR improves roughly performance by 2x
for `TaskLocalRNG()` (no change for other RNGs):

![new-shuffle-outlinethrow](https://github.com/user-attachments/assets/8c0d4740-3bb4-4bcf-a49d-9af09426bec7)

While at it, I outlined a bunch of other unliky throwing branches.

After that, JuliaLang#50509 can probably be merged, finally!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
performance Must go faster randomness Random number generation and the Random stdlib
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants