From fcf3099c37071c3a510ca122712dac45f67fe051 Mon Sep 17 00:00:00 2001 From: Erik Johnston Date: Sun, 31 Dec 2023 11:21:43 +0000 Subject: [PATCH 1/3] Faster load recents for sync --- synapse/handlers/sync.py | 14 ++++++++------ synapse/storage/databases/main/state.py | 16 +++++++++++++++- 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/synapse/handlers/sync.py b/synapse/handlers/sync.py index 1152c0158f..75250540c5 100644 --- a/synapse/handlers/sync.py +++ b/synapse/handlers/sync.py @@ -583,10 +583,11 @@ async def _load_filtered_recents( # `recents`, so partial state is only a problem when a membership # event turns up in `recents` but has not made it into the current # state. - current_state_ids_map = ( - await self.store.get_partial_current_state_ids(room_id) + current_state_ids = ( + await self.store.check_if_events_in_current_state( + {e.event_id for e in recents if e.is_state()} + ) ) - current_state_ids = frozenset(current_state_ids_map.values()) recents = await filter_events_for_client( self._storage_controllers, @@ -664,10 +665,11 @@ async def _load_filtered_recents( # `loaded_recents`, so partial state is only a problem when a # membership event turns up in `loaded_recents` but has not made it # into the current state. - current_state_ids_map = ( - await self.store.get_partial_current_state_ids(room_id) + current_state_ids = ( + await self.store.check_if_events_in_current_state( + {e.event_id for e in loaded_recents if e.is_state()} + ) ) - current_state_ids = frozenset(current_state_ids_map.values()) loaded_recents = await filter_events_for_client( self._storage_controllers, diff --git a/synapse/storage/databases/main/state.py b/synapse/storage/databases/main/state.py index 4700e74ad2..aa20b832e7 100644 --- a/synapse/storage/databases/main/state.py +++ b/synapse/storage/databases/main/state.py @@ -24,6 +24,7 @@ Any, Collection, Dict, + FrozenSet, Iterable, List, Mapping, @@ -52,7 +53,7 @@ ) from synapse.storage.databases.main.events_worker import EventsWorkerStore from synapse.storage.databases.main.roommember import RoomMemberWorkerStore -from synapse.types import JsonDict, JsonMapping, StateMap +from synapse.types import JsonDict, JsonMapping, StateMap, StrCollection from synapse.types.state import StateFilter from synapse.util.caches import intern_string from synapse.util.caches.descriptors import cached, cachedList @@ -318,6 +319,19 @@ def _get_current_state_ids_txn(txn: LoggingTransaction) -> StateMap[str]: "get_partial_current_state_ids", _get_current_state_ids_txn ) + async def check_if_events_in_current_state( + self, event_ids: StrCollection + ) -> FrozenSet[str]: + rows = await self.db_pool.simple_select_many_batch( + table="current_state_events", + column="event_id", + iterable=event_ids, + retcols=("event_id",), + desc="check_if_events_in_current_state", + ) + + return frozenset(event_id for event_id, in rows) + # FIXME: how should this be cached? @cancellable async def get_partial_filtered_current_state_ids( From 26c72a83cf5814240210effa420d918d9045392b Mon Sep 17 00:00:00 2001 From: Erik Johnston Date: Fri, 5 Jan 2024 17:02:29 +0000 Subject: [PATCH 2/3] Newsfile --- changelog.d/16783.misc | 1 + 1 file changed, 1 insertion(+) create mode 100644 changelog.d/16783.misc diff --git a/changelog.d/16783.misc b/changelog.d/16783.misc new file mode 100644 index 0000000000..9d3b96ffc6 --- /dev/null +++ b/changelog.d/16783.misc @@ -0,0 +1 @@ +Faster load recents for sync by reducing amount of state pulled out. From b315b41e66d4f95054188ad1c77cc746e8995375 Mon Sep 17 00:00:00 2001 From: Erik Johnston Date: Wed, 10 Jan 2024 14:13:33 +0000 Subject: [PATCH 3/3] Docstring --- synapse/storage/databases/main/state.py | 1 + 1 file changed, 1 insertion(+) diff --git a/synapse/storage/databases/main/state.py b/synapse/storage/databases/main/state.py index aa20b832e7..2d214476fc 100644 --- a/synapse/storage/databases/main/state.py +++ b/synapse/storage/databases/main/state.py @@ -322,6 +322,7 @@ def _get_current_state_ids_txn(txn: LoggingTransaction) -> StateMap[str]: async def check_if_events_in_current_state( self, event_ids: StrCollection ) -> FrozenSet[str]: + """Checks and returns which of the given events is part of the current state.""" rows = await self.db_pool.simple_select_many_batch( table="current_state_events", column="event_id",