From 095f0ec2fc48ff353bd3bee21d0055b99fdc3aa9 Mon Sep 17 00:00:00 2001 From: Rapptz Date: Mon, 24 Sep 2018 22:50:05 -0400 Subject: [PATCH] Optimise some member and user related routines. * Skip user update if possible * Drop copy.copy in favour of manual copy Credit to Hornwitser for these findings. --- discord/member.py | 23 +++++++++++++++++++---- discord/state.py | 2 +- discord/user.py | 13 +++++++++++++ 3 files changed, 33 insertions(+), 5 deletions(-) diff --git a/discord/member.py b/discord/member.py index 009a21043..f265a1be7 100644 --- a/discord/member.py +++ b/discord/member.py @@ -178,6 +178,20 @@ class Member(discord.abc.Messageable, _BaseUser): def __hash__(self): return hash(self._user) + @classmethod + def _copy(cls, member): + self = cls.__new__(cls) # to bypass __init__ + + self._roles = utils.SnowflakeList(member._roles, is_sorted=True) + self.joined_at = member.joined_at + self.status = member.status + self.guild = member.guild + self.nick = member.nick + self.activity = member.activity + self._state = member._state + self._user = User._copy(member._user) + return self + async def _get_channel(self): ch = await self.create_dm() return ch @@ -205,10 +219,11 @@ class Member(discord.abc.Messageable, _BaseUser): self.status = try_enum(Status, data['status']) self.activity = create_activity(data.get('game')) - u = self._user - u.name = user.get('username', u.name) - u.avatar = user.get('avatar', u.avatar) - u.discriminator = user.get('discriminator', u.discriminator) + if len(user) > 1: + u = self._user + u.name = user.get('username', u.name) + u.avatar = user.get('avatar', u.avatar) + u.discriminator = user.get('discriminator', u.discriminator) def _copy(self): c = copy.copy(self) diff --git a/discord/state.py b/discord/state.py index 7180509a0..676ca513b 100644 --- a/discord/state.py +++ b/discord/state.py @@ -449,7 +449,7 @@ class ConnectionState: member = Member(guild=guild, data=data, state=self) guild._add_member(member) - old_member = member._copy() + old_member = Member._copy(member) member._presence_update(data=data, user=user) self.dispatch('member_update', old_member, member) diff --git a/discord/user.py b/discord/user.py index bce31d69e..07a173699 100644 --- a/discord/user.py +++ b/discord/user.py @@ -91,6 +91,19 @@ class BaseUser(_BaseUser): def __hash__(self): return self.id >> 22 + @classmethod + def _copy(cls, user): + self = cls.__new__(cls) # bypass __init__ + + self.name = user.name + self.id = user.id + self.discriminator = user.discriminator + self.avatar = user.avatar + self.bot = user.bot + self._state = user._state + + return self + @property def avatar_url(self): """Returns a friendly URL version of the avatar the user has.