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

Small speed up joining large remote rooms #9825

Merged
merged 3 commits into from
Apr 16, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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/9825.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Small speed up joining large remote rooms.
erikjohnston marked this conversation as resolved.
Show resolved Hide resolved
52 changes: 31 additions & 21 deletions synapse/storage/databases/main/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -1378,17 +1378,21 @@ def get_internal_metadata(event):
],
)

for event, _ in events_and_contexts:
if not event.internal_metadata.is_redacted():
# If we're persisting an unredacted event we go and ensure
# that we mark any redactions that reference this event as
# requiring censoring.
self.db_pool.simple_update_txn(
txn,
table="redactions",
keyvalues={"redacts": event.event_id},
updatevalues={"have_censored": False},
# If we're persisting an unredacted event we go and ensure
# that we mark any redactions that reference this event as
# requiring censoring.
sql = "UPDATE redactions SET have_censored = ? WHERE redacts = ?"
txn.execute_batch(
sql,
(
(
False,
event.event_id,
)
for event, _ in events_and_contexts
if not event.internal_metadata.is_redacted()
),
)

state_events_and_contexts = [
ec for ec in events_and_contexts if ec[0].is_state()
Expand Down Expand Up @@ -1881,20 +1885,26 @@ def _set_push_actions_for_event_and_users_txn(
),
)

for event, _ in events_and_contexts:
user_ids = self.db_pool.simple_select_onecol_txn(
txn,
table="event_push_actions_staging",
keyvalues={"event_id": event.event_id},
retcol="user_id",
)
room_to_event_ids = {} # type: Dict[str, List[str]]
for e, _ in events_and_contexts:
room_to_event_ids.setdefault(e.room_id, []).append(e.event_id)

for uid in user_ids:
txn.call_after(
self.store.get_unread_event_push_actions_by_room_for_user.invalidate_many,
(event.room_id, uid),
for room_id, event_ids in room_to_event_ids.items():
rows = self.db_pool.simple_select_many_txn(
txn,
table="event_push_actions_staging",
column="event_id",
iterable=event_ids,
keyvalues={},
retcols=("user_id",),
)

for row in rows:
txn.call_after(
self.store.get_unread_event_push_actions_by_room_for_user.invalidate_many,
(room_id, row["user_id"]),
)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would it be worth deduping the user_ids, either with a select distinct or with a Python set? presumably there could be quite a few duplicates?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, good point actually.


# Now we delete the staging area for *all* events that were being
# persisted.
txn.execute_batch(
Expand Down