From 00a0856cc44549a29d5123b997db3a5e593f0896 Mon Sep 17 00:00:00 2001 From: Rapptz Date: Wed, 29 May 2019 01:22:36 -0400 Subject: Use a dict instead of getattr for parsing events. Probably not a significant difference but might as well use it here. The basic idea is to cache the getattr calls instead of repeatedly doing it (since they're around 105ns on my machine). The dictionary lookup is about 41ns on my machine. The next step in speeding up library code some more should be in the parser bodies themselves but that's a problem to tackle another day. --- discord/gateway.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) (limited to 'discord/gateway.py') diff --git a/discord/gateway.py b/discord/gateway.py index bf51377f..aa2c02ee 100644 --- a/discord/gateway.py +++ b/discord/gateway.py @@ -227,6 +227,7 @@ class DiscordWebSocket(websockets.client.WebSocketClientProtocol): # dynamically add attributes needed ws.token = client.http.token ws._connection = client._connection + ws._discord_parsers = client._connection.parsers ws._dispatch = client.dispatch ws.gateway = gateway ws.shard_id = shard_id @@ -414,11 +415,9 @@ class DiscordWebSocket(websockets.client.WebSocketClientProtocol): log.info('Shard ID %s has successfully RESUMED session %s under trace %s.', self.shard_id, self.session_id, ', '.join(trace)) - parser = 'parse_' + event.lower() - try: - func = getattr(self._connection, parser) - except AttributeError: + func = self._discord_parsers[event] + except KeyError: log.warning('Unknown event %s.', event) else: func(data) -- cgit v1.2.3