Skip to content

Commit

Permalink
Use inline at function level
Browse files Browse the repository at this point in the history
  • Loading branch information
Tortar committed Apr 18, 2024
1 parent 81d91ea commit e5fd2aa
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 30 deletions.
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,28 +29,28 @@ julia> iter = Iterators.filter(x -> x != 10, 1:10^7);
julia> wv(el) = 1.0

julia> @btime itsample($rng, $iter, 10^4, algRSWRSKIP);
14.578 ms (5 allocations: 156.39 KiB)
11.744 ms (5 allocations: 156.39 KiB)

julia> @btime sample($rng, collect($iter), 10^4; replace=true);
136.139 ms (20 allocations: 146.91 MiB)
131.933 ms (20 allocations: 146.91 MiB)

julia> @btime itsample($rng, $iter, 10^4, algL);
10.591 ms (3 allocations: 78.22 KiB)
10.260 ms (3 allocations: 78.22 KiB)

julia> @btime sample($rng, collect($iter), 10^4; replace=false);
134.352 ms (27 allocations: 147.05 MiB)
132.069 ms (27 allocations: 147.05 MiB)

julia> @btime itsample($rng, $iter, $wv, 10^4, algWRSWRSKIP);
32.892 ms (12 allocations: 568.83 KiB)
32.278 ms (18 allocations: 547.34 KiB)

julia> @btime sample($rng, collect($iter), Weights($wv.($iter)), 10^4; replace=true);
545.058 ms (45 allocations: 702.33 MiB)
348.220 ms (49 allocations: 675.21 MiB)

julia> @btime itsample($rng, $iter, $wv, 10^4, algAExpJ);
41.092 ms (11 allocations: 234.78 KiB)
39.965 ms (11 allocations: 234.78 KiB)

julia> @btime sample($rng, collect($iter), Weights($wv.($iter)), 10^4; replace=false);
312.880 ms (43 allocations: 370.19 MiB)
306.039 ms (43 allocations: 370.19 MiB)
```

More information can be found in the [documentation](https://juliadynamics.github.io/StreamSampling.jl/dev/).
2 changes: 1 addition & 1 deletion docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ julia> using StreamSampling
julia> rs = ReservoirSample(Int, 5);

julia> for x in 1:100
@inline update!(rs, x)
update!(rs, x)
end

julia> value(rs)
Expand Down
5 changes: 5 additions & 0 deletions src/StreamSampling.jl
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,11 @@ export update!
Returns the elements collected in the sample at the current
sampling stage.
Note that even if the sampling respects the schema it is assigned
when [`ReservoirSample`](@ref) is instantiated, some ordering in
the sample can be more probable than others. To represent each one
with the same probability call `shuffle!` over the result.
"""
function value end

Expand Down
18 changes: 9 additions & 9 deletions src/UnweightedSamplingMulti.jl
Original file line number Diff line number Diff line change
Expand Up @@ -72,41 +72,41 @@ function ReservoirSample(rng::AbstractRNG, T, n::Integer, method::AlgRSWRSKIP; o
end
end

function update!(s::Union{SampleMultiAlgR, SampleMultiOrdAlgR}, el)
@inline function update!(s::Union{SampleMultiAlgR, SampleMultiOrdAlgR}, el)
n = length(s.value)
s.seen_k += 1
if s.seen_k <= n
s.value[s.seen_k] = el
@inbounds s.value[s.seen_k] = el
else
j = rand(s.rng, 1:s.seen_k)
if j <= n
s.value[j] = el
@inbounds s.value[j] = el
update_order!(s, j)
end
end
return s
end
function update!(s::Union{SampleMultiAlgL, SampleMultiOrdAlgL}, el)
@inline function update!(s::Union{SampleMultiAlgL, SampleMultiOrdAlgL}, el)
n = length(s.value)
s.seen_k += 1
s.skip_k -= 1
if s.seen_k <= n
s.value[s.seen_k] = el
@inbounds s.value[s.seen_k] = el
s.seen_k == n && @inline recompute_skip!(s, n)
elseif s.skip_k < 0
j = rand(s.rng, 1:n)
s.value[j] = el
@inbounds s.value[j] = el
update_order!(s, j)
@inline recompute_skip!(s, n)
end
return s
end
function update!(s::AbstractWrReservoirSampleMulti, el)
@inline function update!(s::AbstractWrReservoirSampleMulti, el)
n = length(s.value)
s.seen_k += 1
s.skip_k -= 1
if s.seen_k <= n
s.value[s.seen_k] = el
@inbounds s.value[s.seen_k] = el
if s.seen_k == n
recompute_skip!(s, n)
s.value = sample(s.rng, s.value, n, ordered=is_ordered(s))
Expand Down Expand Up @@ -226,7 +226,7 @@ end

function update_all!(s, iter, ordered)
for x in iter
@inline update!(s, x)
update!(s, x)
end
return ordered ? ordered_value(s) : shuffle!(s.rng, value(s))
end
Expand Down
7 changes: 3 additions & 4 deletions src/UnweightedSamplingSingle.jl
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,14 @@ function ReservoirSample(rng::AbstractRNG, T, method::AlgR)
return SampleSingleAlgR{T, typeof(rng)}(0, rng)
end

function update!(s::SampleSingleAlgR, el)
@inline function update!(s::SampleSingleAlgR, el)
s.state += 1
if rand(s.rng) <= 1/s.state
s.value = el
end
return s
end

function update!(s::SampleSingleAlgL, el)
@inline function update!(s::SampleSingleAlgL, el)
if s.skip_k > 0
s.skip_k -= 1
else
Expand All @@ -67,7 +66,7 @@ end
function reservoir_sample(rng, iter, method::ReservoirAlgorithm = algL)
s = ReservoirSample(rng, calculate_eltype(iter), method)
for x in iter
@inline update!(s, x)
update!(s, x)
end
return value(s)
end
12 changes: 6 additions & 6 deletions src/WeightedSamplingMulti.jl
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ function ReservoirSample(rng::AbstractRNG, T, n::Integer, method::AlgWRSWRSKIP;
end
end

function update!(s::SampleMultiAlgARes, el, w)
@inline function update!(s::SampleMultiAlgARes, el, w)
n = s.n
s.seen_k += 1
priority = -randexp(s.rng)/w
Expand All @@ -78,7 +78,7 @@ function update!(s::SampleMultiAlgARes, el, w)
end
return s
end
function update!(s::SampleMultiAlgAExpJ, el, w)
@inline function update!(s::SampleMultiAlgAExpJ, el, w)
n = s.n
s.seen_k += 1
s.state -= w
Expand All @@ -94,13 +94,13 @@ function update!(s::SampleMultiAlgAExpJ, el, w)
end
return s
end
function update!(s::Union{SampleMultiAlgWRSWRSKIP, SampleMultiOrdAlgWRSWRSKIP}, el, w)
@inline function update!(s::Union{SampleMultiAlgWRSWRSKIP, SampleMultiOrdAlgWRSWRSKIP}, el, w)
n = length(s.value)
s.seen_k += 1
s.state += w
if s.seen_k <= n
s.value[s.seen_k] = el
s.weights[s.seen_k] = w
@inbounds s.value[s.seen_k] = el
@inbounds s.weights[s.seen_k] = w
if s.seen_k == n
s.value = sample(s.rng, s.value, weights(s.weights), n; ordered = is_ordered(s))
@inline recompute_skip!(s, n)
Expand Down Expand Up @@ -201,7 +201,7 @@ end

function update_all!(s, iter, wv, ordered)
for x in iter
@inline update!(s, x, wv(x))
update!(s, x, wv(x))
end
return ordered ? ordered_value(s) : shuffle!(s.rng, value(s))
end
4 changes: 2 additions & 2 deletions src/WeightedSamplingSingle.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ function value(s::SampleSingleAlgAExpJ)
return s.value
end

function update!(s::SampleSingleAlgAExpJ, el, weight)
@inline function update!(s::SampleSingleAlgAExpJ, el, weight)
s.state += weight
if s.skip_w <= s.state
s.value = el
Expand All @@ -32,7 +32,7 @@ end
function itsample(rng::AbstractRNG, iter, wv::Function, method::ReservoirAlgorithm = algAExpJ)
s = ReservoirSample(rng, Base.@default_eltype(iter), algAExpJ)
for x in iter
@inline update!(s, x, wv(x))
update!(s, x, wv(x))
end
return value(s)
end

0 comments on commit e5fd2aa

Please sign in to comment.