Skip to content

Commit 2ebbcc6

Browse files
rfourquetcharleskawczynski
authored andcommitted
faster rand(TaskLocalRNG(), 1:n) by outlining throw (JuliaLang#58306)
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!
1 parent bdd1b7f commit 2ebbcc6

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

stdlib/Random/src/Random.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ using Base: BitInteger, BitInteger_types, BitUnsigned, require_one_based_indexin
2121
=======
2222
>>>>>>> 856025e8c6 (Random: make a new "strong" RNG using SHA)
2323

24+
<<<<<<< HEAD
25+
=======
26+
using Base: BitInteger, BitInteger_types, BitUnsigned, require_one_based_indexing,
27+
_throw_argerror
28+
29+
>>>>>>> 5af63ffaa3 (faster `rand(TaskLocalRNG(), 1:n)` by outlining `throw` (#58306))
2430
import Base: copymutable, copy, copy!, ==, hash, convert,
2531
rand, randn, show
2632

0 commit comments

Comments
 (0)