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

Reduce event lookups during room creation by passing known event IDs #13210

Merged
1 change: 1 addition & 0 deletions changelog.d/13210.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Reduce number of events queried during room creation. Contributed by Nick @ Beeper (@fizzadar).
22 changes: 20 additions & 2 deletions synapse/handlers/room.py
Original file line number Diff line number Diff line change
Expand Up @@ -1011,6 +1011,9 @@ async def _send_events_for_new_room(

event_keys = {"room_id": room_id, "sender": creator_id, "state_key": ""}

state_event_ids: List[str] = []
last_sent_event_id: Optional[str] = None

def create(etype: str, content: JsonDict, **kwargs: Any) -> JsonDict:
e = {"type": etype, "content": content}

Expand All @@ -1020,19 +1023,28 @@ def create(etype: str, content: JsonDict, **kwargs: Any) -> JsonDict:
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,
prev_event_ids=[last_sent_event_id] if last_sent_event_id else [],
state_event_ids=state_event_ids.copy(),
)

last_sent_event_id = sent_event.event_id
if sent_event.is_state():
state_event_ids.append(sent_event.event_id)

return last_stream_id

try:
Expand All @@ -1046,15 +1058,21 @@ async def send(etype: str, content: JsonDict, **kwargs: Any) -> int:
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,
"join",
ratelimit=ratelimit,
content=creator_join_profile,
new_room=True,
prev_event_ids=[last_sent_event_id],
state_event_ids=state_event_ids.copy(),
)
state_event_ids.append(member_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.
Expand Down