Skip to content

Commit

Permalink
performance: Initialize capacity on copy (#51)
Browse files Browse the repository at this point in the history
  • Loading branch information
semihbkgr authored Dec 28, 2024
1 parent cc923df commit 5750fe0
Showing 3 changed files with 5 additions and 9 deletions.
2 changes: 1 addition & 1 deletion orderedmap.go
Original file line number Diff line number Diff line change
@@ -109,7 +109,7 @@ func (m *OrderedMap) Back() *Element {
// Copy returns a new OrderedMap with the same elements.
// Using Copy while there are concurrent writes may mangle the result.
func (m *OrderedMap) Copy() *OrderedMap {
m2 := NewOrderedMap()
m2 := NewOrderedMapWithCapacity(m.Len())

for el := m.Front(); el != nil; el = el.Next() {
m2.Set(el.Key, el.Value)
6 changes: 2 additions & 4 deletions v2/orderedmap.go
Original file line number Diff line number Diff line change
@@ -20,9 +20,7 @@ func NewOrderedMapWithCapacity[K comparable, V any](capacity int) *OrderedMap[K,
}

func NewOrderedMapWithElements[K comparable, V any](els ...*Element[K, V]) *OrderedMap[K, V] {
om := &OrderedMap[K, V]{
kv: make(map[K]*Element[K, V], len(els)),
}
om := NewOrderedMapWithCapacity[K, V](len(els))
for _, el := range els {
om.Set(el.Key, el.Value)
}
@@ -134,7 +132,7 @@ func (m *OrderedMap[K, V]) Back() *Element[K, V] {
// Copy returns a new OrderedMap with the same elements.
// Using Copy while there are concurrent writes may mangle the result.
func (m *OrderedMap[K, V]) Copy() *OrderedMap[K, V] {
m2 := NewOrderedMap[K, V]()
m2 := NewOrderedMapWithCapacity[K, V](m.Len())
for el := m.Front(); el != nil; el = el.Next() {
m2.Set(el.Key, el.Value)
}
6 changes: 2 additions & 4 deletions v3/orderedmap.go
Original file line number Diff line number Diff line change
@@ -22,9 +22,7 @@ func NewOrderedMapWithCapacity[K comparable, V any](capacity int) *OrderedMap[K,
}

func NewOrderedMapWithElements[K comparable, V any](els ...*Element[K, V]) *OrderedMap[K, V] {
om := &OrderedMap[K, V]{
kv: make(map[K]*Element[K, V], len(els)),
}
om := NewOrderedMapWithCapacity[K, V](len(els))
for _, el := range els {
om.Set(el.Key, el.Value)
}
@@ -175,7 +173,7 @@ func (m *OrderedMap[K, V]) Back() *Element[K, V] {
// Copy returns a new OrderedMap with the same elements.
// Using Copy while there are concurrent writes may mangle the result.
func (m *OrderedMap[K, V]) Copy() *OrderedMap[K, V] {
m2 := NewOrderedMap[K, V]()
m2 := NewOrderedMapWithCapacity[K, V](m.Len())
for el := m.Front(); el != nil; el = el.Next() {
m2.Set(el.Key, el.Value)
}

0 comments on commit 5750fe0

Please sign in to comment.