Skip to content

Commit d41ce81

Browse files
authored
Merge pull request #38 from semihbkgr/fix-fillrate
Fixing fillrate value
2 parents aee7ca3 + e31466c commit d41ce81

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

e2e_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,19 @@ func TestGrow2(t *testing.T) {
130130
}
131131
}
132132

133+
func TestFillrate(t *testing.T) {
134+
m := New[int, any]()
135+
for i := 0; i < 1000; i++ {
136+
m.Set(i, nil)
137+
}
138+
for i := 0; i < 1000; i++ {
139+
m.Del(i)
140+
}
141+
if fr := m.Fillrate(); fr != 0 {
142+
t.Errorf("Fillrate should be zero when the map is empty, fillrate: %v", fr)
143+
}
144+
}
145+
133146
func TestDelete(t *testing.T) {
134147
m := New[int, *Animal]()
135148
cat := &Animal{"cat"}

map.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -420,14 +420,14 @@ func (m *Map[K, V]) removeItemFromIndex(item *element[K, V]) {
420420
ptr := (*unsafe.Pointer)(unsafe.Pointer(uintptr(data.data) + index*intSizeBytes))
421421

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

428428
if data == m.metadata.Load() { // check that no resize happened
429429
m.numItems.Add(^uintptr(0)) // decrement counter
430-
if next == nil {
430+
if swappedToNil { // decrement the metadata count if the index is set to nil
431431
data.count.Add(^uintptr(0))
432432
}
433433
return

0 commit comments

Comments
 (0)