summary refs log tree commit diff
path: root/synapse/handlers/room.py
diff options
context:
space:
mode:
authorNick Mills-Barrett <nick@beeper.com>2022-07-11 19:00:12 +0200
committerGitHub <noreply@github.com>2022-07-11 18:00:12 +0100
commit92202ce8670b3025bf7798831cdd5f21efa280d5 (patch)
treede18aa575fe64c89b6e46f408485786bce73745a /synapse/handlers/room.py
parentUniformize spam-checker API, part 5: expand other spam-checker callbacks to r... (diff)
downloadsynapse-92202ce8670b3025bf7798831cdd5f21efa280d5.tar.xz
Reduce event lookups during room creation by passing known event IDs (#13210)
Inspired by the room batch handler, this uses previous event inserts to
pre-populate prev events during room creation, reducing the number of
queries required to create a room.

Signed off by Nick @ Beeper (@Fizzadar)
Diffstat (limited to 'synapse/handlers/room.py')
-rw-r--r--synapse/handlers/room.py18
1 files changed, 16 insertions, 2 deletions
diff --git a/synapse/handlers/room.py b/synapse/handlers/room.py
index 8dd94cbc76..a54f163c0a 100644
--- a/synapse/handlers/room.py
+++ b/synapse/handlers/room.py
@@ -1019,6 +1019,8 @@ class RoomCreationHandler:
 
         event_keys = {"room_id": room_id, "sender": creator_id, "state_key": ""}
 
+        last_sent_event_id: Optional[str] = None
+
         def create(etype: str, content: JsonDict, **kwargs: Any) -> JsonDict:
             e = {"type": etype, "content": content}
 
@@ -1028,19 +1030,27 @@ class RoomCreationHandler:
             return e
 
         async def send(etype: str, content: JsonDict, **kwargs: Any) -> int:
+            nonlocal last_sent_event_id
+
             event = create(etype, content, **kwargs)
             logger.debug("Sending %s in new room", etype)
             # Allow these events to be sent even if the user is shadow-banned to
             # allow the room creation to complete.
             (
-                _,
+                sent_event,
                 last_stream_id,
             ) = await self.event_creation_handler.create_and_send_nonmember_event(
                 creator,
                 event,
                 ratelimit=False,
                 ignore_shadow_ban=True,
+                # Note: we don't pass state_event_ids here because this triggers
+                # an additional query per event to look them up from the events table.
+                prev_event_ids=[last_sent_event_id] if last_sent_event_id else [],
             )
+
+            last_sent_event_id = sent_event.event_id
+
             return last_stream_id
 
         try:
@@ -1054,7 +1064,9 @@ class RoomCreationHandler:
         await send(etype=EventTypes.Create, content=creation_content)
 
         logger.debug("Sending %s in new room", EventTypes.Member)
-        await self.room_member_handler.update_membership(
+        # Room create event must exist at this point
+        assert last_sent_event_id is not None
+        member_event_id, _ = await self.room_member_handler.update_membership(
             creator,
             creator.user,
             room_id,
@@ -1062,7 +1074,9 @@ class RoomCreationHandler:
             ratelimit=ratelimit,
             content=creator_join_profile,
             new_room=True,
+            prev_event_ids=[last_sent_event_id],
         )
+        last_sent_event_id = member_event_id
 
         # We treat the power levels override specially as this needs to be one
         # of the first events that get sent into a room.