Skip to content

Commit

Permalink
Fix Hash#rehash to reset @first (#14606)
Browse files Browse the repository at this point in the history
  • Loading branch information
straight-shoota committed Jun 11, 2024
1 parent 2d71e35 commit 4c1f81b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
30 changes: 20 additions & 10 deletions spec/std/hash_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -1393,16 +1393,26 @@ describe "Hash" do
hash.@indices_size_pow2.should eq(12)
end

it "rehashes" do
a = [1]
h = {a => 0}
(10..100).each do |i|
h[[i]] = i
end
a << 2
h[a]?.should be_nil
h.rehash
h[a].should eq(0)
describe "#rehash" do
it "rehashes" do
a = [1]
h = {a => 0}
(10..100).each do |i|
h[[i]] = i
end
a << 2
h[a]?.should be_nil
h.rehash
h[a].should eq(0)
end

it "resets @first (#14602)" do
h = {"a" => 1, "b" => 2}
h.delete("a")
h.rehash
# We cannot test direct equivalence here because `Hash#==(Hash)` does not depend on `@first`
h.to_s.should eq %({"b" => 2})
end
end

describe "some edge cases while changing the implementation to open addressing" do
Expand Down
3 changes: 3 additions & 0 deletions src/hash.cr
Original file line number Diff line number Diff line change
Expand Up @@ -631,6 +631,9 @@ class Hash(K, V)
new_entry_index += 1
end

# Reset offset to first non-deleted entry
@first = 0

# We have to mark entries starting from the final new index
# as deleted so the GC can collect them.
entries_to_clear = entries_size - new_entry_index
Expand Down

0 comments on commit 4c1f81b

Please sign in to comment.