concurrent-map icon indicating copy to clipboard operation
concurrent-map copied to clipboard

Performance issues during traversal

Open big-uncle opened this issue 4 years ago • 2 comments

When we are traversing frequently, using IterBuffered() will generate a lot of memory garbage, which will cause a lot of burden on the GC of the program. At this time, we need to traverse externally, so we have to provide the items method

big-uncle avatar Jun 24 '21 03:06 big-uncle

// return items to the outside
// When external traversal, it is helpful to reduce heap memory allocation
// When the external traversal is very frequent, the external traversal by itself will have better performance than using IterBuffered()
//use example:
// for _, shard := cmap {
// 	shard.RLock()
// 	for _, val := range shard.GetItems() {
// 		//to do something...
// 	}
// 	shard.RUnlock()
// }
func (m ConcurrentMapShared) GetItems() map[string]interface{} {
	return m.items
}

big-uncle avatar Jun 24 '21 03:06 big-uncle

@big-uncle i would strongly discourage that get items method. i had a similar method in my program and it wreaked havoc on my garbage collector. the absolute best bet is to use IterCb which is similar to the Range method on sync.Map. That will eliminate any unnecessary heap allocations and allow you to iterate through the items without having to spin up a goroutine to push items to a channel.

jamelt avatar Oct 21 '21 11:10 jamelt