Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Improve /sync performance of when passing filters with empty arrays #14786

Merged
merged 5 commits into from
Jan 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/14786.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Improve performance of `/sync` when filtering all rooms, message types, or senders.
13 changes: 8 additions & 5 deletions synapse/api/filtering.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,9 @@ async def filter_room_account_data(
await self._room_filter.filter(events)
)

def blocks_all_rooms(self) -> bool:
return self._room_filter.filters_all_rooms()

def blocks_all_presence(self) -> bool:
return (
self._presence_filter.filters_all_types()
Expand Down Expand Up @@ -351,13 +354,13 @@ def __init__(self, hs: "HomeServer", filter_json: JsonDict):
self.not_rel_types = filter_json.get("org.matrix.msc3874.not_rel_types", [])

def filters_all_types(self) -> bool:
return "*" in self.not_types
return self.types == [] or "*" in self.not_types

def filters_all_senders(self) -> bool:
return "*" in self.not_senders
return self.senders == [] or "*" in self.not_senders

def filters_all_rooms(self) -> bool:
return "*" in self.not_rooms
return self.rooms == [] or "*" in self.not_rooms
MatMaul marked this conversation as resolved.
Show resolved Hide resolved

def _check(self, event: FilterEvent) -> bool:
"""Checks whether the filter matches the given event.
Expand Down Expand Up @@ -450,8 +453,8 @@ def _check_fields(self, field_matchers: Dict[str, Callable[[str], bool]]) -> boo
if any(map(match_func, disallowed_values)):
return False

# Other the event does not match at least one of the allowed values,
# reject it.
# Otherwise if the event does not match at least one of the allowed
# values, reject it.
allowed_values = getattr(self, name)
if allowed_values is not None:
if not any(map(match_func, allowed_values)):
Expand Down
2 changes: 1 addition & 1 deletion synapse/handlers/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ async def _search(
)
room_ids = {r.room_id for r in rooms}

# If doing a subset of all rooms seearch, check if any of the rooms
# If doing a subset of all rooms search, check if any of the rooms
# are from an upgraded room, and search their contents as well
if search_filter.rooms:
historical_room_ids: List[str] = []
Expand Down
14 changes: 11 additions & 3 deletions synapse/handlers/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -1403,11 +1403,14 @@ async def generate_sync_result(

logger.debug("Fetching room data")

res = await self._generate_sync_entry_for_rooms(
(
newly_joined_rooms,
newly_joined_or_invited_or_knocked_users,
newly_left_rooms,
newly_left_users,
) = await self._generate_sync_entry_for_rooms(
sync_result_builder, account_data_by_room
)
newly_joined_rooms, newly_joined_or_invited_or_knocked_users, _, _ = res
_, _, newly_left_rooms, newly_left_users = res

block_all_presence_data = (
since_token is None and sync_config.filter_collection.blocks_all_presence()
Expand Down Expand Up @@ -1789,6 +1792,11 @@ async def _generate_sync_entry_for_rooms(
- newly_left_rooms
- newly_left_users
"""

# If the request doesn't care about rooms then nothing to do!
if sync_result_builder.sync_config.filter_collection.blocks_all_rooms():
return set(), set(), set(), set()

since_token = sync_result_builder.since_token

# 1. Start by fetching all ephemeral events in rooms we've joined (if required).
Expand Down