mirror of
https://git.anonymousland.org/anonymousland/synapse.git
synced 2025-05-03 20:24:51 -04:00
Use slots in attrs classes where possible (#8296)
slots use less memory (and attribute access is faster) while slightly limiting the flexibility of the class attributes. This focuses on objects which are instantiated "often" and for short periods of time.
This commit is contained in:
parent
d2a3eb04a4
commit
aec294ee0d
22 changed files with 33 additions and 50 deletions
|
@ -89,14 +89,12 @@ class TimelineBatch:
|
|||
events = attr.ib(type=List[EventBase])
|
||||
limited = attr.ib(bool)
|
||||
|
||||
def __nonzero__(self) -> bool:
|
||||
def __bool__(self) -> bool:
|
||||
"""Make the result appear empty if there are no updates. This is used
|
||||
to tell if room needs to be part of the sync result.
|
||||
"""
|
||||
return bool(self.events)
|
||||
|
||||
__bool__ = __nonzero__ # python3
|
||||
|
||||
|
||||
# We can't freeze this class, because we need to update it after it's instantiated to
|
||||
# update its unread count. This is because we calculate the unread count for a room only
|
||||
|
@ -114,7 +112,7 @@ class JoinedSyncResult:
|
|||
summary = attr.ib(type=Optional[JsonDict])
|
||||
unread_count = attr.ib(type=int)
|
||||
|
||||
def __nonzero__(self) -> bool:
|
||||
def __bool__(self) -> bool:
|
||||
"""Make the result appear empty if there are no updates. This is used
|
||||
to tell if room needs to be part of the sync result.
|
||||
"""
|
||||
|
@ -127,8 +125,6 @@ class JoinedSyncResult:
|
|||
# else in the result, we don't need to send it.
|
||||
)
|
||||
|
||||
__bool__ = __nonzero__ # python3
|
||||
|
||||
|
||||
@attr.s(slots=True, frozen=True)
|
||||
class ArchivedSyncResult:
|
||||
|
@ -137,26 +133,22 @@ class ArchivedSyncResult:
|
|||
state = attr.ib(type=StateMap[EventBase])
|
||||
account_data = attr.ib(type=List[JsonDict])
|
||||
|
||||
def __nonzero__(self) -> bool:
|
||||
def __bool__(self) -> bool:
|
||||
"""Make the result appear empty if there are no updates. This is used
|
||||
to tell if room needs to be part of the sync result.
|
||||
"""
|
||||
return bool(self.timeline or self.state or self.account_data)
|
||||
|
||||
__bool__ = __nonzero__ # python3
|
||||
|
||||
|
||||
@attr.s(slots=True, frozen=True)
|
||||
class InvitedSyncResult:
|
||||
room_id = attr.ib(type=str)
|
||||
invite = attr.ib(type=EventBase)
|
||||
|
||||
def __nonzero__(self) -> bool:
|
||||
def __bool__(self) -> bool:
|
||||
"""Invited rooms should always be reported to the client"""
|
||||
return True
|
||||
|
||||
__bool__ = __nonzero__ # python3
|
||||
|
||||
|
||||
@attr.s(slots=True, frozen=True)
|
||||
class GroupsSyncResult:
|
||||
|
@ -164,11 +156,9 @@ class GroupsSyncResult:
|
|||
invite = attr.ib(type=JsonDict)
|
||||
leave = attr.ib(type=JsonDict)
|
||||
|
||||
def __nonzero__(self) -> bool:
|
||||
def __bool__(self) -> bool:
|
||||
return bool(self.join or self.invite or self.leave)
|
||||
|
||||
__bool__ = __nonzero__ # python3
|
||||
|
||||
|
||||
@attr.s(slots=True, frozen=True)
|
||||
class DeviceLists:
|
||||
|
@ -181,13 +171,11 @@ class DeviceLists:
|
|||
changed = attr.ib(type=Collection[str])
|
||||
left = attr.ib(type=Collection[str])
|
||||
|
||||
def __nonzero__(self) -> bool:
|
||||
def __bool__(self) -> bool:
|
||||
return bool(self.changed or self.left)
|
||||
|
||||
__bool__ = __nonzero__ # python3
|
||||
|
||||
|
||||
@attr.s
|
||||
@attr.s(slots=True)
|
||||
class _RoomChanges:
|
||||
"""The set of room entries to include in the sync, plus the set of joined
|
||||
and left room IDs since last sync.
|
||||
|
@ -227,7 +215,7 @@ class SyncResult:
|
|||
device_one_time_keys_count = attr.ib(type=JsonDict)
|
||||
groups = attr.ib(type=Optional[GroupsSyncResult])
|
||||
|
||||
def __nonzero__(self) -> bool:
|
||||
def __bool__(self) -> bool:
|
||||
"""Make the result appear empty if there are no updates. This is used
|
||||
to tell if the notifier needs to wait for more events when polling for
|
||||
events.
|
||||
|
@ -243,8 +231,6 @@ class SyncResult:
|
|||
or self.groups
|
||||
)
|
||||
|
||||
__bool__ = __nonzero__ # python3
|
||||
|
||||
|
||||
class SyncHandler:
|
||||
def __init__(self, hs: "HomeServer"):
|
||||
|
@ -2038,7 +2024,7 @@ def _calculate_state(
|
|||
return {event_id_to_key[e]: e for e in state_ids}
|
||||
|
||||
|
||||
@attr.s
|
||||
@attr.s(slots=True)
|
||||
class SyncResultBuilder:
|
||||
"""Used to help build up a new SyncResult for a user
|
||||
|
||||
|
@ -2074,7 +2060,7 @@ class SyncResultBuilder:
|
|||
to_device = attr.ib(type=List[JsonDict], default=attr.Factory(list))
|
||||
|
||||
|
||||
@attr.s
|
||||
@attr.s(slots=True)
|
||||
class RoomSyncResultBuilder:
|
||||
"""Stores information needed to create either a `JoinedSyncResult` or
|
||||
`ArchivedSyncResult`.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue