Skip to content
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

Faster iterating over (almost) empty Dict or Set #44411

Merged
merged 2 commits into from
May 10, 2022

Conversation

petvana
Copy link
Member

@petvana petvana commented Mar 2, 2022

It is possible to utilize the existing code 0e12723 defining idxfloor to improve performance of iterating over (almost) empty Dict/Set.

However, this PR cannot solve asymptotic complexity because the current implementation cannot shrink Dict so far (there is a todo for that).

julia/base/dict.jl

Lines 238 to 243 in b10aa56

if newsz <= oldsz
# todo: shrink
# be careful: rehash!() assumes everything fits. it was only designed
# for growing.
return d
end
.

Master

julia> using BenchmarkTools

julia> d = Set{Int}();
julia> @btime minimum(d, init = 0);
  20.737 ns (0 allocations: 0 bytes)

julia> d = Set([1]);
julia> @btime minimum(d, init = 0);
  21.904 ns (0 allocations: 0 bytes)

julia> d = Set(1:10_000); empty!(d);
julia> @btime minimum(d, init = 0);
  6.581 μs (0 allocations: 0 bytes)

julia> d = Set(1:10_000); empty!(d); push!(d,1);
julia> @btime minimum(d, init = 0);
  6.665 μs (0 allocations: 0 bytes)

PR

julia> using BenchmarkTools

julia> d = Set{Int}();
julia> @btime minimum(d, init = 0);
  14.968 ns (0 allocations: 0 bytes)

julia> d = Set([1]);
julia> @btime minimum(d, init = 0);
  16.615 ns (0 allocations: 0 bytes)

julia> d = Set(1:10_000); empty!(d);
julia> @btime minimum(d, init = 0);
  14.933 ns (0 allocations: 0 bytes)

julia> d = Set(1:10_000); empty!(d); push!(d,1);
julia> @btime minimum(d, init = 0);
  821.388 ns (0 allocations: 0 bytes)

@petvana petvana marked this pull request as ready for review March 2, 2022 20:30
@KristofferC KristofferC merged commit 0ba4e71 into JuliaLang:master May 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants