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

proposal: sync: add sync.Map.Len() #56146

Closed
mattn opened this issue Oct 11, 2022 · 3 comments
Closed

proposal: sync: add sync.Map.Len() #56146

mattn opened this issue Oct 11, 2022 · 3 comments

Comments

@mattn
Copy link
Member

mattn commented Oct 11, 2022

Currently, no fast way to get length of sync.Map. To get the length, we should use Range with iterating elements. Hopefully to add Len() to get the length.

// Len returns a size of the map.
func (m *Map) Len() int {
	if m.dirty != nil {
		return len(m.dirty)
	}
	read := m.loadReadOnly()
	return len(read.m)
}
@mattn mattn added the Proposal label Oct 11, 2022
@gopherbot gopherbot added this to the Proposal milestone Oct 11, 2022
@mattn
Copy link
Member Author

mattn commented Oct 11, 2022

func (m *DeepCopyMap) Len() int {
	clean, _ := m.clean.Load().(map[any]any)
	return len(clean)
}

func (m *RWMutexMap) Len() int {
	m.mu.RLock()
	l := len(m.dirty)
	m.mu.RUnlock()
	return l
}
cpu: AMD Ryzen 5 3500U with Radeon Vega Mobile Gfx
BenchmarkLen/*sync_test.DeepCopyMap-8           645149230                2.034 ns/op
BenchmarkLen/*sync_test.RWMutexMap-8            24492294                44.83 ns/op
BenchmarkLen/*sync.Map-8                        1000000000               1.009 ns/op

@dsnet
Copy link
Member

dsnet commented Oct 11, 2022

See #20680. In particular the comment here: #20680 (comment)

@mvdan
Copy link
Member

mvdan commented Oct 11, 2022

This proposal adds no new information, so I'm closing it as a duplicate for now.

@mvdan mvdan closed this as not planned Won't fix, can't repro, duplicate, stale Oct 11, 2022
@golang golang locked and limited conversation to collaborators Oct 11, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants