Skip to content

Commit

Permalink
[patch] add map length check function
Browse files Browse the repository at this point in the history
Signed-off-by: kpango <kpango@vdaas.org>
  • Loading branch information
kpango committed Apr 19, 2023
1 parent c32385f commit 90144d1
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion map.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,6 @@ func (m *Map[K, V]) CompareAndDelete(key K, old V) (deleted bool) {
func (m *Map[K, V]) Range(f func(key K, value V) bool) {
read := m.loadReadOnly()
if read.amended {

m.mu.Lock()
read = m.loadReadOnly()
if read.amended {
Expand All @@ -308,6 +307,23 @@ func (m *Map[K, V]) Range(f func(key K, value V) bool) {
}
}

func (m *Map[K, V]) Len() int {
read := m.loadReadOnly()
if read.amended {
m.mu.Lock()
read = m.loadReadOnly()
if read.amended {
read = readOnly[K, V]{m: m.dirty}
m.read.Store(&read)
m.dirty = nil
m.misses = 0
}
m.mu.Unlock()
}

return len(read.m)
}

func (m *Map[K, V]) missLocked() {
m.misses++
if m.misses < len(m.dirty) {
Expand Down

0 comments on commit 90144d1

Please sign in to comment.