Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP/RFC: Converted WeakKeyDict to hash & compare with object-id #28161

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion base/abstractdict.jl
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@ end

function ==(l::AbstractDict, r::AbstractDict)
l === r && return true
if isa(l,IdDict) != isa(r,IdDict)
if isa(l,IdDict) != isa(r,IdDict) || isa(l,WeakKeyDict) != isa(r,WeakKeyDict)
return false
end
length(l) != length(r) && return false
Expand Down
43 changes: 26 additions & 17 deletions base/weakkeydict.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@

# weak key dictionaries

# Type to wrap a WeakRef to furbish it with objectid comparison and hashing.
struct WeakRefForWeakDict
w::WeakRef
WeakRefForWeakDict(wr::WeakRef) = new(wr)
end
WeakRefForWeakDict(val) = WeakRefForWeakDict(WeakRef(val))
==(wr1::WeakRefForWeakDict, wr2::WeakRefForWeakDict) = wr1.w.value===wr2.w.value
hash(wr::WeakRefForWeakDict, h::UInt) = hash_uint(3h - objectid(wr.w.value))

"""
WeakKeyDict([itr])

Expand All @@ -12,13 +21,13 @@ referenced in a hash table.
See [`Dict`](@ref) for further help.
"""
mutable struct WeakKeyDict{K,V} <: AbstractDict{K,V}
ht::Dict{WeakRef,V}
ht::Dict{WeakRefForWeakDict,V}
lock::Threads.RecursiveSpinLock
finalizer::Function

# Constructors mirror Dict's
function WeakKeyDict{K,V}() where V where K
t = new(Dict{Any,V}(), Threads.RecursiveSpinLock(), identity)
t = new(Dict{WeakRefForWeakDict,V}(), Threads.RecursiveSpinLock(), identity)
t.finalizer = function (k)
# when a weak key is finalized, remove from dictionary if it is still there
if islocked(t)
Expand Down Expand Up @@ -75,32 +84,32 @@ lock(f, wkh::WeakKeyDict) = lock(f, wkh.lock)
trylock(f, wkh::WeakKeyDict) = trylock(f, wkh.lock)

function setindex!(wkh::WeakKeyDict{K}, v, key) where K
k = convert(K, key)
finalizer(wkh.finalizer, k)
!isa(key, K) && throw(ArgumentError("$key is not a valid key for type $K"))
finalizer(wkh.finalizer, key)
lock(wkh) do
wkh.ht[WeakRef(k)] = v
wkh.ht[WeakRefForWeakDict(key)] = v
end
return wkh
end

function getkey(wkh::WeakKeyDict{K}, kk, default) where K
return lock(wkh) do
k = getkey(wkh.ht, kk, secret_table_token)
k = getkey(wkh.ht, WeakRefForWeakDict(kk), secret_table_token)
k === secret_table_token && return default
return k.value::K
return k.w.value::K
end
end

get(wkh::WeakKeyDict{K}, key, default) where {K} = lock(() -> get(wkh.ht, key, default), wkh)
get(default::Callable, wkh::WeakKeyDict{K}, key) where {K} = lock(() -> get(default, wkh.ht, key), wkh)
get!(wkh::WeakKeyDict{K}, key, default) where {K} = lock(() -> get!(wkh.ht, key, default), wkh)
get!(default::Callable, wkh::WeakKeyDict{K}, key) where {K} = lock(() -> get!(default, wkh.ht, key), wkh)
pop!(wkh::WeakKeyDict{K}, key) where {K} = lock(() -> pop!(wkh.ht, key), wkh)
pop!(wkh::WeakKeyDict{K}, key, default) where {K} = lock(() -> pop!(wkh.ht, key, default), wkh)
delete!(wkh::WeakKeyDict, key) = lock(() -> delete!(wkh.ht, key), wkh)
get(wkh::WeakKeyDict{K}, key, default) where {K} = lock(() -> get(wkh.ht, WeakRefForWeakDict(key), default), wkh)
get(default::Callable, wkh::WeakKeyDict{K}, key) where {K} = lock(() -> get(default, wkh.ht, WeakRefForWeakDict(key)), wkh)
get!(wkh::WeakKeyDict{K}, key, default) where {K} = lock(() -> get!(wkh.ht, WeakRefForWeakDict(key), default), wkh)
get!(default::Callable, wkh::WeakKeyDict{K}, key) where {K} = lock(() -> get!(default, wkh.ht, WeakRefForWeakDict(key)), wkh)
pop!(wkh::WeakKeyDict{K}, key) where {K} = lock(() -> pop!(wkh.ht, WeakRefForWeakDict(key)), wkh)
pop!(wkh::WeakKeyDict{K}, key, default) where {K} = lock(() -> pop!(wkh.ht, WeakRefForWeakDict(key), default), wkh)
delete!(wkh::WeakKeyDict, key) = lock(() -> delete!(wkh.ht, WeakRefForWeakDict(key)), wkh)
empty!(wkh::WeakKeyDict) = (lock(() -> empty!(wkh.ht), wkh); wkh)
haskey(wkh::WeakKeyDict{K}, key) where {K} = lock(() -> haskey(wkh.ht, key), wkh)
getindex(wkh::WeakKeyDict{K}, key) where {K} = lock(() -> getindex(wkh.ht, key), wkh)
haskey(wkh::WeakKeyDict{K}, key) where {K} = lock(() -> haskey(wkh.ht, WeakRefForWeakDict(key)), wkh)
getindex(wkh::WeakKeyDict{K}, key) where {K} = lock(() -> getindex(wkh.ht, WeakRefForWeakDict(key)), wkh)
isempty(wkh::WeakKeyDict) = isempty(wkh.ht)
length(t::WeakKeyDict) = length(t.ht)

Expand All @@ -120,7 +129,7 @@ function iterate(t::WeakKeyDict{K,V}, state) where V where K
y = iterate(t.ht, tail(state)...)
y === nothing && return nothing
wkv, i = y
kv = Pair{K,V}(wkv[1].value::K, wkv[2])
kv = Pair{K,V}(wkv[1].w.value::K, wkv[2])
return (kv, (gc_token, i))
end

Expand Down
15 changes: 15 additions & 0 deletions test/dict.jl
Original file line number Diff line number Diff line change
Expand Up @@ -830,6 +830,21 @@ Dict(1 => rand(2,3), 'c' => "asdf") # just make sure this does not trigger a dep

@test_throws ArgumentError WeakKeyDict([1, 2, 3])

# WeakKeyDict does not convert keys
@test_throws ArgumentError WeakKeyDict{Int,Any}(5.0=>1)

# WeakKeyDict hashes with object-id
AA = copy(A)
Copy link
Sponsor Member

@vtjnash vtjnash Jul 18, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GC.@preserve A AA begin
...
end

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, fixed! Quesion: why would they need to be preserved? The do not go out of scope and thus should not be GC'ed, no?

Copy link
Sponsor Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A is used subsequently so is relatively safe but AA isn't so it could be garbage collected.

wkd = WeakKeyDict(A=>1, AA=>2)
@test length(wkd)==2
kk = collect(keys(wkd))
@test kk[1]==kk[2]
@test kk[1]!==kk[2]

# WeakKeyDict compares false to non-WeakKeyDict
@test IdDict(A=>1)!=WeakKeyDict(A=>1)
@test Dict(A=>1)!=WeakKeyDict(A=>1)

# issue #26939
d26939 = WeakKeyDict()
d26939[big"1.0" + 1.1] = 1
Expand Down