Try to reduce allocations further

This commit is contained in:
Neil Alexander 2022-04-06 12:33:04 +01:00
parent ca73cf09b3
commit e6f72a8870
No known key found for this signature in database
GPG key ID: A02A2019A2BB0944
2 changed files with 12 additions and 13 deletions

View file

@ -258,6 +258,17 @@ func (n *Notifier) SharedUsers(userID string) (sharedUsers []string) {
return sharedUsers return sharedUsers
} }
func (n *Notifier) IsSharedUser(userA, userB string) 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 // GetListener returns a UserStreamListener that can be used to wait for
// updates for a user. Must be closed. // updates for a user. Must be closed.
// notify for anything before sincePos // notify for anything before sincePos

View file

@ -64,17 +64,6 @@ func (p *PresenceStreamProvider) IncrementalSync(
return to return to
} }
// get all joined users
// TODO: SharedUsers might get out of sync
sharedUsers := p.notifier.SharedUsers(req.Device.UserID)
// convert array to a map for easier checking if a user exists
sharedUsersMap := make(map[string]bool, len(sharedUsers))
sharedUsersMap[req.Device.UserID] = true
for i := range sharedUsers {
sharedUsersMap[sharedUsers[i]] = true
}
// add newly joined rooms user presences // add newly joined rooms user presences
newlyJoined := joinedRooms(req.Response, req.Device.UserID) newlyJoined := joinedRooms(req.Response, req.Device.UserID)
if len(newlyJoined) > 0 { if len(newlyJoined) > 0 {
@ -87,7 +76,6 @@ func (p *PresenceStreamProvider) IncrementalSync(
for _, roomID := range newlyJoined { for _, roomID := range newlyJoined {
roomUsers := p.notifier.JoinedUsers(roomID) roomUsers := p.notifier.JoinedUsers(roomID)
for i := range roomUsers { for i := range roomUsers {
sharedUsersMap[roomUsers[i]] = true
// we already got a presence from this user // we already got a presence from this user
if _, ok := presences[roomUsers[i]]; ok { if _, ok := presences[roomUsers[i]]; ok {
continue continue
@ -108,7 +96,7 @@ func (p *PresenceStreamProvider) IncrementalSync(
for i := range presences { for i := range presences {
presence := presences[i] presence := presences[i]
// Ignore users we don't share a room with // Ignore users we don't share a room with
if !sharedUsersMap[presence.UserID] { if req.Device.UserID != presence.UserID && !p.notifier.IsSharedUser(req.Device.UserID, presence.UserID) {
continue continue
} }
cacheKey := req.Device.UserID + req.Device.ID + presence.UserID cacheKey := req.Device.UserID + req.Device.ID + presence.UserID