Skip to content

Commit

Permalink
reset maxprobe on empty! (#51595)
Browse files Browse the repository at this point in the history
As pointed out in
#51594 (comment),
this is necessary for the assertion added in
#49447 to be valid.

Fix #51594

(cherry picked from commit c18e485)
  • Loading branch information
oscardssmith authored and KristofferC committed Oct 9, 2023
1 parent 674f440 commit 1e95bf8
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
2 changes: 2 additions & 0 deletions base/dict.jl
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ end
resize!(h.keys, newsz)
resize!(h.vals, newsz)
h.ndel = 0
h.maxprobe = 0
return h
end

Expand Down Expand Up @@ -251,6 +252,7 @@ function empty!(h::Dict{K,V}) where V where K
resize!(h.vals, sz)
h.ndel = 0
h.count = 0
h.maxprobe = 0
h.age += 1
h.idxfloor = sz
return h
Expand Down
21 changes: 21 additions & 0 deletions test/dict.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1371,3 +1371,24 @@ for T in (Int, Float64, String, Symbol)
@test !Core.Compiler.is_nothrow(Base.infer_effects(getindex, (Dict{T,Any}, T)))
@test Core.Compiler.is_terminates(Base.infer_effects(getindex, (Dict{T,Any}, T)))
end

struct BadHash
i::Int
end
Base.hash(::BadHash, ::UInt)=UInt(1)
@testset "maxprobe reset #51595" begin
d = Dict(BadHash(i)=>nothing for i in 1:20)
empty!(d)
sizehint!(d, 0)
@test d.maxprobe < length(d.keys)
d[BadHash(1)]=nothing
@test !(BadHash(2) in keys(d))
d = Dict(BadHash(i)=>nothing for i in 1:20)
for _ in 1:20
pop!(d)
end
sizehint!(d, 0)
@test d.maxprobe < length(d.keys)
d[BadHash(1)]=nothing
@test !(BadHash(2) in keys(d))
end

0 comments on commit 1e95bf8

Please sign in to comment.