Skip to content

Commit

Permalink
Merge pull request #38 from semihbkgr/fix-fillrate
Browse files Browse the repository at this point in the history
Fixing fillrate value
  • Loading branch information
alphadose authored Feb 19, 2023
2 parents aee7ca3 + e31466c commit d41ce81
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
13 changes: 13 additions & 0 deletions e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,19 @@ func TestGrow2(t *testing.T) {
}
}

func TestFillrate(t *testing.T) {
m := New[int, any]()
for i := 0; i < 1000; i++ {
m.Set(i, nil)
}
for i := 0; i < 1000; i++ {
m.Del(i)
}
if fr := m.Fillrate(); fr != 0 {
t.Errorf("Fillrate should be zero when the map is empty, fillrate: %v", fr)
}
}

func TestDelete(t *testing.T) {
m := New[int, *Animal]()
cat := &Animal{"cat"}
Expand Down
6 changes: 3 additions & 3 deletions map.go
Original file line number Diff line number Diff line change
Expand Up @@ -420,14 +420,14 @@ func (m *Map[K, V]) removeItemFromIndex(item *element[K, V]) {
ptr := (*unsafe.Pointer)(unsafe.Pointer(uintptr(data.data) + index*intSizeBytes))

next := item.next()
if next != nil && item.keyHash>>data.keyshifts != index {
if next != nil && next.keyHash>>data.keyshifts != index {
next = nil // do not set index to next item if it's not the same slice index
}
atomic.CompareAndSwapPointer(ptr, unsafe.Pointer(item), unsafe.Pointer(next))
swappedToNil := atomic.CompareAndSwapPointer(ptr, unsafe.Pointer(item), unsafe.Pointer(next)) && next == nil

if data == m.metadata.Load() { // check that no resize happened
m.numItems.Add(^uintptr(0)) // decrement counter
if next == nil {
if swappedToNil { // decrement the metadata count if the index is set to nil
data.count.Add(^uintptr(0))
}
return
Expand Down

0 comments on commit d41ce81

Please sign in to comment.