Skip to content

Implement nth and nth_back to provide a O(1) way of skipping through elements#142

Merged
NULLx76 merged 1 commit into
NULLx76:mainfrom
cdellacqua:nth
Jul 11, 2025
Merged

Implement nth and nth_back to provide a O(1) way of skipping through elements#142
NULLx76 merged 1 commit into
NULLx76:mainfrom
cdellacqua:nth

Conversation

@cdellacqua

Copy link
Copy Markdown
Contributor

By providing an override for the default implementation of nth (and nth_back), which in turn is used by skip, there is a noticeable improvement in performance, as jumping through elements becomes a constant time operation.

The implementation applies only to RingBufferIterator and RingBufferMutIterator, as they don't drop skipped elements.

Benchmarks

With 16 elements in the ringbuffer, criterion shows a 47% performance improvement

image

While with 1024 elements, criterion shows a 97% performance improvement

image

@dzmitry-lahoda

This comment was marked as off-topic.

@NULLx76

NULLx76 commented Jul 11, 2025

Copy link
Copy Markdown
Owner

This PR looks straightforward and good, thanks for the work!

@dzmitry-lahoda I've recently just been quite busy with university, no need to be inflammatory :)

@NULLx76 NULLx76 merged commit 344eba1 into NULLx76:main Jul 11, 2025
NULLx76 added a commit that referenced this pull request Jul 11, 2025
Implement nth and nth_back to provide a O(1) way of skipping through elements
@cdellacqua cdellacqua deleted the nth branch August 15, 2025 14:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants