Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Faster load recents for sync #16783

Merged
merged 4 commits into from Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/16783.misc
@@ -0,0 +1 @@
Faster load recents for sync by reducing amount of state pulled out.
14 changes: 8 additions & 6 deletions synapse/handlers/sync.py
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
16 changes: 15 additions & 1 deletion synapse/storage/databases/main/state.py
Expand Up @@ -24,6 +24,7 @@
Any,
Collection,
Dict,
FrozenSet,
Iterable,
List,
Mapping,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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]:
erikjohnston marked this conversation as resolved.
Show resolved Hide resolved
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(
Expand Down