diff options
author | Neil Alexander <neilalexander@users.noreply.github.com> | 2022-04-06 13:31:44 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-04-06 13:31:44 +0100 |
commit | 602818460d3801b480f63a187514b2d9ba9185f1 (patch) | |
tree | 1065f9a877593015be8b89706cc1b6303c4e30ab /syncapi/notifier/notifier.go | |
parent | e5e3350ce168a192dfc6b6b654276d5cffbdbf0f (diff) |
Reduce allocations in `/sync` presence stream (#2326)
* Reduce allocations on presence
* Try to reduce allocations further
* Tweak `IsSharedUser` some more
* Take map lock
Diffstat (limited to 'syncapi/notifier/notifier.go')
-rw-r--r-- | syncapi/notifier/notifier.go | 18 |
1 files changed, 18 insertions, 0 deletions
diff --git a/syncapi/notifier/notifier.go b/syncapi/notifier/notifier.go index d2b79b63..4e1a3c2a 100644 --- a/syncapi/notifier/notifier.go +++ b/syncapi/notifier/notifier.go @@ -250,6 +250,8 @@ func (n *Notifier) OnNewPresence( } func (n *Notifier) SharedUsers(userID string) (sharedUsers []string) { + n.mapLock.RLock() + defer n.mapLock.RUnlock() for roomID, users := range n.roomIDToJoinedUsers { if _, ok := users[userID]; ok { sharedUsers = append(sharedUsers, n.JoinedUsers(roomID)...) @@ -258,6 +260,20 @@ func (n *Notifier) SharedUsers(userID string) (sharedUsers []string) { return sharedUsers } +func (n *Notifier) IsSharedUser(userA, userB string) bool { + n.mapLock.RLock() + defer n.mapLock.RUnlock() + var okA, okB bool + for _, users := range n.roomIDToJoinedUsers { + _, okA = users[userA] + _, okB = users[userB] + if okA && okB { + return true + } + } + return false +} + // GetListener returns a UserStreamListener that can be used to wait for // updates for a user. Must be closed. // notify for anything before sincePos @@ -509,6 +525,7 @@ func (s userIDSet) remove(str string) { } func (s userIDSet) values() (vals []string) { + vals = make([]string, 0, len(s)) for str := range s { vals = append(vals, str) } @@ -529,6 +546,7 @@ func (s peekingDeviceSet) remove(d types.PeekingDevice) { } func (s peekingDeviceSet) values() (vals []types.PeekingDevice) { + vals = make([]types.PeekingDevice, 0, len(s)) for d := range s { vals = append(vals, d) } |