Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use a ShrinkingMap inside the OrderedMap to avoid the map leaking memory #616

Merged
merged 1 commit into from
Nov 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions ds/orderedmap/orderedmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"sync"

"github.com/iotaledger/hive.go/ds/shrinkingmap"
"github.com/iotaledger/hive.go/ierrors"
"github.com/iotaledger/hive.go/serializer/v2"
"github.com/iotaledger/hive.go/serializer/v2/serix"
Expand All @@ -13,7 +14,7 @@ import (
type OrderedMap[K comparable, V any] struct {
head *Element[K, V]
tail *Element[K, V]
dictionary map[K]*Element[K, V]
dictionary *shrinkingmap.ShrinkingMap[K, *Element[K, V]]
size int
mutex sync.RWMutex
}
Expand All @@ -30,7 +31,8 @@ func New[K comparable, V any]() *OrderedMap[K, V] {
func (o *OrderedMap[K, V]) Initialize() {
o.mutex.Lock()
defer o.mutex.Unlock()
o.dictionary = make(map[K]*Element[K, V])

o.dictionary = shrinkingmap.New[K, *Element[K, V]]()
}

// Head returns the first map entry.
Expand Down Expand Up @@ -66,17 +68,15 @@ func (o *OrderedMap[K, V]) Has(key K) (has bool) {
o.mutex.RLock()
defer o.mutex.RUnlock()

_, has = o.dictionary[key]

return
return o.dictionary.Has(key)
}

// Get returns the value mapped to the given key if exists.
func (o *OrderedMap[K, V]) Get(key K) (value V, exists bool) {
o.mutex.RLock()
defer o.mutex.RUnlock()

orderedMapElement, orderedMapElementExists := o.dictionary[key]
orderedMapElement, orderedMapElementExists := o.dictionary.Get(key)
if !orderedMapElementExists {
var result V
return result, false
Expand All @@ -90,7 +90,7 @@ func (o *OrderedMap[K, V]) Set(key K, newValue V) (previousValue V, previousValu
o.mutex.Lock()
defer o.mutex.Unlock()

if oldValue, oldValueExists := o.dictionary[key]; oldValueExists {
if oldValue, oldValueExists := o.dictionary.Get(key); oldValueExists {
previousValue = oldValue.value
oldValue.value = newValue

Expand All @@ -110,7 +110,7 @@ func (o *OrderedMap[K, V]) Set(key K, newValue V) (previousValue V, previousValu
o.tail = newElement
o.size++

o.dictionary[key] = newElement
o.dictionary.Set(key, newElement)

return previousValue, false
}
Expand Down Expand Up @@ -171,7 +171,7 @@ func (o *OrderedMap[K, V]) Clear() {
o.head = nil
o.tail = nil
o.size = 0
o.dictionary = make(map[K]*Element[K, V])
o.dictionary = shrinkingmap.New[K, *Element[K, V]]()
}

// Delete deletes the given key (and related value) from the orderedMap.
Expand All @@ -184,12 +184,12 @@ func (o *OrderedMap[K, V]) Delete(key K) bool {
o.mutex.Lock()
defer o.mutex.Unlock()

value, valueExists := o.dictionary[key]
value, valueExists := o.dictionary.Get(key)
if !valueExists {
return false
}

delete(o.dictionary, key)
o.dictionary.Delete(key)
o.size--

if value.prev != nil {
Expand Down
16 changes: 15 additions & 1 deletion ds/orderedmap/orderedmap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,22 @@ func TestOrderedMap_Size(t *testing.T) {

require.Equal(t, 2, orderedMap.Size())

expectedMap := map[int]int{}
orderedMap.ForEach(func(key int, value int) bool {
expectedMap[key] = value
return true
})

clone := orderedMap.Clone()
require.EqualValues(t, orderedMap, clone)

clonedMap := map[int]int{}
clone.ForEach(func(key int, value int) bool {
clonedMap[key] = value
return true
})

// We compare the maps because EqualValues does a deep compare and the original has a deleted items count inside the ShrinkingMap
require.EqualValues(t, expectedMap, clonedMap)

clone.Clear()
require.True(t, clone.IsEmpty())
Expand Down
Loading