Skip to content

Commit

Permalink
Add NewOrderedMapWithCapacity (#48)
Browse files Browse the repository at this point in the history
For initializing an orderedmap with enough preallocated space. This improves performances when the result size is known before hand.
  • Loading branch information
VarusHsu authored Dec 4, 2024
1 parent ce850a7 commit 378040e
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 0 deletions.
8 changes: 8 additions & 0 deletions orderedmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ func NewOrderedMap() *OrderedMap {
}
}

// NewOrderedMapWithCapacity creates a map with enough pre-allocated space to
// hold the specified number of elements.
func NewOrderedMapWithCapacity(capacity int) *OrderedMap {
return &OrderedMap{
kv: make(map[interface{}]*Element, capacity),
}
}

// Get returns the value for a key. If the key does not exist, the second return
// parameter will be false and the value will be nil.
func (m *OrderedMap) Get(key interface{}) (interface{}, bool) {
Expand Down
15 changes: 15 additions & 0 deletions orderedmap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -761,6 +761,21 @@ func BenchmarkBigOrderedMap_Set(b *testing.B) {
benchmarkBigOrderedMap_Set()(b)
}

func benchmarkBigMapWithCapacity_Set() func(b *testing.B) {
return func(b *testing.B) {
for j := 0; j < b.N; j++ {
m := orderedmap.NewOrderedMapWithCapacity(10000000)
for i := 0; i < 10000000; i++ {
m.Set(i, true)
}
}
}
}

func BenchmarkBigMapWithCapacity_Set(b *testing.B) {
benchmarkBigMapWithCapacity_Set()(b)
}

func benchmarkBigMap_Get() func(b *testing.B) {
m := make(map[int]bool)
for i := 0; i < 10000000; i++ {
Expand Down
8 changes: 8 additions & 0 deletions v2/orderedmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ func NewOrderedMap[K comparable, V any]() *OrderedMap[K, V] {
}
}

// NewOrderedMapWithCapacity creates a map with enough pre-allocated space to
// hold the specified number of elements.
func NewOrderedMapWithCapacity[K comparable, V any](capacity int) *OrderedMap[K, V] {
return &OrderedMap[K, V]{
kv: make(map[K]*Element[K, V], capacity),
}
}

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)),
Expand Down
15 changes: 15 additions & 0 deletions v2/orderedmap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -730,6 +730,21 @@ func BenchmarkBigOrderedMap_Set(b *testing.B) {
benchmarkBigOrderedMap_Set()(b)
}

func benchmarkBigMapWithCapacity_Set() func(b *testing.B) {
return func(b *testing.B) {
for j := 0; j < b.N; j++ {
m := orderedmap.NewOrderedMapWithCapacity[int, bool](10000000)
for i := 0; i < 10000000; i++ {
m.Set(i, true)
}
}
}
}

func BenchmarkBigMapWithCapacity_Set(b *testing.B) {
benchmarkBigMapWithCapacity_Set()(b)
}

func benchmarkBigMap_Get() func(b *testing.B) {
m := make(map[int]bool)
for i := 0; i < 10000000; i++ {
Expand Down

0 comments on commit 378040e

Please sign in to comment.