From bf44840343c75d8630b3e47f7d55f7a7ef5c647e Mon Sep 17 00:00:00 2001 From: Patrick Cloke Date: Thu, 2 Feb 2023 08:19:13 -0500 Subject: [PATCH 1/7] Add comments. --- synapse/handlers/sync.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/synapse/handlers/sync.py b/synapse/handlers/sync.py index 5235e294608d..dcd31d6d2ed5 100644 --- a/synapse/handlers/sync.py +++ b/synapse/handlers/sync.py @@ -1459,6 +1459,9 @@ async def generate_sync_result( sync_result_builder, account_data_by_room ) + # Presence data is included if the server has it enabled and: + # - There is a sync token, or + # - Is not filtered out. block_all_presence_data = ( since_token is None and sync_config.filter_collection.blocks_all_presence() ) From 1a8f515271b50c8fd1a6f575ffefe1bcf340f1ea Mon Sep 17 00:00:00 2001 From: Patrick Cloke Date: Thu, 2 Feb 2023 08:19:50 -0500 Subject: [PATCH 2/7] Swap logic to positive. --- synapse/handlers/sync.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/synapse/handlers/sync.py b/synapse/handlers/sync.py index dcd31d6d2ed5..8491e24f3db8 100644 --- a/synapse/handlers/sync.py +++ b/synapse/handlers/sync.py @@ -1462,10 +1462,10 @@ async def generate_sync_result( # Presence data is included if the server has it enabled and: # - There is a sync token, or # - Is not filtered out. - block_all_presence_data = ( + include_presence_data = not ( since_token is None and sync_config.filter_collection.blocks_all_presence() ) - if self.hs_config.server.use_presence and not block_all_presence_data: + if self.hs_config.server.use_presence and include_presence_data: logger.debug("Fetching presence data") await self._generate_sync_entry_for_presence( sync_result_builder, From d303310e7f2ba1544009cc79f070f6cc14f88ea4 Mon Sep 17 00:00:00 2001 From: Patrick Cloke Date: Thu, 2 Feb 2023 08:20:25 -0500 Subject: [PATCH 3/7] Move config flag into boolean. --- synapse/handlers/sync.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/synapse/handlers/sync.py b/synapse/handlers/sync.py index 8491e24f3db8..131514316bdd 100644 --- a/synapse/handlers/sync.py +++ b/synapse/handlers/sync.py @@ -1462,10 +1462,10 @@ async def generate_sync_result( # Presence data is included if the server has it enabled and: # - There is a sync token, or # - Is not filtered out. - include_presence_data = not ( + include_presence_data = self.hs_config.server.use_presence and not ( since_token is None and sync_config.filter_collection.blocks_all_presence() ) - if self.hs_config.server.use_presence and include_presence_data: + if include_presence_data: logger.debug("Fetching presence data") await self._generate_sync_entry_for_presence( sync_result_builder, From a9ced4d5a62748870e1d1e869d3258aedb816433 Mon Sep 17 00:00:00 2001 From: Patrick Cloke Date: Thu, 2 Feb 2023 08:21:04 -0500 Subject: [PATCH 4/7] Apply De Morgan's Theorem. --- synapse/handlers/sync.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/synapse/handlers/sync.py b/synapse/handlers/sync.py index 131514316bdd..86f8bf876869 100644 --- a/synapse/handlers/sync.py +++ b/synapse/handlers/sync.py @@ -1462,8 +1462,9 @@ async def generate_sync_result( # Presence data is included if the server has it enabled and: # - There is a sync token, or # - Is not filtered out. - include_presence_data = self.hs_config.server.use_presence and not ( - since_token is None and sync_config.filter_collection.blocks_all_presence() + include_presence_data = self.hs_config.server.use_presence and ( + since_token is not None + or not sync_config.filter_collection.blocks_all_presence() ) if include_presence_data: logger.debug("Fetching presence data") From 63fa12c590b702f1149705890a157ea392b0a5b3 Mon Sep 17 00:00:00 2001 From: Patrick Cloke Date: Thu, 2 Feb 2023 08:25:50 -0500 Subject: [PATCH 5/7] Newsfragment --- changelog.d/14970.misc | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog.d/14970.misc diff --git a/changelog.d/14970.misc b/changelog.d/14970.misc new file mode 100644 index 000000000000..365762360285 --- /dev/null +++ b/changelog.d/14970.misc @@ -0,0 +1 @@ +Improve performance of `/sync` in a few situations. From 9b970d6c5bf4fcc9509d93bc2f5e3ad22c0f09be Mon Sep 17 00:00:00 2001 From: Patrick Cloke Date: Thu, 2 Feb 2023 08:56:41 -0500 Subject: [PATCH 6/7] Clarify comment. Co-authored-by: David Robertson --- synapse/handlers/sync.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/synapse/handlers/sync.py b/synapse/handlers/sync.py index 86f8bf876869..cc3c2de513f1 100644 --- a/synapse/handlers/sync.py +++ b/synapse/handlers/sync.py @@ -1461,7 +1461,7 @@ async def generate_sync_result( # Presence data is included if the server has it enabled and: # - There is a sync token, or - # - Is not filtered out. + # - Presence is not filtered out. include_presence_data = self.hs_config.server.use_presence and ( since_token is not None or not sync_config.filter_collection.blocks_all_presence() From b02780bb1367f5cb569dfb970f2a099da8b7aaec Mon Sep 17 00:00:00 2001 From: Patrick Cloke Date: Thu, 2 Feb 2023 09:17:30 -0500 Subject: [PATCH 7/7] More simplification. --- synapse/handlers/sync.py | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/synapse/handlers/sync.py b/synapse/handlers/sync.py index cc3c2de513f1..0cb8d5ef4b66 100644 --- a/synapse/handlers/sync.py +++ b/synapse/handlers/sync.py @@ -1459,12 +1459,10 @@ async def generate_sync_result( sync_result_builder, account_data_by_room ) - # Presence data is included if the server has it enabled and: - # - There is a sync token, or - # - Presence is not filtered out. - include_presence_data = self.hs_config.server.use_presence and ( - since_token is not None - or not sync_config.filter_collection.blocks_all_presence() + # Presence data is included if the server has it enabled and not filtered out. + include_presence_data = ( + self.hs_config.server.use_presence + and not sync_config.filter_collection.blocks_all_presence() ) if include_presence_data: logger.debug("Fetching presence data") @@ -1845,15 +1843,12 @@ async def _generate_sync_entry_for_rooms( """ since_token = sync_result_builder.since_token - - # 1. Start by fetching all ephemeral events in rooms we've joined (if required). user_id = sync_result_builder.sync_config.user.to_string() - block_all_room_ephemeral = ( - since_token is None - and sync_result_builder.sync_config.filter_collection.blocks_all_room_ephemeral() - ) - if block_all_room_ephemeral: + # 1. Start by fetching all ephemeral events in rooms we've joined (if required). + if ( + sync_result_builder.sync_config.filter_collection.blocks_all_room_ephemeral() + ): ephemeral_by_room: Dict[str, List[JsonDict]] = {} else: now_token, ephemeral_by_room = await self.ephemeral_by_room(