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

@@ -562,17 +562,17 @@ class Guild(Hashable):
self._add_thread(Thread(guild=self, state=self._state, data=thread))
@property
def channels(self) -> List[GuildChannel]:
"""List[:class:`abc.GuildChannel`]: A list of channels that belongs to this guild."""
return list(self._channels.values())
def channels(self) -> Sequence[GuildChannel]:
"""Sequence[:class:`abc.GuildChannel`]: A list of channels that belongs to this guild."""
return utils.SequenceProxy(self._channels.values())
@property
def threads(self) -> List[Thread]:
"""List[:class:`Thread`]: A list of threads that you have permission to view.
def threads(self) -> Sequence[Thread]:
"""Sequence[:class:`Thread`]: A list of threads that you have permission to view.
.. versionadded:: 2.0
"""
return list(self._threads.values())
return utils.SequenceProxy(self._threads.values())
@property
def large(self) -> bool:
@@ -817,9 +817,9 @@ class Guild(Hashable):
return self._PREMIUM_GUILD_LIMITS[self.premium_tier].filesize
@property
def members(self) -> List[Member]:
"""List[:class:`Member`]: A list of members that belong to this guild."""
return list(self._members.values())
def members(self) -> Sequence[Member]:
"""Sequence[:class:`Member`]: A list of members that belong to this guild."""
return utils.SequenceProxy(self._members.values())
def get_member(self, user_id: int, /) -> Optional[Member]:
"""Returns a member with the given ID.
@@ -846,13 +846,13 @@ class Guild(Hashable):
return [member for member in self.members if member.premium_since is not None]
@property
def roles(self) -> List[Role]:
"""List[:class:`Role`]: Returns a :class:`list` of the guild's roles in hierarchy order.
def roles(self) -> Sequence[Role]:
"""Sequence[:class:`Role`]: Returns a sequence of the guild's roles in hierarchy order.
The first element of this list will be the lowest role in the
The first element of this sequence will be the lowest role in the
hierarchy.
"""
return sorted(self._roles.values())
return utils.SequenceProxy(self._roles.values(), sorted=True)
def get_role(self, role_id: int, /) -> Optional[Role]:
"""Returns a role with the given ID.
@@ -904,13 +904,13 @@ class Guild(Hashable):
return None
@property
def stage_instances(self) -> List[StageInstance]:
"""List[:class:`StageInstance`]: Returns a :class:`list` of the guild's stage instances that
def stage_instances(self) -> Sequence[StageInstance]:
"""Sequence[:class:`StageInstance`]: Returns a sequence of the guild's stage instances that
are currently running.
.. versionadded:: 2.0
"""
return list(self._stage_instances.values())
return utils.SequenceProxy(self._stage_instances.values())
def get_stage_instance(self, stage_instance_id: int, /) -> Optional[StageInstance]:
"""Returns a stage instance with the given ID.
@@ -930,12 +930,12 @@ class Guild(Hashable):
return self._stage_instances.get(stage_instance_id)
@property
def scheduled_events(self) -> List[ScheduledEvent]:
"""List[:class:`ScheduledEvent`]: Returns a :class:`list` of the guild's scheduled events.
def scheduled_events(self) -> Sequence[ScheduledEvent]:
"""Sequence[:class:`ScheduledEvent`]: Returns a sequence of the guild's scheduled events.
.. versionadded:: 2.0
"""
return list(self._scheduled_events.values())
return utils.SequenceProxy(self._scheduled_events.values())
def get_scheduled_event(self, scheduled_event_id: int, /) -> Optional[ScheduledEvent]:
"""Returns a scheduled event with the given ID.