Skip to content

Commit

Permalink
Improve backward compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
petvana committed Mar 12, 2022
1 parent aca0272 commit 8fd9617
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 10 deletions.
19 changes: 11 additions & 8 deletions base/dict.jl
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ end
# and the key would be inserted at pos
# sh::UInt8 - short hash (7 highest hash bits)
# This version is for use by setindex! and get!
function ht_keyindex2!(h::Dict{K,V}, key) where V where K
function ht_keyindex2_shorthash!(h::Dict{K,V}, key) where V where K
sz = length(h.keys)
iter = 0
maxprobe = h.maxprobe
Expand Down Expand Up @@ -364,10 +364,13 @@ function ht_keyindex2!(h::Dict{K,V}, key) where V where K

rehash!(h, h.count > 64000 ? sz*2 : sz*4)

return ht_keyindex2!(h, key)
return ht_keyindex2_shorthash!(h, key)
end

@propagate_inbounds function _setindex!(h::Dict, v, key, index, sh)
# Only for better backward compatibility. It can be removed in the future.
ht_keyindex2!(h::Dict, key) = ht_keyindex2_shorthash!(h, key)[1]

@propagate_inbounds function _setindex!(h::Dict, v, key, index, sh = _shorthash7(hash(key)))
h.slots[index] = sh
h.keys[index] = key
h.vals[index] = v
Expand Down Expand Up @@ -396,7 +399,7 @@ end

function setindex!(h::Dict{K,V}, v0, key::K) where V where K
v = convert(V, v0)
index, sh = ht_keyindex2!(h, key)
index, sh = ht_keyindex2_shorthash!(h, key)

if index > 0
h.age += 1
Expand All @@ -411,7 +414,7 @@ end

function setindex!(h::Dict{K,Any}, v, key::K) where K
@nospecialize v
index, sh = ht_keyindex2!(h, key)
index, sh = ht_keyindex2_shorthash!(h, key)

if index > 0
h.age += 1
Expand Down Expand Up @@ -489,14 +492,14 @@ function get!(default::Callable, h::Dict{K,V}, key0) where V where K
end

function get!(default::Callable, h::Dict{K,V}, key::K) where V where K
index, sh = ht_keyindex2!(h, key)
index, sh = ht_keyindex2_shorthash!(h, key)

index > 0 && return h.vals[index]

age0 = h.age
v = convert(V, default())
if h.age != age0
index, sh = ht_keyindex2!(h, key)
index, sh = ht_keyindex2_shorthash!(h, key)
end
if index > 0
h.age += 1
Expand Down Expand Up @@ -762,7 +765,7 @@ end

function mergewith!(combine, d1::Dict{K, V}, d2::AbstractDict) where {K, V}
for (k, v) in d2
i, sh = ht_keyindex2!(d1, k)
i, sh = ht_keyindex2_shorthash!(d1, k)
if i > 0
d1.vals[i] = combine(d1.vals[i], v)
else
Expand Down
4 changes: 2 additions & 2 deletions base/set.jl
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ function allunique(C)
if haslength(C) && length(C) > 1000
for i in OneTo(1000)
v, s = x
idx, sh = ht_keyindex2!(seen, v)
idx, sh = ht_keyindex2_shorthash!(seen, v)
idx > 0 && return false
_setindex!(seen, nothing, v, -idx, sh)
x = iterate(C, s)
Expand All @@ -415,7 +415,7 @@ function allunique(C)
end
while x !== nothing
v, s = x
idx, sh = ht_keyindex2!(seen, v)
idx, sh = ht_keyindex2_shorthash!(seen, v)
idx > 0 && return false
_setindex!(seen, nothing, v, -idx, sh)
x = iterate(C, s)
Expand Down

0 comments on commit 8fd9617

Please sign in to comment.