-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
speed-up randperm
by using our current rand(1:n)
#50509
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -176,11 +176,6 @@ julia> randsubseq(Xoshiro(123), 1:8, 0.3) | |||||
randsubseq(A::AbstractArray, p::Real) = randsubseq(default_rng(), A, p) | ||||||
|
||||||
|
||||||
## rand Less Than Masked 52 bits (helper function) | ||||||
|
||||||
"Return a sampler generating a random `Int` (masked with `mask`) in ``[0, n)``, when `n <= 2^52`." | ||||||
ltm52(n::Int, mask::Int=nextpow(2, n)-1) = LessThan(n-1, Masked(mask, UInt52Raw(Int))) | ||||||
|
||||||
## shuffle & shuffle! | ||||||
|
||||||
""" | ||||||
|
@@ -191,31 +186,22 @@ optionally supplying the random-number generator `rng`. | |||||
|
||||||
# Examples | ||||||
```jldoctest | ||||||
julia> shuffle!(Xoshiro(123), Vector(1:10)) | ||||||
10-element Vector{Int64}: | ||||||
5 | ||||||
4 | ||||||
2 | ||||||
3 | ||||||
6 | ||||||
10 | ||||||
8 | ||||||
1 | ||||||
9 | ||||||
7 | ||||||
julia> shuffle!(Xoshiro(0), Vector(1:6)) | ||||||
6-element Vector{Int64}: | ||||||
5 | ||||||
1 | ||||||
2 | ||||||
6 | ||||||
3 | ||||||
4 | ||||||
``` | ||||||
""" | ||||||
function shuffle!(r::AbstractRNG, a::AbstractArray) | ||||||
function shuffle!(rng::AbstractRNG, a::AbstractArray) | ||||||
# keep it consistent with `randperm!` and `randcycle!` if possible | ||||||
require_one_based_indexing(a) | ||||||
n = length(a) | ||||||
@assert n <= Int64(2)^52 | ||||||
n == 0 && return a | ||||||
mask = 3 | ||||||
@inbounds for i = 2:n | ||||||
j = 1 + rand(r, ltm52(i, mask)) | ||||||
@inbounds for i = 2:length(a) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we delete this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We currently support one one-based indexing, but yes I will re-benchmark without the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So it doesn't make a big difference, but still bring some speed-up, so I will prefer not changing that here. E.g.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
j = rand(rng, 1:i) | ||||||
a[i], a[j] = a[j], a[i] | ||||||
i == 1 + mask && (mask = 2 * mask + 1) | ||||||
end | ||||||
return a | ||||||
end | ||||||
|
@@ -247,18 +233,14 @@ indices, see [`randperm`](@ref). | |||||
|
||||||
# Examples | ||||||
```jldoctest | ||||||
julia> shuffle(Xoshiro(123), Vector(1:10)) | ||||||
10-element Vector{Int64}: | ||||||
5 | ||||||
4 | ||||||
2 | ||||||
3 | ||||||
6 | ||||||
10 | ||||||
8 | ||||||
1 | ||||||
9 | ||||||
7 | ||||||
julia> shuffle(Xoshiro(0), 1:6) | ||||||
6-element Vector{Int64}: | ||||||
5 | ||||||
1 | ||||||
2 | ||||||
6 | ||||||
3 | ||||||
4 | ||||||
``` | ||||||
""" | ||||||
shuffle(r::AbstractRNG, a::AbstractArray) = shuffle!(r, copymutable(a)) | ||||||
|
@@ -285,12 +267,14 @@ To randomly permute an arbitrary vector, see [`shuffle`](@ref) or | |||||
|
||||||
# Examples | ||||||
```jldoctest | ||||||
julia> randperm(Xoshiro(123), 4) | ||||||
4-element Vector{Int64}: | ||||||
julia> randperm(Xoshiro(0), 6) | ||||||
6-element Vector{Int64}: | ||||||
5 | ||||||
1 | ||||||
4 | ||||||
2 | ||||||
6 | ||||||
3 | ||||||
4 | ||||||
``` | ||||||
""" | ||||||
randperm(r::AbstractRNG, n::T) where {T <: Integer} = randperm!(r, Vector{T}(undef, n)) | ||||||
|
@@ -309,29 +293,28 @@ optional `rng` argument specifies a random number generator (see | |||||
|
||||||
# Examples | ||||||
```jldoctest | ||||||
julia> randperm!(Xoshiro(123), Vector{Int}(undef, 4)) | ||||||
4-element Vector{Int64}: | ||||||
julia> randperm!(Xoshiro(0), Vector{Int}(undef, 6)) | ||||||
6-element Vector{Int64}: | ||||||
5 | ||||||
1 | ||||||
4 | ||||||
2 | ||||||
6 | ||||||
3 | ||||||
4 | ||||||
``` | ||||||
""" | ||||||
function randperm!(r::AbstractRNG, a::AbstractArray{<:Integer}) | ||||||
function randperm!(rng::AbstractRNG, a::AbstractArray{<:Integer}) | ||||||
# keep it consistent with `shuffle!` and `randcycle!` if possible | ||||||
Base.require_one_based_indexing(a) | ||||||
n = length(a) | ||||||
@assert n <= Int64(2)^52 | ||||||
n == 0 && return a | ||||||
a[1] = 1 | ||||||
mask = 3 | ||||||
@inbounds for i = 2:n | ||||||
j = 1 + rand(r, ltm52(i, mask)) | ||||||
j = rand(rng, 1:i) | ||||||
if i != j # a[i] is undef (and could be #undef) | ||||||
a[i] = a[j] | ||||||
end | ||||||
a[j] = i | ||||||
i == 1 + mask && (mask = 2 * mask + 1) | ||||||
end | ||||||
return a | ||||||
end | ||||||
|
@@ -360,10 +343,13 @@ which are sampled uniformly. If `n == 0`, `randcycle` returns an empty vector. | |||||
|
||||||
# Examples | ||||||
```jldoctest | ||||||
julia> randcycle(Xoshiro(123), 6) | ||||||
julia> randcycle(Xoshiro(0), 6) | ||||||
6-element Vector{Int64}: | ||||||
5 | ||||||
1 | ||||||
4 | ||||||
6 | ||||||
3 | ||||||
2 | ||||||
6 | ||||||
3 | ||||||
|
@@ -391,30 +377,30 @@ which are sampled uniformly. If `A` is empty, `randcycle!` leaves it unchanged. | |||||
|
||||||
# Examples | ||||||
```jldoctest | ||||||
julia> randcycle!(Xoshiro(123), Vector{Int}(undef, 6)) | ||||||
julia> randcycle!(Xoshiro(0), Vector{Int}(undef, 6)) | ||||||
6-element Vector{Int64}: | ||||||
5 | ||||||
1 | ||||||
4 | ||||||
6 | ||||||
3 | ||||||
2 | ||||||
6 | ||||||
3 | ||||||
1 | ||||||
``` | ||||||
""" | ||||||
function randcycle!(r::AbstractRNG, a::AbstractArray{<:Integer}) | ||||||
function randcycle!(rng::AbstractRNG, a::AbstractArray{<:Integer}) | ||||||
# keep it consistent with `shuffle!` and `randperm!` if possible | ||||||
Base.require_one_based_indexing(a) | ||||||
n = length(a) | ||||||
@assert n <= Int64(2)^52 | ||||||
n == 0 && return a | ||||||
a[1] = 1 | ||||||
mask = 3 | ||||||
# Sattolo's algorithm: | ||||||
@inbounds for i = 2:n | ||||||
j = 1 + rand(r, ltm52(i-1, mask)) | ||||||
j = rand(rng, 1:i-1) | ||||||
a[i] = a[j] | ||||||
a[j] = i | ||||||
i == 1 + mask && (mask = 2 * mask + 1) | ||||||
end | ||||||
return a | ||||||
end | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Although this is not exported, I see that it is referenced in at least two packages:
SimpleChains.jl: https://github.com/PumasAI/SimpleChains.jl/blob/f028d69679d47f11d35e7f311abdf0d1d3bfab9c/src/utils.jl#L114
REPLference.jl: https://github.com/udohjeremiah/REPLference.jl/blob/f3801aac2713ee5f19705513d8318e0923e0bee7/src/_22_random.jl#L170
Should we keep it to avoid breakage? Or just submit PRs to update those packages to remove the obsolete function?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh good catch! I pretty much prefer deleting this function, so will attempt PRs against these packages.