Skip to content

Commit

Permalink
Revert "Enable shrinkage of Dict (#45004)"
Browse files Browse the repository at this point in the history
This reverts commit 8c61f40.
  • Loading branch information
DilumAluthge authored May 9, 2022
1 parent f9bd142 commit 1c27f04
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 11 deletions.
2 changes: 1 addition & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ Library changes
tasks mutating the dictionary or set ([#44534]).
* Predicate function negation `!f` now returns a composed function `(!) ∘ f` instead of an anonymous function ([#44752]).
* `RoundFromZero` now works for non-`BigFloat` types ([#41246]).
* `Dict` can be now shrunk manually by `sizehint!` ([#45004]).
* `@time` now separates out % time spent recompiling invalidated methods ([#45015]).
* `@time_imports` now shows any compilation and recompilation time percentages per import ([#45064]).


Standard library changes
------------------------

Expand Down
12 changes: 9 additions & 3 deletions base/dict.jl
Original file line number Diff line number Diff line change
Expand Up @@ -240,10 +240,16 @@ end
function sizehint!(d::Dict{T}, newsz) where T
oldsz = length(d.slots)
# limit new element count to max_values of the key type
newsz = min(max(newsz, length(d)), max_values(T)::Int)
newsz = min(newsz, max_values(T)::Int)
# need at least 1.5n space to hold n elements
newsz = _tablesz(cld(3 * newsz, 2))
return newsz == oldsz ? d : rehash!(d, newsz)
newsz = cld(3 * newsz, 2)
if newsz <= oldsz
# todo: shrink
# be careful: rehash!() assumes everything fits. it was only designed
# for growing.
return d
end
rehash!(d, newsz)
end

"""
Expand Down
7 changes: 0 additions & 7 deletions test/dict.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1252,10 +1252,3 @@ end
let c = bar()
@test c === missing || c == ComparesWithGC38727(1)
end

@testset "shrinking" begin
d = Dict(i => i for i = 1:1000)
filter!(x -> x.first < 10, d)
sizehint!(d, 10)
@test length(d.slots) < 100
end

0 comments on commit 1c27f04

Please sign in to comment.