Skip to content

Commit

Permalink
Avoid allocating new list entries (#2910)
Browse files Browse the repository at this point in the history
  • Loading branch information
StephenButtolph authored Apr 3, 2024
1 parent d3a74eb commit 0fea82e
Showing 1 changed file with 19 additions and 8 deletions.
27 changes: 19 additions & 8 deletions utils/linked/hashmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type Hashmap[K comparable, V any] struct {
lock sync.RWMutex
entryMap map[K]*ListElement[keyValue[K, V]]
entryList *List[keyValue[K, V]]
freeList []*ListElement[keyValue[K, V]]
}

func NewHashmap[K comparable, V any]() *Hashmap[K, V] {
Expand Down Expand Up @@ -79,16 +80,24 @@ func (lh *Hashmap[K, V]) put(key K, value V) {
key: key,
value: value,
}
return
}

var e *ListElement[keyValue[K, V]]
if numFree := len(lh.freeList); numFree > 0 {
numFree--
e = lh.freeList[numFree]
lh.freeList = lh.freeList[:numFree]
} else {
e = &ListElement[keyValue[K, V]]{
Value: keyValue[K, V]{
key: key,
value: value,
},
}
lh.entryMap[key] = e
lh.entryList.PushBack(e)
e = &ListElement[keyValue[K, V]]{}
}

e.Value = keyValue[K, V]{
key: key,
value: value,
}
lh.entryMap[key] = e
lh.entryList.PushBack(e)
}

func (lh *Hashmap[K, V]) get(key K) (V, bool) {
Expand All @@ -103,6 +112,8 @@ func (lh *Hashmap[K, V]) delete(key K) bool {
if ok {
lh.entryList.Remove(e)
delete(lh.entryMap, key)
e.Value = keyValue[K, V]{} // Free the key value pair
lh.freeList = append(lh.freeList, e)
}
return ok
}
Expand Down

0 comments on commit 0fea82e

Please sign in to comment.