From 95d8bb2e8551e61d1603f588dbed053344fada80 Mon Sep 17 00:00:00 2001 From: Rapptz Date: Mon, 24 Sep 2018 22:06:49 -0400 Subject: Change internal representation of roles in Member and Emoji. Introduce a new internal type, SnowflakeList, which has better memory footprint over a regular list or set of roles. It is suspected that there will be a 9x reduction of memory for every Emoji instance and a 48 byte saving per Member instance. However, these savings will probably only be evident on larger bots. As a consequence of this change, Member.roles is now computed lazily. Currently I am not sure if I want to do the initial sorting on the SnowflakeList for Member, as this comes with a O(n log n) cost when creating a Member for little purpose since SnowflakeList.has is not overly relied on. If CPU time becomes an issue this might change. --- discord/emoji.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'discord/emoji.py') diff --git a/discord/emoji.py b/discord/emoji.py index 16b5bbd7..facdaa93 100644 --- a/discord/emoji.py +++ b/discord/emoji.py @@ -154,7 +154,7 @@ class Emoji(Hashable): self.id = int(emoji['id']) self.name = emoji['name'] self.animated = emoji.get('animated', False) - self._roles = set(emoji.get('roles', [])) + self._roles = utils.SnowflakeList(map(int, emoji.get('roles', []))) def _iterator(self): for attr in self.__slots__: @@ -187,7 +187,7 @@ class Emoji(Hashable): @property def roles(self): - """List[:class:`Role`]: A list of roles that is allowed to use this emoji. + """List[:class:`Role`]: A :class:`list` of roles that is allowed to use this emoji. If roles is empty, the emoji is unrestricted. """ @@ -195,7 +195,7 @@ class Emoji(Hashable): if guild is None: return [] - return [role for role in guild.roles if role.id in self._roles] + return [role for role in guild.roles if self._roles.has(role.id)] @property def guild(self): -- cgit v1.2.3