From a1fcf07a7458949f66899e1c27abc14cb239137e Mon Sep 17 00:00:00 2001 From: Patrick Cloke Date: Fri, 6 Jan 2023 08:47:22 -0500 Subject: [PATCH 1/5] Fix typo. --- synapse/handlers/search.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/synapse/handlers/search.py b/synapse/handlers/search.py index 33115ce488ca..40f4635c4e29 100644 --- a/synapse/handlers/search.py +++ b/synapse/handlers/search.py @@ -275,7 +275,7 @@ async def _search( ) room_ids = {r.room_id for r in rooms} - # If doing a subset of all rooms seearch, check if any of the rooms + # If doing a subset of all rooms search, check if any of the rooms # are from an upgraded room, and search their contents as well if search_filter.rooms: historical_room_ids: List[str] = [] From 65239fe8ddb540f3eade3bce6dffb0d79e046770 Mon Sep 17 00:00:00 2001 From: Patrick Cloke Date: Fri, 6 Jan 2023 08:47:40 -0500 Subject: [PATCH 2/5] Reformat code. --- synapse/handlers/sync.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/synapse/handlers/sync.py b/synapse/handlers/sync.py index 4fa480262b03..df477776fcc0 100644 --- a/synapse/handlers/sync.py +++ b/synapse/handlers/sync.py @@ -1403,11 +1403,14 @@ async def generate_sync_result( logger.debug("Fetching room data") - res = await self._generate_sync_entry_for_rooms( + ( + newly_joined_rooms, + newly_joined_or_invited_or_knocked_users, + newly_left_rooms, + newly_left_users, + ) = await self._generate_sync_entry_for_rooms( sync_result_builder, account_data_by_room ) - newly_joined_rooms, newly_joined_or_invited_or_knocked_users, _, _ = res - _, _, newly_left_rooms, newly_left_users = res block_all_presence_data = ( since_token is None and sync_config.filter_collection.blocks_all_presence() From b0480c65557c45ca4116e15bcffe3eb60e8d860b Mon Sep 17 00:00:00 2001 From: Patrick Cloke Date: Fri, 6 Jan 2023 08:48:01 -0500 Subject: [PATCH 3/5] Consider empty arrays equivalent to no types. --- synapse/api/filtering.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/synapse/api/filtering.py b/synapse/api/filtering.py index a9888381b451..4cf8f0cc8ef9 100644 --- a/synapse/api/filtering.py +++ b/synapse/api/filtering.py @@ -351,13 +351,13 @@ def __init__(self, hs: "HomeServer", filter_json: JsonDict): self.not_rel_types = filter_json.get("org.matrix.msc3874.not_rel_types", []) def filters_all_types(self) -> bool: - return "*" in self.not_types + return self.types == [] or "*" in self.not_types def filters_all_senders(self) -> bool: - return "*" in self.not_senders + return self.senders == [] or "*" in self.not_senders def filters_all_rooms(self) -> bool: - return "*" in self.not_rooms + return self.rooms == [] or "*" in self.not_rooms def _check(self, event: FilterEvent) -> bool: """Checks whether the filter matches the given event. @@ -450,8 +450,8 @@ def _check_fields(self, field_matchers: Dict[str, Callable[[str], bool]]) -> boo if any(map(match_func, disallowed_values)): return False - # Other the event does not match at least one of the allowed values, - # reject it. + # Otherwise if the event does not match at least one of the allowed + # values, reject it. allowed_values = getattr(self, name) if allowed_values is not None: if not any(map(match_func, allowed_values)): From 193f79ee28a6624375f2635b5da7492e23ca91bb Mon Sep 17 00:00:00 2001 From: Patrick Cloke Date: Fri, 6 Jan 2023 08:48:43 -0500 Subject: [PATCH 4/5] Fast-path filtering out all rooms. --- synapse/api/filtering.py | 3 +++ synapse/handlers/sync.py | 5 +++++ 2 files changed, 8 insertions(+) diff --git a/synapse/api/filtering.py b/synapse/api/filtering.py index 4cf8f0cc8ef9..2b5af264b43d 100644 --- a/synapse/api/filtering.py +++ b/synapse/api/filtering.py @@ -283,6 +283,9 @@ async def filter_room_account_data( await self._room_filter.filter(events) ) + def blocks_all_rooms(self) -> bool: + return self._room_filter.filters_all_rooms() + def blocks_all_presence(self) -> bool: return ( self._presence_filter.filters_all_types() diff --git a/synapse/handlers/sync.py b/synapse/handlers/sync.py index df477776fcc0..6942e06c770e 100644 --- a/synapse/handlers/sync.py +++ b/synapse/handlers/sync.py @@ -1792,6 +1792,11 @@ async def _generate_sync_entry_for_rooms( - newly_left_rooms - newly_left_users """ + + # If the request doesn't care about rooms then nothing to do! + if sync_result_builder.sync_config.filter_collection.blocks_all_rooms(): + return set(), set(), set(), set() + since_token = sync_result_builder.since_token # 1. Start by fetching all ephemeral events in rooms we've joined (if required). From 9c86d103e91689b7cacc3de62b2e9567c11e91df Mon Sep 17 00:00:00 2001 From: Patrick Cloke Date: Fri, 6 Jan 2023 09:26:47 -0500 Subject: [PATCH 5/5] Newsfragment --- changelog.d/14786.feature | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog.d/14786.feature diff --git a/changelog.d/14786.feature b/changelog.d/14786.feature new file mode 100644 index 000000000000..008d61ab039c --- /dev/null +++ b/changelog.d/14786.feature @@ -0,0 +1 @@ +Improve performance of `/sync` when filtering all rooms, message types, or senders.