Use sets instead of isinstance in StateCache too

Similar reasoning to the change for EntityCache, sets are faster
than attribute lookups (into types.) and isinstance (another global
lookup). Updating the state is also very common, so it should be
as fast as possible.
This commit is contained in:
Lonami Exo 2019-05-01 16:37:54 +02:00
parent 9a400748f7
commit e84c9847c5

View File

@ -27,7 +27,7 @@ class StateCache:
update, update,
*, *,
channel_id=None, channel_id=None,
has_pts=( has_pts=frozenset(x.CONSTRUCTOR_ID for x in (
types.UpdateNewMessage, types.UpdateNewMessage,
types.UpdateDeleteMessages, types.UpdateDeleteMessages,
types.UpdateReadHistoryInbox, types.UpdateReadHistoryInbox,
@ -40,8 +40,8 @@ class StateCache:
types.UpdateShortMessage, types.UpdateShortMessage,
types.UpdateShortChatMessage, types.UpdateShortChatMessage,
types.UpdateShortSentMessage types.UpdateShortSentMessage
), )),
has_date=( has_date=frozenset(x.CONSTRUCTOR_ID for x in (
types.UpdateUserPhoto, types.UpdateUserPhoto,
types.UpdateEncryption, types.UpdateEncryption,
types.UpdateEncryptedMessagesRead, types.UpdateEncryptedMessagesRead,
@ -53,8 +53,8 @@ class StateCache:
types.UpdatesCombined, types.UpdatesCombined,
types.Updates, types.Updates,
types.UpdateShortSentMessage, types.UpdateShortSentMessage,
), )),
has_channel_pts=( has_channel_pts=frozenset(x.CONSTRUCTOR_ID for x in (
types.UpdateChannelTooLong, types.UpdateChannelTooLong,
types.UpdateNewChannelMessage, types.UpdateNewChannelMessage,
types.UpdateDeleteChannelMessages, types.UpdateDeleteChannelMessages,
@ -63,22 +63,21 @@ class StateCache:
types.updates.ChannelDifferenceEmpty, types.updates.ChannelDifferenceEmpty,
types.updates.ChannelDifferenceTooLong, types.updates.ChannelDifferenceTooLong,
types.updates.ChannelDifference types.updates.ChannelDifference
) ))
): ):
""" """
Update the state with the given update. Update the state with the given update.
""" """
has_pts = isinstance(update, has_pts) cid = update.CONSTRUCTOR_ID
has_date = isinstance(update, has_date) if cid in has_pts:
has_channel_pts = isinstance(update, has_channel_pts) if cid in has_date:
if has_pts and has_date: self._pts_date = update.pts, update.date
self._pts_date = update.pts, update.date else:
elif has_pts: self._pts_date = update.pts, self._pts_date[1]
self._pts_date = update.pts, self._pts_date[1] elif cid in has_date:
elif has_date:
self._pts_date = self._pts_date[0], update.date self._pts_date = self._pts_date[0], update.date
if has_channel_pts: if cid in has_channel_pts:
if channel_id is None: if channel_id is None:
channel_id = self.get_channel_id(update) channel_id = self.get_channel_id(update)
@ -91,20 +90,26 @@ class StateCache:
def get_channel_id( def get_channel_id(
self, self,
update, update,
has_channel_id=( has_channel_id=frozenset(x.CONSTRUCTOR_ID for x in (
types.UpdateChannelTooLong, types.UpdateChannelTooLong,
types.UpdateDeleteChannelMessages, types.UpdateDeleteChannelMessages,
types.UpdateChannelWebPage types.UpdateChannelWebPage
), )),
has_message=( has_message=frozenset(x.CONSTRUCTOR_ID for x in (
types.UpdateNewChannelMessage, types.UpdateNewChannelMessage,
types.UpdateEditChannelMessage types.UpdateEditChannelMessage
) ))
): ):
# Will only fail for *difference, where channel_id is known """
if isinstance(update, has_channel_id): Gets the **unmarked** channel ID from this update, if it has any.
Fails for ``*difference`` updates, where ``channel_id``
is supposedly already known from the outside.
"""
cid = update.CONSTRUCTOR_ID
if cid in has_channel_id:
return update.channel_id return update.channel_id
elif isinstance(update, has_message): elif cid in has_message:
if update.message.to_id is None: if update.message.to_id is None:
self._logger.info('Update has None to_id %s', update) self._logger.info('Update has None to_id %s', update)
else: else:
@ -114,7 +119,9 @@ class StateCache:
def __getitem__(self, item): def __getitem__(self, item):
""" """
Gets the corresponding ``(pts, date)`` for the given ID or peer, If `item` is ``None``, returns the default ``(pts, date)``.
If it's an **unmarked** channel ID, returns its ``pts``.
""" """
if item is None: if item is None:
return self._pts_date return self._pts_date