Reuse map to reduce allocations/GC pressure

This commit is contained in:
Neil Alexander 2022-04-06 14:34:52 +01:00
parent 4d2d79b4da
commit 36f06c5e54
No known key found for this signature in database
GPG key ID: A02A2019A2BB0944

View file

@ -45,6 +45,8 @@ type Notifier struct {
lastCleanUpTime time.Time
// Protects roomIDToJoinedUsers and roomIDToPeekingDevices
mapLock *sync.RWMutex
// This map is reused to prevent allocations and GC pressure in SharedUsers.
_sharedUsers map[string]struct{}
}
// NewNotifier creates a new notifier set to the given sync position.
@ -255,20 +257,19 @@ func (n *Notifier) OnNewPresence(
func (n *Notifier) SharedUsers(userID string) []string {
n.mapLock.RLock()
defer n.mapLock.RUnlock()
sharedMap := map[string]struct{}{
userID: {},
}
n._sharedUsers[userID] = struct{}{}
for roomID, users := range n.roomIDToJoinedUsers {
if _, ok := users[userID]; !ok {
continue
}
for _, userID := range n.JoinedUsers(roomID) {
sharedMap[userID] = struct{}{}
n._sharedUsers[userID] = struct{}{}
}
}
sharedUsers := make([]string, 0, len(sharedMap))
for userID := range sharedMap {
sharedUsers := make([]string, 0, len(n._sharedUsers))
for userID := range n._sharedUsers {
sharedUsers = append(sharedUsers, userID)
delete(n._sharedUsers, userID)
}
return sharedUsers
}