mirror of
https://github.com/matrix-org/dendrite.git
synced 2026-01-01 03:03:10 -06:00
Reuse map to reduce allocations/GC pressure
This commit is contained in:
parent
4d2d79b4da
commit
36f06c5e54
|
|
@ -45,6 +45,8 @@ type Notifier struct {
|
||||||
lastCleanUpTime time.Time
|
lastCleanUpTime time.Time
|
||||||
// Protects roomIDToJoinedUsers and roomIDToPeekingDevices
|
// Protects roomIDToJoinedUsers and roomIDToPeekingDevices
|
||||||
mapLock *sync.RWMutex
|
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.
|
// 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 {
|
func (n *Notifier) SharedUsers(userID string) []string {
|
||||||
n.mapLock.RLock()
|
n.mapLock.RLock()
|
||||||
defer n.mapLock.RUnlock()
|
defer n.mapLock.RUnlock()
|
||||||
sharedMap := map[string]struct{}{
|
n._sharedUsers[userID] = struct{}{}
|
||||||
userID: {},
|
|
||||||
}
|
|
||||||
for roomID, users := range n.roomIDToJoinedUsers {
|
for roomID, users := range n.roomIDToJoinedUsers {
|
||||||
if _, ok := users[userID]; !ok {
|
if _, ok := users[userID]; !ok {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
for _, userID := range n.JoinedUsers(roomID) {
|
for _, userID := range n.JoinedUsers(roomID) {
|
||||||
sharedMap[userID] = struct{}{}
|
n._sharedUsers[userID] = struct{}{}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
sharedUsers := make([]string, 0, len(sharedMap))
|
sharedUsers := make([]string, 0, len(n._sharedUsers))
|
||||||
for userID := range sharedMap {
|
for userID := range n._sharedUsers {
|
||||||
sharedUsers = append(sharedUsers, userID)
|
sharedUsers = append(sharedUsers, userID)
|
||||||
|
delete(n._sharedUsers, userID)
|
||||||
}
|
}
|
||||||
return sharedUsers
|
return sharedUsers
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue