Skip to content

Commit

Permalink
Small performance improvements for sliding sync (#17672)
Browse files Browse the repository at this point in the history
A couple of small performance improvements for sliding sync.
  • Loading branch information
erikjohnston authored Sep 6, 2024
1 parent 786de85 commit a708e1a
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 17 deletions.
1 change: 1 addition & 0 deletions changelog.d/17672.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Small performance improvement in speeding up sliding sync.
15 changes: 10 additions & 5 deletions synapse/handlers/sliding_sync/room_lists.py
Original file line number Diff line number Diff line change
Expand Up @@ -350,13 +350,18 @@ async def _compute_interested_rooms_new_tables(

all_rooms.update(filtered_sync_room_map)

# Sort the list
sorted_room_info = await self.sort_rooms_using_tables(
filtered_sync_room_map, to_token
)

ops: List[SlidingSyncResult.SlidingWindowList.Operation] = []

if list_config.ranges:
if list_config.ranges == [(0, len(filtered_sync_room_map) - 1)]:
# If we are asking for the full range, we don't need to sort the list.
sorted_room_info = list(filtered_sync_room_map.values())
else:
# Sort the list
sorted_room_info = await self.sort_rooms_using_tables(
filtered_sync_room_map, to_token
)

for range in list_config.ranges:
room_ids_in_list: List[str] = []

Expand Down
8 changes: 7 additions & 1 deletion synapse/types/handlers/sliding_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from typing import (
TYPE_CHECKING,
AbstractSet,
Any,
Callable,
Dict,
Final,
Expand Down Expand Up @@ -703,7 +704,12 @@ def previously(last_token: T) -> "HaveSentRoom[T]":

@staticmethod
def never() -> "HaveSentRoom[T]":
return HaveSentRoom(HaveSentRoomFlag.NEVER, None)
# We use a singleton to avoid repeatedly instantiating new `never`
# values.
return _HAVE_SENT_ROOM_NEVER


_HAVE_SENT_ROOM_NEVER: HaveSentRoom[Any] = HaveSentRoom(HaveSentRoomFlag.NEVER, None)


@attr.s(auto_attribs=True, slots=True, frozen=True)
Expand Down
19 changes: 8 additions & 11 deletions tests/rest/client/sliding_sync/test_rooms_meta.py
Original file line number Diff line number Diff line change
Expand Up @@ -588,19 +588,16 @@ def test_rooms_bump_stamp(self) -> None:
)

# Make sure the list includes the rooms in the right order
self.assertListEqual(
list(response_body["lists"]["foo-list"]["ops"]),
[
{
"op": "SYNC",
"range": [0, 1],
# room1 sorts before room2 because it has the latest event (the
# reaction)
"room_ids": [room_id1, room_id2],
}
],
self.assertEqual(
len(response_body["lists"]["foo-list"]["ops"]),
1,
response_body["lists"]["foo-list"],
)
op = response_body["lists"]["foo-list"]["ops"][0]
self.assertEqual(op["op"], "SYNC")
self.assertEqual(op["range"], [0, 1])
# Note that we don't order the ops anymore, so we need to compare sets.
self.assertIncludes(set(op["room_ids"]), {room_id1, room_id2}, exact=True)

# The `bump_stamp` for room1 should point at the latest message (not the
# reaction since it's not one of the `DEFAULT_BUMP_EVENT_TYPES`)
Expand Down

0 comments on commit a708e1a

Please sign in to comment.