Skip to content

Commit 378040e

Browse files
authored
Add NewOrderedMapWithCapacity (#48)
For initializing an orderedmap with enough preallocated space. This improves performances when the result size is known before hand.
1 parent ce850a7 commit 378040e

File tree

4 files changed

+46
-0
lines changed

4 files changed

+46
-0
lines changed

orderedmap.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@ func NewOrderedMap() *OrderedMap {
1111
}
1212
}
1313

14+
// NewOrderedMapWithCapacity creates a map with enough pre-allocated space to
15+
// hold the specified number of elements.
16+
func NewOrderedMapWithCapacity(capacity int) *OrderedMap {
17+
return &OrderedMap{
18+
kv: make(map[interface{}]*Element, capacity),
19+
}
20+
}
21+
1422
// Get returns the value for a key. If the key does not exist, the second return
1523
// parameter will be false and the value will be nil.
1624
func (m *OrderedMap) Get(key interface{}) (interface{}, bool) {

orderedmap_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -761,6 +761,21 @@ func BenchmarkBigOrderedMap_Set(b *testing.B) {
761761
benchmarkBigOrderedMap_Set()(b)
762762
}
763763

764+
func benchmarkBigMapWithCapacity_Set() func(b *testing.B) {
765+
return func(b *testing.B) {
766+
for j := 0; j < b.N; j++ {
767+
m := orderedmap.NewOrderedMapWithCapacity(10000000)
768+
for i := 0; i < 10000000; i++ {
769+
m.Set(i, true)
770+
}
771+
}
772+
}
773+
}
774+
775+
func BenchmarkBigMapWithCapacity_Set(b *testing.B) {
776+
benchmarkBigMapWithCapacity_Set()(b)
777+
}
778+
764779
func benchmarkBigMap_Get() func(b *testing.B) {
765780
m := make(map[int]bool)
766781
for i := 0; i < 10000000; i++ {

v2/orderedmap.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@ func NewOrderedMap[K comparable, V any]() *OrderedMap[K, V] {
1111
}
1212
}
1313

14+
// NewOrderedMapWithCapacity creates a map with enough pre-allocated space to
15+
// hold the specified number of elements.
16+
func NewOrderedMapWithCapacity[K comparable, V any](capacity int) *OrderedMap[K, V] {
17+
return &OrderedMap[K, V]{
18+
kv: make(map[K]*Element[K, V], capacity),
19+
}
20+
}
21+
1422
func NewOrderedMapWithElements[K comparable, V any](els ...*Element[K, V]) *OrderedMap[K, V] {
1523
om := &OrderedMap[K, V]{
1624
kv: make(map[K]*Element[K, V], len(els)),

v2/orderedmap_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -730,6 +730,21 @@ func BenchmarkBigOrderedMap_Set(b *testing.B) {
730730
benchmarkBigOrderedMap_Set()(b)
731731
}
732732

733+
func benchmarkBigMapWithCapacity_Set() func(b *testing.B) {
734+
return func(b *testing.B) {
735+
for j := 0; j < b.N; j++ {
736+
m := orderedmap.NewOrderedMapWithCapacity[int, bool](10000000)
737+
for i := 0; i < 10000000; i++ {
738+
m.Set(i, true)
739+
}
740+
}
741+
}
742+
}
743+
744+
func BenchmarkBigMapWithCapacity_Set(b *testing.B) {
745+
benchmarkBigMapWithCapacity_Set()(b)
746+
}
747+
733748
func benchmarkBigMap_Get() func(b *testing.B) {
734749
m := make(map[int]bool)
735750
for i := 0; i < 10000000; i++ {

0 commit comments

Comments
 (0)