Skip to content

Commit fa766bc

Browse files
chethegaJeffBezanson
authored andcommitted
Speed up filtering of dicts by skipping unneeded hash evaluations (#31242)
1 parent 90fc054 commit fa766bc

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

base/dict.jl

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -689,7 +689,15 @@ length(t::Dict) = t.count
689689
(@inbounds vals[i], i == typemax(Int) ? 0 : i+1)
690690
end
691691

692-
filter!(f, d::Dict) = filter_in_one_pass!(f, d)
692+
function filter!(pred, h::Dict{K,V}) where {K,V}
693+
h.count == 0 && return h
694+
@inbounds for i=1:length(h.slots)
695+
if h.slots[i] == 0x01 && !pred(Pair{K,V}(h.keys[i], h.vals[i]))
696+
_delete!(h, i)
697+
end
698+
end
699+
return h
700+
end
693701

694702
function reduce(::typeof(merge), items::Vector{<:Dict})
695703
K = mapreduce(keytype, promote_type, items)

0 commit comments

Comments
 (0)