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

perf: allow pre-allocate of size when new map #48

Merged
merged 1 commit into from
Dec 4, 2024
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
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
Loading