Skip to content

Commit 8146597

Browse files
committed
Reduce allocations for findfirst
1 parent 99c257f commit 8146597

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

src/Random.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,9 @@ function rand(rng::AbstractRNG, sampler::NodeSampler{N,F,W}) where {N,F,W<:Funct
7878
return out[]
7979
end
8080
function sample_idx(rng::AbstractRNG, weights)
81-
normalization = sum(weights)
82-
r = rand(rng, typeof(normalization)) * normalization
83-
return findfirst(cumsum(weights) .> r)::Int
81+
csum = cumsum(weights)
82+
r = rand(rng, eltype(weights)) * csum[end]
83+
return findfirst(ci -> ci > r, csum)::Int
8484
end
8585

8686
end

0 commit comments

Comments
 (0)