diff options
| author | Rapptz <[email protected]> | 2020-01-06 22:13:35 -0500 |
|---|---|---|
| committer | Rapptz <[email protected]> | 2020-01-06 22:13:35 -0500 |
| commit | de5caf59d65351ee918fcce852ae690264e438ce (patch) | |
| tree | 00d04bea27ea09fe103e8b55e4323834e7011d7f | |
| parent | [commands] Add check_any check to OR together various checks (diff) | |
| download | discord.py-de5caf59d65351ee918fcce852ae690264e438ce.tar.xz discord.py-de5caf59d65351ee918fcce852ae690264e438ce.zip | |
Cache member.roles access to avoid surprising performance traps
Without the cache, repeated access could be accidentally quadratic or
worse.
| -rw-r--r-- | discord/member.py | 14 |
1 files changed, 12 insertions, 2 deletions
diff --git a/discord/member.py b/discord/member.py index e660187e..70acedd6 100644 --- a/discord/member.py +++ b/discord/member.py @@ -161,7 +161,8 @@ class Member(discord.abc.Messageable, _BaseUser): Nitro boost on the guild, if available. This could be ``None``. """ - __slots__ = ('_roles', 'joined_at', 'premium_since', '_client_status', 'activities', 'guild', 'nick', '_user', '_state') + __slots__ = ('_roles', '_cs_roles', 'joined_at', 'premium_since', '_client_status', + 'activities', 'guild', 'nick', '_user', '_state') def __init__(self, *, data, guild, state): self._state = state @@ -233,6 +234,11 @@ class Member(discord.abc.Messageable, _BaseUser): self.activities = member.activities self._state = member._state + try: + del self._cs_roles + except AttributeError: + pass + # Reference will not be copied unless necessary by PRESENCE_UPDATE # See below self._user = member._user @@ -244,6 +250,10 @@ class Member(discord.abc.Messageable, _BaseUser): def _update_roles(self, data): self._roles = utils.SnowflakeList(map(int, data['roles'])) + try: + del self._cs_roles + except AttributeError: + pass def _update(self, data): # the nickname change is optional, @@ -334,7 +344,7 @@ class Member(discord.abc.Messageable, _BaseUser): """ return self.colour - @property + @utils.cached_slot_property('_cs_roles') def roles(self): """List[:class:`Role`]: A :class:`list` of :class:`Role` that the member belongs to. Note that the first element of this list is always the default '@everyone' |