diff --git a/base/dict.jl b/base/dict.jl index 77272acf98aff..bdd1591d731cc 100644 --- a/base/dict.jl +++ b/base/dict.jl @@ -184,6 +184,7 @@ end resize!(h.keys, newsz) resize!(h.vals, newsz) h.ndel = 0 + h.maxprobe = 0 return h end @@ -259,6 +260,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 diff --git a/test/dict.jl b/test/dict.jl index 37067c304bb62..fabaf1758f1bd 100644 --- a/test/dict.jl +++ b/test/dict.jl @@ -1502,3 +1502,24 @@ for T in (Int, Float64, String, Symbol) @test_broken Core.Compiler.is_terminates(Base.infer_effects(getindex, (Dict{T,Any}, T))) end 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