Skip to content

Commit bec0313

Browse files
committed
Improve perf of sync device lists (#17191)
It's almost always more efficient to query the rooms that have device list changes, rather than looking at the list of all users whose devices have changed and then look for shared rooms.
1 parent 38f03a0 commit bec0313

File tree

3 files changed

+9
-46
lines changed

3 files changed

+9
-46
lines changed

changelog.d/17191.misc

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Improve performance of calculating device lists changes in `/sync`.

synapse/handlers/sync.py

+6-31
Original file line numberDiff line numberDiff line change
@@ -1886,38 +1886,13 @@ async def _generate_sync_entry_for_device_list(
18861886

18871887
# Step 1a, check for changes in devices of users we share a room
18881888
# with
1889-
#
1890-
# We do this in two different ways depending on what we have cached.
1891-
# If we already have a list of all the user that have changed since
1892-
# the last sync then it's likely more efficient to compare the rooms
1893-
# they're in with the rooms the syncing user is in.
1894-
#
1895-
# If we don't have that info cached then we get all the users that
1896-
# share a room with our user and check if those users have changed.
1897-
cache_result = self.store.get_cached_device_list_changes(
1898-
since_token.device_list_key
1899-
)
1900-
if cache_result.hit:
1901-
changed_users = cache_result.entities
1902-
1903-
result = await self.store.get_rooms_for_users(changed_users)
1904-
1905-
for changed_user_id, entries in result.items():
1906-
# Check if the changed user shares any rooms with the user,
1907-
# or if the changed user is the syncing user (as we always
1908-
# want to include device list updates of their own devices).
1909-
if user_id == changed_user_id or any(
1910-
rid in joined_room_ids for rid in entries
1911-
):
1912-
users_that_have_changed.add(changed_user_id)
1913-
else:
1914-
users_that_have_changed = (
1915-
await self._device_handler.get_device_changes_in_shared_rooms(
1916-
user_id,
1917-
sync_result_builder.joined_room_ids,
1918-
from_token=since_token,
1919-
)
1889+
users_that_have_changed = (
1890+
await self._device_handler.get_device_changes_in_shared_rooms(
1891+
user_id,
1892+
sync_result_builder.joined_room_ids,
1893+
from_token=since_token,
19201894
)
1895+
)
19211896

19221897
# Step 1b, check for newly joined rooms
19231898
for room_id in newly_joined_rooms:

synapse/storage/databases/main/devices.py

+2-15
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,7 @@
7070
from synapse.util import json_decoder, json_encoder
7171
from synapse.util.caches.descriptors import cached, cachedList
7272
from synapse.util.caches.lrucache import LruCache
73-
from synapse.util.caches.stream_change_cache import (
74-
AllEntitiesChangedResult,
75-
StreamChangeCache,
76-
)
73+
from synapse.util.caches.stream_change_cache import StreamChangeCache
7774
from synapse.util.cancellation import cancellable
7875
from synapse.util.iterutils import batch_iter
7976
from synapse.util.stringutils import shortstr
@@ -832,16 +829,6 @@ async def get_cached_devices_for_user(
832829
)
833830
return {device[0]: db_to_json(device[1]) for device in devices}
834831

835-
def get_cached_device_list_changes(
836-
self,
837-
from_key: int,
838-
) -> AllEntitiesChangedResult:
839-
"""Get set of users whose devices have changed since `from_key`, or None
840-
if that information is not in our cache.
841-
"""
842-
843-
return self._device_list_stream_cache.get_all_entities_changed(from_key)
844-
845832
@cancellable
846833
async def get_all_devices_changed(
847834
self,
@@ -1475,7 +1462,7 @@ async def get_device_list_changes_in_rooms(
14751462

14761463
sql = """
14771464
SELECT DISTINCT user_id FROM device_lists_changes_in_room
1478-
WHERE {clause} AND stream_id >= ?
1465+
WHERE {clause} AND stream_id > ?
14791466
"""
14801467

14811468
def _get_device_list_changes_in_rooms_txn(

0 commit comments

Comments
 (0)