Skip to content

Commit

Permalink
Optimize Iterator::count() for PyDict, PyList, PyTuple & PySet
Browse files Browse the repository at this point in the history
  • Loading branch information
bschoenmaeckers committed Jan 29, 2025
1 parent cbdca14 commit 482525b
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 1 deletion.
3 changes: 2 additions & 1 deletion newsfragments/4878.added.md
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
Optimizes `last` for `BoundListIterator`, `BoundTupleIterator` and `BorrowedTupleIterator`
- Optimizes `last` for `BoundListIterator`, `BoundTupleIterator` and `BorrowedTupleIterator`
- Optimize `Iterator::count()` for `PyDict`, `PyList`, `PyTuple` & `PySet`
16 changes: 16 additions & 0 deletions src/types/dict.rs
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,14 @@ impl<'py> Iterator for BoundDictIterator<'py> {
(len, Some(len))
}

#[inline]
fn count(self) -> usize
where
Self: Sized,
{
self.len()
}

#[inline]
#[cfg(Py_GIL_DISABLED)]
fn fold<B, F>(mut self, init: B, mut f: F) -> B
Expand Down Expand Up @@ -736,6 +744,14 @@ mod borrowed_iter {
let len = self.len();
(len, Some(len))
}

#[inline]
fn count(self) -> usize
where
Self: Sized
{
self.len()
}
}

impl ExactSizeIterator for BorrowedDictIter<'_, '_> {
Expand Down
8 changes: 8 additions & 0 deletions src/types/frozenset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,14 @@ impl<'py> Iterator for BoundFrozenSetIterator<'py> {
fn size_hint(&self) -> (usize, Option<usize>) {
(self.remaining, Some(self.remaining))
}

#[inline]
fn count(self) -> usize
where
Self: Sized,
{
self.len()
}
}

impl ExactSizeIterator for BoundFrozenSetIterator<'_> {
Expand Down
8 changes: 8 additions & 0 deletions src/types/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -631,6 +631,14 @@ impl<'py> Iterator for BoundListIterator<'py> {
(len, Some(len))
}

#[inline]
fn count(self) -> usize
where
Self: Sized,
{
self.len()
}

#[inline]
fn last(mut self) -> Option<Self::Item>
where
Expand Down
8 changes: 8 additions & 0 deletions src/types/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,14 @@ impl<'py> Iterator for BoundSetIterator<'py> {
fn size_hint(&self) -> (usize, Option<usize>) {
(self.remaining, Some(self.remaining))
}

#[inline]
fn count(self) -> usize
where
Self: Sized,
{
self.len()
}
}

impl ExactSizeIterator for BoundSetIterator<'_> {
Expand Down
16 changes: 16 additions & 0 deletions src/types/tuple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,14 @@ impl<'py> Iterator for BoundTupleIterator<'py> {
(len, Some(len))
}

#[inline]
fn count(self) -> usize
where
Self: Sized,
{
self.len()
}

#[inline]
fn last(mut self) -> Option<Self::Item>
where
Expand Down Expand Up @@ -476,6 +484,14 @@ impl<'a, 'py> Iterator for BorrowedTupleIterator<'a, 'py> {
(len, Some(len))
}

#[inline]
fn count(self) -> usize
where
Self: Sized,
{
self.len()
}

#[inline]
fn last(mut self) -> Option<Self::Item>
where
Expand Down

0 comments on commit 482525b

Please sign in to comment.