Skip to content

Commit afe3a87

Browse files
vtjnashHans-Peter Suter
authored andcommitted
remove gc-token from WeakKeyDict (#33825)
This was a hold-over from the old iteration protocol, which needed to maintain state between `done` and `next`. The `iteration` function of `Dict` has since been re-written to be safe for concurrent deletions. Replaces: #33756 Co-Authored-By: Hans-Peter Suter <[email protected]> (cherry picked from commit 1731d0a)
1 parent 281225d commit afe3a87

File tree

1 file changed

+5
-17
lines changed

1 file changed

+5
-17
lines changed

base/weakkeydict.jl

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
WeakKeyDict([itr])
77
88
`WeakKeyDict()` constructs a hash table where the keys are weak
9-
references to objects, and thus may be garbage collected even when
9+
references to objects which may be garbage collected even when
1010
referenced in a hash table.
1111
1212
See [`Dict`](@ref) for further help. Note, unlike [`Dict`](@ref),
@@ -111,24 +111,12 @@ getindex(wkh::WeakKeyDict{K}, key) where {K} = lock(() -> getindex(wkh.ht, key),
111111
isempty(wkh::WeakKeyDict) = isempty(wkh.ht)
112112
length(t::WeakKeyDict) = length(t.ht)
113113

114-
function iterate(t::WeakKeyDict{K,V}) where V where K
115-
gc_token = Ref{Bool}(false) # no keys will be deleted via finalizers until this token is gc'd
116-
finalizer(gc_token) do r
117-
if r[]
118-
r[] = false
119-
unlock(t.lock)
120-
end
121-
end
122-
s = lock(t.lock)
123-
iterate(t, (gc_token,))
124-
end
125-
function iterate(t::WeakKeyDict{K,V}, state) where V where K
126-
gc_token = first(state)
127-
y = iterate(t.ht, tail(state)...)
114+
function iterate(t::WeakKeyDict{K,V}, state...) where {K, V}
115+
y = lock(() -> iterate(t.ht, state...), t)
128116
y === nothing && return nothing
129-
wkv, i = y
117+
wkv, newstate = y
130118
kv = Pair{K,V}(wkv[1].value::K, wkv[2])
131-
return (kv, (gc_token, i))
119+
return (kv, newstate)
132120
end
133121

134122
filter!(f, d::WeakKeyDict) = filter_in_one_pass!(f, d)

0 commit comments

Comments
 (0)