Skip to content

Commit e1e3a34

Browse files
committed
Use empty struct in set values
* Reduces memory footprint
1 parent 6f61683 commit e1e3a34

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

queue/priority_queue.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ func (items *priorityItems) push(item Item) {
107107
type PriorityQueue struct {
108108
waiters waiters
109109
items priorityItems
110-
itemMap map[Item]bool
110+
itemMap map[Item]struct{}
111111
lock sync.Mutex
112112
disposeLock sync.Mutex
113113
disposed bool
@@ -126,8 +126,8 @@ func (pq *PriorityQueue) Put(items ...Item) error {
126126
}
127127

128128
for _, item := range items {
129-
if ok := pq.itemMap[item]; !ok {
130-
pq.itemMap[item] = true
129+
if _, ok := pq.itemMap[item]; !ok {
130+
pq.itemMap[item] = struct{}{}
131131
pq.items.push(item)
132132
}
133133
}
@@ -170,7 +170,7 @@ func (pq *PriorityQueue) Get(number int) ([]Item, error) {
170170
// Remove references to popped items.
171171
deleteItems := func(items []Item) {
172172
for _, item := range items {
173-
pq.itemMap[item] = false
173+
delete(pq.itemMap, item)
174174
}
175175
}
176176

@@ -257,6 +257,6 @@ func (pq *PriorityQueue) Dispose() {
257257
func NewPriorityQueue(hint int) *PriorityQueue {
258258
return &PriorityQueue{
259259
items: make(priorityItems, 0, hint),
260-
itemMap: make(map[Item]bool, hint),
260+
itemMap: make(map[Item]struct{}, hint),
261261
}
262262
}

0 commit comments

Comments
 (0)