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

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

Open
mhmtszr opened this issue Aug 5, 2023 · 1 comment
Open

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

mhmtszr opened this issue Aug 5, 2023 · 1 comment

Comments

@mhmtszr
Copy link

mhmtszr commented Aug 5, 2023

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
Copy link
Contributor

reltuk commented Aug 14, 2023

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants