From 7ebf518c028088d1932262649c1042fb76f9d013 Mon Sep 17 00:00:00 2001 From: Erik Johnston Date: Tue, 2 May 2017 10:43:34 +0100 Subject: [PATCH 1/3] Make get_joined_users faster --- synapse/storage/roommember.py | 54 ++++++++++++++++++++++++----------- 1 file changed, 38 insertions(+), 16 deletions(-) diff --git a/synapse/storage/roommember.py b/synapse/storage/roommember.py index 7ad2198d9..46efdfb3e 100644 --- a/synapse/storage/roommember.py +++ b/synapse/storage/roommember.py @@ -417,25 +417,47 @@ class RoomMemberStore(SQLBaseStore): 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", + 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, ev_entry in event_map.iteritems(): + if event_id: + 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=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: From aa93cb9f442019dde1704a0c12b47e5b664e192a Mon Sep 17 00:00:00 2001 From: Erik Johnston Date: Thu, 4 May 2017 14:59:28 +0100 Subject: [PATCH 2/3] Add comment --- synapse/storage/roommember.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/synapse/storage/roommember.py b/synapse/storage/roommember.py index 46efdfb3e..dcb95b924 100644 --- a/synapse/storage/roommember.py +++ b/synapse/storage/roommember.py @@ -417,6 +417,9 @@ class RoomMemberStore(SQLBaseStore): if key[0] == EventTypes.Member ] + # 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, From cf589f2c1e68b71db80cbc2cf56f88118faf545b Mon Sep 17 00:00:00 2001 From: Erik Johnston Date: Fri, 5 May 2017 10:17:56 +0100 Subject: [PATCH 3/3] Fixes --- synapse/storage/roommember.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/synapse/storage/roommember.py b/synapse/storage/roommember.py index dcb95b924..c63c0622d 100644 --- a/synapse/storage/roommember.py +++ b/synapse/storage/roommember.py @@ -427,8 +427,9 @@ class RoomMemberStore(SQLBaseStore): missing_member_event_ids = [] users_in_room = {} - for event_id, ev_entry in event_map.iteritems(): - if event_id: + 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( @@ -445,7 +446,7 @@ class RoomMemberStore(SQLBaseStore): rows = yield self._simple_select_many_batch( table="room_memberships", column="event_id", - iterable=member_event_ids, + iterable=missing_member_event_ids, retcols=('user_id', 'display_name', 'avatar_url',), keyvalues={ "membership": Membership.JOIN,