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

RFC: Fix rehash check #74

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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 src/ordered_dict.jl
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ function _setindex!(h::OrderedDict, v, key, index)
sz = length(h.slots)
cnt = nk - h.ndel
# Rehash now if necessary
if h.ndel >= ((3*nk)>>2) || cnt*3 > sz*2
if h.ndel >= ((3*sz)>>2) || cnt*3 > sz*2
Copy link
Member Author

Choose a reason for hiding this comment

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

Previously, when nk was less than 4, we would rehash every time. This was probably unintended.

The fix here mimics Base.Dict, but the implementation there is different--keys are stored in an array the same shape/length as slots, whereas here, the keys are stored in a shorter vector that, after rehash, is only as long as the number of keys. (nk here is the number of keys before rehash--i.e., it includes deleted keys.)

This might suggest that the strategy should be different. An alternative could be, e.g.

Suggested change
if h.ndel >= ((3*sz)>>2) || cnt*3 > sz*2
if h.ndel >= ((3*nk)>>2) >= 3 || cnt*3 > sz*2

which basically requires at least 4 keys before anything happens.

# > 3/4 deleted or > 2/3 full
rehash!(h, cnt > 64000 ? cnt*2 : cnt*4)
end
Expand Down
33 changes: 33 additions & 0 deletions test/test_ordered_dict.jl
Original file line number Diff line number Diff line change
Expand Up @@ -450,4 +450,37 @@ using OrderedCollections, Test
@test od[14] == 14
end

@testset "Issue #65" begin
x = OrderedDict{OrderedDict, Int}()
x[x] = 0 # There's no reason to ever do this, but it shouldn't overflow the stack
@test length(deepcopy(x)) == 1

# Test that a rehash isn't triggered during setindex! with only a few keys added/removed.
# It should only be triggered when a count equal to 3/4 of the slots have been removed.
# With the smallest size dict, that would be 12/16
od = OrderedDict{Int,Int}(i=>i for i in 1:5)
for i in 1:4
pop!(od, i)
end
del_slots1 = sum(od.slots .< 0)
od[6] = 6
del_slots2 = sum(od.slots .< 0)
@test del_slots1 - 1 <= del_slots2 <= del_slots1

for i in 7:14
od[i] = i
end
for i in 5:13
pop!(od, i)
end
del_slots3 = sum(od.slots .< 0)
# Some of the previously deleted slots might have been reused, but we should at
# least see deleted slots for items 5 through 13
@test del_slots3 >= 9
# We've now removed 13 items, so the next assignment should trigger a rehash
od[15] = 15
del_slots4 = sum(od.slots .< 0)
@test del_slots4 == 0
end

end # @testset OrderedDict