Skip to content

Commit 4a1d0bb

Browse files
Allow pre-allocating linked.Hashmap (#2918)
1 parent 88d304c commit 4a1d0bb

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

utils/linked/hashmap.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,19 @@ type Hashmap[K comparable, V any] struct {
2020
}
2121

2222
func NewHashmap[K comparable, V any]() *Hashmap[K, V] {
23-
return &Hashmap[K, V]{
24-
entryMap: make(map[K]*ListElement[keyValue[K, V]]),
23+
return NewHashmapWithSize[K, V](0)
24+
}
25+
26+
func NewHashmapWithSize[K comparable, V any](initialSize int) *Hashmap[K, V] {
27+
lh := &Hashmap[K, V]{
28+
entryMap: make(map[K]*ListElement[keyValue[K, V]], initialSize),
2529
entryList: NewList[keyValue[K, V]](),
30+
freeList: make([]*ListElement[keyValue[K, V]], initialSize),
31+
}
32+
for i := range lh.freeList {
33+
lh.freeList[i] = &ListElement[keyValue[K, V]]{}
2634
}
35+
return lh
2736
}
2837

2938
func (lh *Hashmap[K, V]) Put(key K, value V) {

0 commit comments

Comments
 (0)