Skip to content
This repository was archived by the owner on May 3, 2026. It is now read-only.

Improve performance characteristics of Vector iterators to better match Vec iterators - #104

Open
jessegrosjean wants to merge 2 commits into
bodil:masterfrom
jessegrosjean:master
Open

Improve performance characteristics of Vector iterators to better match Vec iterators#104
jessegrosjean wants to merge 2 commits into
bodil:masterfrom
jessegrosjean:master

Conversation

@jessegrosjean

Copy link
Copy Markdown

In particular provided implementations of len, count, nth, and nth_back for Iter, IterMut, Chunks, ChunksMut that make use of front_index and back_index to improve performance.

Similar work might also be useful for ConsumingIter, but I couldn't easily trace that code to see where/if such improvements would make sense.

@timotree3 timotree3 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just passing by and I noticed what I believe to be multiple bugs in this implementation. All my comments apply to both the Iterator and DoubleEndedIterator implementations for all the mutability variants.

Comment thread src/vector/mod.rs
if n >= self.len() {
None
} else {
self.front_index += n;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if this overflows?

Comment thread src/vector/mod.rs

fn nth(&mut self, n: usize) -> Option<Self::Item> {
if n >= self.len() {
None

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't believe this shortcut is correct. It introduces a bug.

Before this change, this works:

let vec = vector![1, 2, 3];
let mut iter = vec.iter();
assert_eq!(iter.nth(3), None);
assert_eq!(iter.next(), None);

Afterwards, it panics because the call to next() returns Some(1).

Since the implementation of next() already has a check for exhaustion, I would remove this if altogether and replace it with its else case.

Comment thread src/vector/mod.rs
impl<'a, A: Clone> ExactSizeIterator for Iter<'a, A> {}
impl<'a, A: Clone> ExactSizeIterator for Iter<'a, A> {
fn len(&self) -> usize {
self.back_index - self.front_index

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if this underflows?

Suggested change
self.back_index - self.front_index
self.back_index.saturating_sub(self.front_index)

Comment thread src/vector/mod.rs
impl<'a, A: Clone> ExactSizeIterator for IterMut<'a, A> {}
impl<'a, A: Clone> ExactSizeIterator for IterMut<'a, A> {
fn len(&self) -> usize {
self.back_index - self.front_index

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
self.back_index - self.front_index
self.back_index.saturing_sub(self.front_index)

Comment thread src/vector/mod.rs

impl<'a, A: Clone> ExactSizeIterator for Chunks<'a, A> {
fn len(&self) -> usize {
self.back_index - self.front_index

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
self.back_index - self.front_index
self.back_index.saturing_sub(self.front_index)

Comment thread src/vector/mod.rs

impl<'a, A: Clone> ExactSizeIterator for ChunksMut<'a, A> {
fn len(&self) -> usize {
self.back_index - self.front_index

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
self.back_index - self.front_index
self.back_index.saturing_sub(self.front_index)

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants