mirror of
https://github.com/Rapptz/discord.py.git
synced 2025-05-15 18:29:52 +00:00
Optimise tight loops in DiscordGateway.received_message
* type(x) is y is faster than isinstance(x, y) * Re-arrange if-statements for common statements * Drop handler getattr for most events that don't use it
This commit is contained in:
parent
095f0ec2fc
commit
2721689254
@ -125,7 +125,11 @@ class Client:
|
|||||||
proxy_auth = options.pop('proxy_auth', None)
|
proxy_auth = options.pop('proxy_auth', None)
|
||||||
self.http = HTTPClient(connector, proxy=proxy, proxy_auth=proxy_auth, loop=self.loop)
|
self.http = HTTPClient(connector, proxy=proxy, proxy_auth=proxy_auth, loop=self.loop)
|
||||||
|
|
||||||
self._connection = ConnectionState(dispatch=self.dispatch, chunker=self._chunker,
|
self._handlers = {
|
||||||
|
'ready': self._handle_ready
|
||||||
|
}
|
||||||
|
|
||||||
|
self._connection = ConnectionState(dispatch=self.dispatch, chunker=self._chunker, handlers=self._handlers,
|
||||||
syncer=self._syncer, http=self.http, loop=self.loop, **options)
|
syncer=self._syncer, http=self.http, loop=self.loop, **options)
|
||||||
|
|
||||||
self._connection.shard_count = self.shard_count
|
self._connection.shard_count = self.shard_count
|
||||||
@ -261,13 +265,6 @@ class Client:
|
|||||||
for idx in reversed(removed):
|
for idx in reversed(removed):
|
||||||
del listeners[idx]
|
del listeners[idx]
|
||||||
|
|
||||||
try:
|
|
||||||
actual_handler = getattr(self, handler)
|
|
||||||
except AttributeError:
|
|
||||||
pass
|
|
||||||
else:
|
|
||||||
actual_handler(*args, **kwargs)
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
coro = getattr(self, method)
|
coro = getattr(self, method)
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
|
@ -312,7 +312,7 @@ class DiscordWebSocket(websockets.client.WebSocketClientProtocol):
|
|||||||
async def received_message(self, msg):
|
async def received_message(self, msg):
|
||||||
self._dispatch('socket_raw_receive', msg)
|
self._dispatch('socket_raw_receive', msg)
|
||||||
|
|
||||||
if isinstance(msg, bytes):
|
if type(msg) is bytes:
|
||||||
self._buffer.extend(msg)
|
self._buffer.extend(msg)
|
||||||
|
|
||||||
if len(msg) >= 4:
|
if len(msg) >= 4:
|
||||||
@ -336,6 +336,7 @@ class DiscordWebSocket(websockets.client.WebSocketClientProtocol):
|
|||||||
if seq is not None:
|
if seq is not None:
|
||||||
self.sequence = seq
|
self.sequence = seq
|
||||||
|
|
||||||
|
if op != self.DISPATCH:
|
||||||
if op == self.RECONNECT:
|
if op == self.RECONNECT:
|
||||||
# "reconnect" can only be handled by the Client
|
# "reconnect" can only be handled by the Client
|
||||||
# so we terminate our connection and raise an
|
# so we terminate our connection and raise an
|
||||||
@ -373,7 +374,6 @@ class DiscordWebSocket(websockets.client.WebSocketClientProtocol):
|
|||||||
await self.identify()
|
await self.identify()
|
||||||
return
|
return
|
||||||
|
|
||||||
if op != self.DISPATCH:
|
|
||||||
log.warning('Unknown OP code %s.', op)
|
log.warning('Unknown OP code %s.', op)
|
||||||
return
|
return
|
||||||
|
|
||||||
@ -386,7 +386,7 @@ class DiscordWebSocket(websockets.client.WebSocketClientProtocol):
|
|||||||
log.info('Shard ID %s has connected to Gateway: %s (Session ID: %s).',
|
log.info('Shard ID %s has connected to Gateway: %s (Session ID: %s).',
|
||||||
self.shard_id, ', '.join(trace), self.session_id)
|
self.shard_id, ', '.join(trace), self.session_id)
|
||||||
|
|
||||||
if event == 'RESUMED':
|
elif event == 'RESUMED':
|
||||||
self._trace = trace = data.get('_trace', [])
|
self._trace = trace = data.get('_trace', [])
|
||||||
log.info('Shard ID %s has successfully RESUMED session %s under trace %s.',
|
log.info('Shard ID %s has successfully RESUMED session %s under trace %s.',
|
||||||
self.shard_id, self.session_id, ', '.join(trace))
|
self.shard_id, self.session_id, ', '.join(trace))
|
||||||
|
@ -124,7 +124,8 @@ class AutoShardedClient(Client):
|
|||||||
raise ClientException('shard_ids parameter must be a list or a tuple.')
|
raise ClientException('shard_ids parameter must be a list or a tuple.')
|
||||||
|
|
||||||
self._connection = AutoShardedConnectionState(dispatch=self.dispatch, chunker=self._chunker,
|
self._connection = AutoShardedConnectionState(dispatch=self.dispatch, chunker=self._chunker,
|
||||||
syncer=self._syncer, http=self.http, loop=self.loop, **kwargs)
|
handlers=self._handlers, syncer=self._syncer,
|
||||||
|
http=self.http, loop=self.loop, **kwargs)
|
||||||
|
|
||||||
# instead of a single websocket, we have multiple
|
# instead of a single websocket, we have multiple
|
||||||
# the key is the shard_id
|
# the key is the shard_id
|
||||||
|
@ -54,7 +54,7 @@ log = logging.getLogger(__name__)
|
|||||||
ReadyState = namedtuple('ReadyState', ('launch', 'guilds'))
|
ReadyState = namedtuple('ReadyState', ('launch', 'guilds'))
|
||||||
|
|
||||||
class ConnectionState:
|
class ConnectionState:
|
||||||
def __init__(self, *, dispatch, chunker, syncer, http, loop, **options):
|
def __init__(self, *, dispatch, chunker, handlers, syncer, http, loop, **options):
|
||||||
self.loop = loop
|
self.loop = loop
|
||||||
self.http = http
|
self.http = http
|
||||||
self.max_messages = max(options.get('max_messages', 5000), 100)
|
self.max_messages = max(options.get('max_messages', 5000), 100)
|
||||||
@ -62,6 +62,7 @@ class ConnectionState:
|
|||||||
self.chunker = chunker
|
self.chunker = chunker
|
||||||
self.syncer = syncer
|
self.syncer = syncer
|
||||||
self.is_bot = None
|
self.is_bot = None
|
||||||
|
self.handlers = handlers
|
||||||
self.shard_count = None
|
self.shard_count = None
|
||||||
self._ready_task = None
|
self._ready_task = None
|
||||||
self._fetch_offline = options.get('fetch_offline_members', True)
|
self._fetch_offline = options.get('fetch_offline_members', True)
|
||||||
@ -127,6 +128,14 @@ class ConnectionState:
|
|||||||
for index in reversed(removed):
|
for index in reversed(removed):
|
||||||
del self._listeners[index]
|
del self._listeners[index]
|
||||||
|
|
||||||
|
def call_handlers(self, key, *args, **kwargs):
|
||||||
|
try:
|
||||||
|
func = self.handlers[key]
|
||||||
|
except KeyError:
|
||||||
|
pass
|
||||||
|
else:
|
||||||
|
func(*args, **kwargs)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def self_id(self):
|
def self_id(self):
|
||||||
u = self.user
|
u = self.user
|
||||||
@ -308,6 +317,7 @@ class ConnectionState:
|
|||||||
pass
|
pass
|
||||||
else:
|
else:
|
||||||
# dispatch the event
|
# dispatch the event
|
||||||
|
self.call_handlers('ready')
|
||||||
self.dispatch('ready')
|
self.dispatch('ready')
|
||||||
finally:
|
finally:
|
||||||
self._ready_task = None
|
self._ready_task = None
|
||||||
@ -960,6 +970,7 @@ class AutoShardedConnectionState(ConnectionState):
|
|||||||
self._ready_task = None
|
self._ready_task = None
|
||||||
|
|
||||||
# dispatch the event
|
# dispatch the event
|
||||||
|
self.call_handlers('ready')
|
||||||
self.dispatch('ready')
|
self.dispatch('ready')
|
||||||
|
|
||||||
def parse_ready(self, data):
|
def parse_ready(self, data):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user