Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Commit

Permalink
Merge pull request #2176 from matrix-org/erikj/faster_get_joined
Browse files Browse the repository at this point in the history
Make get_joined_users faster
  • Loading branch information
erikjohnston authored May 5, 2017
2 parents e3aa8a7 + cf589f2 commit 66f371e
Showing 1 changed file with 42 additions and 16 deletions.
58 changes: 42 additions & 16 deletions synapse/storage/roommember.py
Original file line number Diff line number Diff line change
Expand Up @@ -418,25 +418,51 @@ def _get_joined_users_from_context(self, room_id, state_group, current_state_ids
if key[0] == EventTypes.Member
]

rows = yield self._simple_select_many_batch(
table="room_memberships",
column="event_id",
iterable=member_event_ids,
retcols=['user_id', 'display_name', 'avatar_url'],
keyvalues={
"membership": Membership.JOIN,
},
batch_size=500,
desc="_get_joined_users_from_context",
# We check if we have any of the member event ids in the event cache
# before we ask the DB

event_map = self._get_events_from_cache(
member_event_ids,
allow_rejected=False,
)

users_in_room = {
to_ascii(row["user_id"]): ProfileInfo(
avatar_url=to_ascii(row["avatar_url"]),
display_name=to_ascii(row["display_name"]),
missing_member_event_ids = []
users_in_room = {}
for event_id in member_event_ids:
ev_entry = event_map.get(event_id)
if ev_entry:
if ev_entry.event.membership == Membership.JOIN:
users_in_room[to_ascii(ev_entry.event.state_key)] = ProfileInfo(
display_name=to_ascii(
ev_entry.event.content.get("displayname", None)
),
avatar_url=to_ascii(
ev_entry.event.content.get("avatar_url", None)
),
)
else:
missing_member_event_ids.append(event_id)

if missing_member_event_ids:
rows = yield self._simple_select_many_batch(
table="room_memberships",
column="event_id",
iterable=missing_member_event_ids,
retcols=('user_id', 'display_name', 'avatar_url',),
keyvalues={
"membership": Membership.JOIN,
},
batch_size=500,
desc="_get_joined_users_from_context",
)
for row in rows
}

users_in_room.update({
to_ascii(row["user_id"]): ProfileInfo(
avatar_url=to_ascii(row["avatar_url"]),
display_name=to_ascii(row["display_name"]),
)
for row in rows
})

if event is not None and event.type == EventTypes.Member:
if event.membership == Membership.JOIN:
Expand Down

0 comments on commit 66f371e

Please sign in to comment.