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

performance of count #143

Open
blight19 opened this issue Jul 14, 2023 · 3 comments · May be fixed by #142
Open

performance of count #143

blight19 opened this issue Jul 14, 2023 · 3 comments · May be fixed by #142

Comments

@blight19
Copy link

the performance of count is not very good,can use record it instead of calc it?

@blight19 blight19 linked a pull request Jul 14, 2023 that will close this issue
@helloteemo
Copy link

func (m ConcurrentMap[K, V]) Count() int {
	count := 0
	for i := 0; i < SHARD_COUNT; i++ {
		shard := m.shards[i]
		shard.RLock()
		count += len(shard.items)
		shard.RUnlock()
	}
	return count
}

The function len(shard.items) only reads the count count at the bottom of the hmap and doesn't go through it; there is no need to count a separate count

@blight19
Copy link
Author

func (m ConcurrentMap[K, V]) Count() int {
	count := 0
	for i := 0; i < SHARD_COUNT; i++ {
		shard := m.shards[i]
		shard.RLock()
		count += len(shard.items)
		shard.RUnlock()
	}
	return count
}

The function len(shard.items) only reads the count count at the bottom of the hmap and doesn't go through it; there is no need to count a separate count

there's lock in the function ,the benchmark demonstrate it's necessary to separate count instead of count it

@helloteemo
Copy link

func (m ConcurrentMap[K, V]) Count() int {
	count := 0
	for i := 0; i < SHARD_COUNT; i++ {
		shard := m.shards[i]
		shard.RLock()
		count += len(shard.items)
		shard.RUnlock()
	}
	return count
}

The function len(shard.items) only reads the count count at the bottom of the hmap and doesn't go through it; there is no need to count a separate count

there's lock in the function ,the benchmark demonstrate it's necessary to separate count instead of count it

You're right, I read your code, excellent job

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

Successfully merging a pull request may close this issue.

2 participants