From 78004afe922d1413fa9efe133bd2a0342196d655 Mon Sep 17 00:00:00 2001 From: Patrick Cloke Date: Thu, 2 Feb 2023 14:19:05 -0500 Subject: [PATCH] Do not fetch room-specific account data unless needed. --- changelog.d/14973.misc | 1 + synapse/handlers/sync.py | 40 +++++++++++++++++----------------------- 2 files changed, 18 insertions(+), 23 deletions(-) create mode 100644 changelog.d/14973.misc diff --git a/changelog.d/14973.misc b/changelog.d/14973.misc new file mode 100644 index 000000000000..365762360285 --- /dev/null +++ b/changelog.d/14973.misc @@ -0,0 +1 @@ +Improve performance of `/sync` in a few situations. diff --git a/synapse/handlers/sync.py b/synapse/handlers/sync.py index 92e5f42fcaec..428e1fcc2125 100644 --- a/synapse/handlers/sync.py +++ b/synapse/handlers/sync.py @@ -1444,9 +1444,7 @@ async def generate_sync_result( logger.debug("Fetching account data") - account_data_by_room = await self._generate_sync_entry_for_account_data( - sync_result_builder - ) + await self._generate_sync_entry_for_account_data(sync_result_builder) # Presence data is included if the server has it enabled and not filtered out. include_presence_data = bool( @@ -1472,9 +1470,7 @@ async def generate_sync_result( ( newly_joined_rooms, newly_left_rooms, - ) = await self._generate_sync_entry_for_rooms( - sync_result_builder, account_data_by_room - ) + ) = await self._generate_sync_entry_for_rooms(sync_result_builder) # Work out which users have joined or left rooms we're in. We use this # to build the presence and device_list parts of the sync response in @@ -1717,7 +1713,7 @@ async def _generate_sync_entry_for_to_device( async def _generate_sync_entry_for_account_data( self, sync_result_builder: "SyncResultBuilder" - ) -> Dict[str, Dict[str, JsonDict]]: + ) -> None: """Generates the account data portion of the sync response. Account data (called "Client Config" in the spec) can be set either globally @@ -1740,17 +1736,11 @@ async def _generate_sync_entry_for_account_data( since_token = sync_result_builder.since_token if since_token and not sync_result_builder.full_state: - # TODO Do not fetch room account data if it will be unused. global_account_data = ( await self.store.get_updated_global_account_data_for_user( user_id, since_token.account_data_key ) ) - account_data_by_room = ( - await self.store.get_updated_room_account_data_for_user( - user_id, since_token.account_data_key - ) - ) push_rules_changed = await self.store.have_push_rules_changed_for_user( user_id, int(since_token.push_rules_key) @@ -1763,10 +1753,7 @@ async def _generate_sync_entry_for_account_data( else: # TODO Do not fetch room account data if it will be unused. global_account_data = await self.store.get_global_account_data_for_user( - sync_config.user.to_string() - ) - account_data_by_room = await self.store.get_room_account_data_for_user( - sync_config.user.to_string() + user_id ) global_account_data["m.push_rules"] = await self.push_rules_for_user( @@ -1782,8 +1769,6 @@ async def _generate_sync_entry_for_account_data( sync_result_builder.account_data = account_data_for_user - return account_data_by_room - async def _generate_sync_entry_for_presence( self, sync_result_builder: "SyncResultBuilder", @@ -1843,9 +1828,7 @@ async def _generate_sync_entry_for_presence( sync_result_builder.presence = presence async def _generate_sync_entry_for_rooms( - self, - sync_result_builder: "SyncResultBuilder", - account_data_by_room: Dict[str, Dict[str, JsonDict]], + self, sync_result_builder: "SyncResultBuilder" ) -> Tuple[AbstractSet[str], AbstractSet[str]]: """Generates the rooms portion of the sync response. Populates the `sync_result_builder` with the result. @@ -1856,7 +1839,6 @@ async def _generate_sync_entry_for_rooms( Args: sync_result_builder - account_data_by_room: Dictionary of per room account data Returns: Returns a 2-tuple describing rooms the user has joined or left. @@ -1869,6 +1851,18 @@ async def _generate_sync_entry_for_rooms( since_token = sync_result_builder.since_token user_id = sync_result_builder.sync_config.user.to_string() + # 0. Start by fetching room account data. + if since_token and not sync_result_builder.full_state: + account_data_by_room = ( + await self.store.get_updated_room_account_data_for_user( + user_id, since_token.account_data_key + ) + ) + else: + account_data_by_room = await self.store.get_room_account_data_for_user( + user_id + ) + # 1. Start by fetching all ephemeral events in rooms we've joined (if required). block_all_room_ephemeral = ( sync_result_builder.sync_config.filter_collection.blocks_all_rooms()