Skip to content

Commit

Permalink
Optimize Iterator:last() for PyList & PyTuple
Browse files Browse the repository at this point in the history
  • Loading branch information
bschoenmaeckers committed Jan 29, 2025
1 parent f89b5f7 commit 9a92993
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 0 deletions.
1 change: 1 addition & 0 deletions newsfragments/4878.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Optimizes `last` for `BoundListIterator`, `BoundTupleIterator` and `BorrowedTupleIterator`
8 changes: 8 additions & 0 deletions src/types/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -750,6 +750,14 @@ impl<'py> Iterator for BoundListIterator<'py> {
None
})
}

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

impl DoubleEndedIterator for BoundListIterator<'_> {
Expand Down
16 changes: 16 additions & 0 deletions src/types/tuple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,14 @@ impl<'py> Iterator for BoundTupleIterator<'py> {
let len = self.len();
(len, Some(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 +475,14 @@ impl<'a, 'py> Iterator for BorrowedTupleIterator<'a, 'py> {
let len = self.len();
(len, Some(len))
}

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

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

0 comments on commit 9a92993

Please sign in to comment.