Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize Iterator::last() for PyList & PyTuple and Iterator::count() for PyDict, PyList, PyTuple & PySet #4878

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions newsfragments/4878.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- Optimizes `last` for `BoundListIterator`, `BoundTupleIterator` and `BorrowedTupleIterator`
- Optimizes `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
16 changes: 16 additions & 0 deletions src/types/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -631,6 +631,22 @@ 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
Self: Sized,
{
self.next_back()
}

#[inline]
#[cfg(all(Py_GIL_DISABLED, not(feature = "nightly")))]
fn fold<B, F>(mut self, init: B, mut f: F) -> B
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
32 changes: 32 additions & 0 deletions src/types/tuple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,22 @@ impl<'py> Iterator for BoundTupleIterator<'py> {
let len = self.len();
(len, Some(len))
}

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

#[inline]
fn last(mut self) -> Option<Self::Item>
where
Self: Sized,
{
self.next_back()
}
}

impl DoubleEndedIterator for BoundTupleIterator<'_> {
Expand Down Expand Up @@ -467,6 +483,22 @@ impl<'a, 'py> Iterator for BorrowedTupleIterator<'a, 'py> {
let len = self.len();
(len, Some(len))
}

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

#[inline]
fn last(mut self) -> Option<Self::Item>
where
Self: Sized,
{
self.next_back()
}
}

impl DoubleEndedIterator for BorrowedTupleIterator<'_, '_> {
Expand Down
Loading