Skip to content

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 41 additions & 55 deletions stdlib/Random/src/misc.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
Copy link
Member

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?

Copy link
Member Author

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.


## shuffle & shuffle!

"""
Expand All @@ -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)
Copy link
Member

@jakobnissen jakobnissen May 9, 2025

Choose a reason for hiding this comment

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

Can we delete this @inbounds, given that this is out-of-bounds for OffsetArrays? Benchmarking in #57771 suggests this @inbounds has almost no effect on speed.
Edit: Ah, there is a require_one_based_indexing above. Nonetheless.

Copy link
Member Author

Choose a reason for hiding this comment

The 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 @inbounds.

Copy link
Member Author

Choose a reason for hiding this comment

The 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.

julia> @btime randperm!($([1:2^12;]));
  5.636 μs (0 allocations: 0 bytes) # with @inbounds
  6.562 μs (0 allocations: 0 bytes) # without @inbounds

Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
@inbounds for i = 2:length(a)
for i = 2:length(a)

j = rand(rng, 1:i)
a[i], a[j] = a[j], a[i]
i == 1 + mask && (mask = 2 * mask + 1)
end
return a
end
Expand Down Expand Up @@ -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))
Expand All @@ -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))
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down