Skip to content

Commit 1e95bf8

Browse files
oscardssmithKristofferC
authored andcommitted
reset maxprobe on empty! (#51595)
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)
1 parent 674f440 commit 1e95bf8

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

base/dict.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ end
176176
resize!(h.keys, newsz)
177177
resize!(h.vals, newsz)
178178
h.ndel = 0
179+
h.maxprobe = 0
179180
return h
180181
end
181182

@@ -251,6 +252,7 @@ function empty!(h::Dict{K,V}) where V where K
251252
resize!(h.vals, sz)
252253
h.ndel = 0
253254
h.count = 0
255+
h.maxprobe = 0
254256
h.age += 1
255257
h.idxfloor = sz
256258
return h

test/dict.jl

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1371,3 +1371,24 @@ for T in (Int, Float64, String, Symbol)
13711371
@test !Core.Compiler.is_nothrow(Base.infer_effects(getindex, (Dict{T,Any}, T)))
13721372
@test Core.Compiler.is_terminates(Base.infer_effects(getindex, (Dict{T,Any}, T)))
13731373
end
1374+
1375+
struct BadHash
1376+
i::Int
1377+
end
1378+
Base.hash(::BadHash, ::UInt)=UInt(1)
1379+
@testset "maxprobe reset #51595" begin
1380+
d = Dict(BadHash(i)=>nothing for i in 1:20)
1381+
empty!(d)
1382+
sizehint!(d, 0)
1383+
@test d.maxprobe < length(d.keys)
1384+
d[BadHash(1)]=nothing
1385+
@test !(BadHash(2) in keys(d))
1386+
d = Dict(BadHash(i)=>nothing for i in 1:20)
1387+
for _ in 1:20
1388+
pop!(d)
1389+
end
1390+
sizehint!(d, 0)
1391+
@test d.maxprobe < length(d.keys)
1392+
d[BadHash(1)]=nothing
1393+
@test !(BadHash(2) in keys(d))
1394+
end

0 commit comments

Comments
 (0)