dolthub/swiss

Can Iter function return a boolean if it's stopped?

mhmtszr opened this issue · 1 comments

Hi,

Currently, I'm developing the concurrent version of swiss-map https://github.com/mhmtszr/concurrent-swiss-map

While implementing Range functions https://github.com/mhmtszr/concurrent-swiss-map/blob/master/concurrent_swiss_map.go#L119 I couldn't stop the other shard because I didn't know the result of the callback functions thus I couldn't stop other shard iterations.

If the Iter function returns a boolean would be great for here what do you think?

Also, I'm open to your feedback about my concurrent-swiss-map implementation too.

reltuk commented

I'm not entirely certain we should change the interface here. The boolean return seems poorly self-documenting in particular. A simple solution at the call-site is to keep track of this yourself:

func (m *CsMap[K, V]) Range(f func(key K, value V) (stop bool)) {
	for i := 0; i < len(m.shards); i++ {
		shard := m.shards[i]
		var stop bool
		shard.RLock()
		shard.items.Iter(func(key K, value V) bool {
			stop = f(key, value)
			return stop
		})
		shard.RUnlock()
		if stop {
			return
		}
	}
}

I realize it's not beautiful...

Is there appreciable perf overhead to this approach?