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

Commit

Permalink
Do not fetch room-specific account data unless needed.
Browse files Browse the repository at this point in the history
  • Loading branch information
clokep committed Feb 3, 2023
1 parent 238f9af commit 78004af
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 23 deletions.
1 change: 1 addition & 0 deletions changelog.d/14973.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Improve performance of `/sync` in a few situations.
40 changes: 17 additions & 23 deletions synapse/handlers/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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(
Expand All @@ -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",
Expand Down Expand Up @@ -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.
Expand All @@ -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.
Expand All @@ -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()
Expand Down

0 comments on commit 78004af

Please sign in to comment.