Skip to content

Commit

Permalink
Merge pull request #60 from jakobnissen/hashcheck
Browse files Browse the repository at this point in the history
Check hashes before values when getting Indices token
  • Loading branch information
andyferris authored Aug 15, 2021
2 parents 985459b + d444944 commit 0462e92
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions src/Indices.jl
Original file line number Diff line number Diff line change
Expand Up @@ -313,16 +313,19 @@ function gettoken(indices::Indices{I}, i::I) where {I}
full_hash = hash(i) & hash_mask
n_slots = length(_slots(indices))
bit_mask = n_slots - 1 # n_slots is always a power of two
hashes = indices.hashes

trial_slot = reinterpret(Int, full_hash) & bit_mask
@inbounds while true
trial_slot = (trial_slot + 1)
trial_index = _slots(indices)[trial_slot]
if trial_index > 0
value = _values(indices)[trial_index]
if i === value || isequal(i, value)
return (true, (trial_slot, trial_index))
end
if isbitstype(I) || Base.isbitsunion(I) || I === Symbol || full_hash === @inbounds hashes[trial_index]
value = _values(indices)[trial_index]
if i === value || isequal(i, value)
return (true, (trial_slot, trial_index))
end
end
elseif trial_index === 0
return (false, (0, 0))
end
Expand All @@ -349,6 +352,7 @@ function gettoken!(indices::Indices{I}, i::I, values = ()) where {I}
n_slots = length(_slots(indices))
bit_mask = n_slots - 1 # n_slots is always a power of two
n_values = length(_values(indices))
hashes = indices.hashes

trial_slot = reinterpret(Int, full_hash) & bit_mask
trial_index = 0
Expand All @@ -363,9 +367,11 @@ function gettoken!(indices::Indices{I}, i::I, values = ()) where {I}
deleted_slot = trial_slot
end
else
value = _values(indices)[trial_index]
if i === value || isequal(i, value)
return (true, (trial_slot, trial_index))
if isbitstype(I) || Base.isbitsunion(I) || I === Symbol || full_hash === @inbounds hashes[trial_index]
value = _values(indices)[trial_index]
if i === value || isequal(i, value)
return (true, (trial_slot, trial_index))
end
end
end

Expand Down Expand Up @@ -525,4 +531,4 @@ function randtoken(rng::Random.AbstractRNG, inds::Indices)
return (0, i)
end
end
end
end

0 comments on commit 0462e92

Please sign in to comment.