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

More speedups/fixes to creating batched events #15195

Merged
merged 8 commits into from
Mar 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/15195.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Improve performance of creating and authenticating events.
23 changes: 17 additions & 6 deletions synapse/event_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,13 +168,24 @@ async def check_state_independent_auth_rules(
return

# 2. Reject if event has auth_events that: ...
auth_events = await store.get_events(
event.auth_event_ids(),
redact_behaviour=EventRedactBehaviour.as_is,
allow_rejected=True,
)
if batched_auth_events:
auth_events.update(batched_auth_events)
# Copy the batched auth events to avoid mutating them.
auth_events = dict(batched_auth_events)
H-Shay marked this conversation as resolved.
Show resolved Hide resolved
needed_auth_event_ids = set(event.auth_event_ids()) - batched_auth_events.keys()
if needed_auth_event_ids:
auth_events.update(
await store.get_events(
needed_auth_event_ids,
redact_behaviour=EventRedactBehaviour.as_is,
allow_rejected=True,
)
)
else:
auth_events = await store.get_events(
event.auth_event_ids(),
redact_behaviour=EventRedactBehaviour.as_is,
allow_rejected=True,
)

room_id = event.room_id
auth_dict: MutableStateMap[str] = {}
Expand Down
1 change: 1 addition & 0 deletions synapse/events/snapshot.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,7 @@ async def get_prev_state_ids(
Maps a (type, state_key) to the event ID of the state event matching
this tuple.
"""

assert self.state_group_before_event is not None
return await self._storage.state.get_state_ids_for_group(
self.state_group_before_event, state_filter
Expand Down
13 changes: 11 additions & 2 deletions synapse/handlers/event_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,18 @@ async def check_auth_rules_from_context(
self._store, event, batched_auth_events
)
auth_event_ids = event.auth_event_ids()
auth_events_by_id = await self._store.get_events(auth_event_ids)

if batched_auth_events:
auth_events_by_id.update(batched_auth_events)
# Copy the batched auth events to avoid mutating them.
auth_events_by_id = dict(batched_auth_events)
needed_auth_event_ids = set(auth_event_ids) - set(batched_auth_events)
if needed_auth_event_ids:
auth_events_by_id.update(
await self._store.get_events(needed_auth_event_ids)
)
else:
auth_events_by_id = await self._store.get_events(auth_event_ids)

check_state_dependent_auth_rules(event, auth_events_by_id.values())

def compute_auth_events(
Expand Down
4 changes: 3 additions & 1 deletion synapse/handlers/room.py
Original file line number Diff line number Diff line change
Expand Up @@ -1123,7 +1123,9 @@ async def create_event(
event_dict,
prev_event_ids=prev_event,
depth=depth,
state_map=state_map,
# Take a copy to ensure each event gets a unique copy of
# state_map since it is modified below.
state_map=dict(state_map),
H-Shay marked this conversation as resolved.
Show resolved Hide resolved
for_batch=for_batch,
)

Expand Down