Skip to content

Commit

Permalink
fix Dict sizehint! to allow enough space for the requested length (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffBezanson committed Aug 31, 2020
1 parent 35ac635 commit 2154472
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions base/dict.jl
Expand Up @@ -231,15 +231,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(newsz, max_values(T)::Int)
# need at least 1.5n space to hold n elements
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
# grow at least 25%
newsz = min(max(newsz, (oldsz*5)>>2),
max_values(T)::Int)
rehash!(d, newsz)
end

Expand Down

0 comments on commit 2154472

Please sign in to comment.