Change certain sequences to use a special proxy type instead of list

This is to speed up cases where someone is just querying the length
of the underlying sequence. If anything else is done to the sequence
then it is copied from the original iterator.

This change should be mostly transparent.
This commit is contained in:
Rapptz
2022-07-19 01:31:33 -04:00
parent 98c4f2ae8b
commit 87bc79e6e3
6 changed files with 75 additions and 45 deletions

View File

@ -378,8 +378,8 @@ class ConnectionState:
return self._view_store.persistent_views
@property
def guilds(self) -> List[Guild]:
return list(self._guilds.values())
def guilds(self) -> Sequence[Guild]:
return utils.SequenceProxy(self._guilds.values())
def _get_guild(self, guild_id: Optional[int]) -> Optional[Guild]:
# the keys of self._guilds are ints
@ -403,12 +403,12 @@ class ConnectionState:
del guild
@property
def emojis(self) -> List[Emoji]:
return list(self._emojis.values())
def emojis(self) -> Sequence[Emoji]:
return utils.SequenceProxy(self._emojis.values())
@property
def stickers(self) -> List[GuildSticker]:
return list(self._stickers.values())
def stickers(self) -> Sequence[GuildSticker]:
return utils.SequenceProxy(self._stickers.values())
def get_emoji(self, emoji_id: Optional[int]) -> Optional[Emoji]:
# the keys of self._emojis are ints
@ -419,8 +419,8 @@ class ConnectionState:
return self._stickers.get(sticker_id) # type: ignore
@property
def private_channels(self) -> List[PrivateChannel]:
return list(self._private_channels.values())
def private_channels(self) -> Sequence[PrivateChannel]:
return utils.SequenceProxy(self._private_channels.values())
def _get_private_channel(self, channel_id: Optional[int]) -> Optional[PrivateChannel]:
try: