Skip to content

Commit

Permalink
fix infinite for loop bug in Set() operation
Browse files Browse the repository at this point in the history
  • Loading branch information
alphadose committed Sep 14, 2022
1 parent f3e3c3f commit c46b187
Showing 1 changed file with 17 additions and 20 deletions.
37 changes: 17 additions & 20 deletions map.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,25 +142,24 @@ func (m *HashMap[K, V]) Set(key K, value V) {
alloc *element[K, V]
created = false
)
for {
data := m.Datamap.Load()
if data == nil {
m.Grow(defaultSize)
continue // read mapdata and slice item again
}
existing := data.indexElement(h)
if existing == nil {
existing = m.listHead
}
if alloc, created = existing.inject(h, key, valPtr); created {
m.numItems.Add(1)
}

count := data.addItemToIndex(alloc)
if resizeNeeded(uintptr(len(data.index)), count) && m.resizing.CompareAndSwap(notResizing, resizingInProgress) {
m.grow(0, true)
}
return
start:
data := m.Datamap.Load()
if data == nil {
m.Grow(defaultSize)
goto start // read mapdata and slice item again
}
existing := data.indexElement(h)
if existing == nil || existing.keyHash > h {
existing = m.listHead
}
if alloc, created = existing.inject(h, key, valPtr); created {
m.numItems.Add(1)
}

count := data.addItemToIndex(alloc)
if resizeNeeded(uintptr(len(data.index)), count) && m.resizing.CompareAndSwap(notResizing, resizingInProgress) {
m.grow(0, true)
}
}

Expand Down Expand Up @@ -273,8 +272,6 @@ func (m *HashMap[K, V]) grow(newSize uintptr, loop bool) {

m.Datamap.Store(newdata)

m.fillIndexItems(newdata) // re-index once again just to be safe

if !loop {
return
}
Expand Down

0 comments on commit c46b187

Please sign in to comment.