diff options
author | 2021-08-22 15:38:44 +0300 | |
---|---|---|
committer | 2021-08-22 15:38:44 +0300 | |
commit | d0d140438c13110bad99a18a7e42e60b325b1176 (patch) | |
tree | a31d6965a97d4630a1fe4eea41a02957d0c87fca | |
parent | Additional comments and tests for slicing (diff) |
Improve cache iteration speed
getitem based iteration included operations that aren't necessary when iterating over the cache continuously. Adding an iter method to the class seems to have improved iteration speed by several orders of magnitude.
-rw-r--r-- | bot/utils/message_cache.py | 10 |
1 files changed, 10 insertions, 0 deletions
diff --git a/bot/utils/message_cache.py b/bot/utils/message_cache.py index f5656cdeb..f68d280c9 100644 --- a/bot/utils/message_cache.py +++ b/bot/utils/message_cache.py @@ -170,6 +170,16 @@ class MessageCache: else: raise TypeError(f"cache indices must be integers or slices, not {type(item)}") + def __iter__(self) -> t.Iterator[Message]: + if self._is_empty(): + return + + if self._start < self._end: + yield from self._messages[self._start:self._end] + else: + yield from self._messages[self._start:] + yield from self._messages[:self._end] + def __len__(self): """Get the number of non-empty cells in the cache.""" if self._is_empty(): |