Skip to content

Conversation

Dan54
Copy link

@Dan54 Dan54 commented Oct 15, 2025

Fix #147704 by replacing next_back() calls in a loop with one call to nth_back().

This also modifies the safety conditions for the (unstable) TrustedRandomAccessNoCoerce trait by allowing calls to self.nth_back(n) and self.advance_back_by(n) after a call to self.__iterator_get_unchecked() provided they don't move the tail too far.

A grep found that the TrustedRandomAccessNoCoerce trait is implemented by the following structs:

  • alloc::collections::vec_deque::{Iter<T>, IterMut<T>}
  • alloc::vec::IntoIter<T>
  • core::array::IntoIter<T, N>
  • core::char::{CaseMappingIter, ToLowercase, ToUppercase}
  • core::iter::{Cloned<I>, Copied<I>, Enumerate<I>, Fused<I>, Map<I, F>, Skip<I>, Zip<A, B>}
  • core::ops::Range<_>
  • core::range::IterRange<_>
  • core::slice::{Chunks<T>, ChunksExact<T>, ChunksExactMut<T>, ChunksMut<T>, Iter<T>, IterMut<T>, RChunks<T>, RChunksExact<T>, RChunksExactMut<T>, RChunksMut<T>, Windows<T>}
  • core::str::Bytes

I believe this is sound, as the implementations of advance_back_by() for the above do essentially the same thing as repeated next_back() calls, but better optimized.

If there is an issue, a partial fix could be implemented by using the new version as the default and specializing TrustedRandomAccessNoCoerce to use the current implementation.

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Oct 15, 2025
@rustbot
Copy link
Collaborator

rustbot commented Oct 15, 2025

r? @joboet

rustbot has assigned @joboet.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@Dan54 Dan54 marked this pull request as draft October 15, 2025 21:38
@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Oct 15, 2025
@Dan54 Dan54 marked this pull request as ready for review October 15, 2025 21:39
@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Oct 15, 2025
@Dan54
Copy link
Author

Dan54 commented Oct 15, 2025

Just realised that if a.len() < b.len() then a.next_back() is called before equalizing the lengths. I'm writing a quick fix.

Comment on lines +541 to +542
/// Calls to `self.nth_back(n)` are counted as `n + 1` calls to `self.next_back()`, and
/// calls to `self.advance_back_by(n)` as `n` calls.
Copy link
Member

Choose a reason for hiding this comment

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

While this is true, I'm not sure what it's doing here.

Copy link
Author

Choose a reason for hiding this comment

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

Point 3 of TrustedRandomAccess's safety requirements says (paraphrased) that you can't use next_back() to move the tail to an element that has already been returned by __iterator_get_unchecked(). This extends that requirement to calls to nth_back() and advance_back_by().

iter4.next_back();
assert_eq!(&log.take(), &[0, 1, 1, 1, 0, 1]);
}

Copy link
Member

Choose a reason for hiding this comment

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

This tests for the ordering of calls to the constituent iterators of a Zip, but I don't think that ordering is specified, nor do I think it should be...

Copy link
Author

@Dan54 Dan54 Oct 16, 2025

Choose a reason for hiding this comment

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

I didn't want to break anything, but if the call order is unspecified then we can get cleaner code here, and faster code for nth() and nth_back(), so I've removed the test.

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

Labels

S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

core::Zip::next_back() slow for iterators of different lengths

4 participants