Implement nth_back for Zip iterator#152652
Open
veeceey wants to merge 2 commits intorust-lang:mainfrom
Open
Conversation
The `Zip` iterator was missing a specialized `nth_back` implementation, causing it to fall back to the default `DoubleEndedIterator::nth_back` which calls `next_back()` in a loop. This is O(n) even for iterators that support efficient random access. This adds `nth_back` to all three `ZipImpl` specialization levels: - Default: delegates to `super_nth_back` (calls `next_back` in a loop, same as the trait default but going through the Zip dispatch) - TrustedRandomAccessNoCoerce: uses the default - TrustedRandomAccess: uses direct index-based access with proper side-effect handling and panic safety This completes the last three unchecked items from the tracking list in rust-lang#54054 (Zip, Zip in ZipImpl default fn, Zip in ZipImpl where TrustedRandomAccess). The performance impact is most visible when using patterns like `iter_a.zip(iter_b).rev().step_by(n)`, since `step_by` calls `nth` on the underlying iterator, and `Rev::nth` delegates to `nth_back`.
Collaborator
|
rustbot has assigned @Mark-Simulacrum. Use Why was this reviewer chosen?The reviewer was selected based on:
|
Collaborator
|
This comment has been minimized.
This comment has been minimized.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Resolves the remaining Zip items from the tracking list in #54054.
The
Zipiterator was missing anth_backspecialization, so calling.rev().step_by(n)(or any other pattern that ends up callingnth_back) on a zipped iterator would fall through to the defaultDoubleEndedIterator::nth_back, which just loops overnext_back(). ForTrustedRandomAccessiterators like slices, this turns what should be O(1) into O(n).This adds
nth_backat all three specialization levels inZipImpl:zip_impl_general_defaultsmacro): delegates to asuper_nth_backhelper that callsnext_back()in a loop, same as the trait default but properly routed through the Zip dispatch.TrustedRandomAccessNoCoerce: inherits the default.TrustedRandomAccess: O(1) via direct__iterator_get_uncheckedindexing, with proper length-equalization on first backward call, side-effect execution for skipped elements (as required by theMAY_HAVE_SIDE_EFFECTcontract), and panic-safeself.lenupdates before each unchecked access.The implementation mirrors the existing
nthspecialization andnext_backbackward-trimming logic.Test plan
test_zip_nth_backcovering basic nth_back, exhaustion, and unequal-length iteratorstest_zip_nth_back_after_nextcovering interleaved forward/backward iterationtest_zip_rev_nth_uses_nth_backcovering the motivating use case (zip(..).rev().step_by(..))