Skip to content

Conversation

cuviper
Copy link
Member

@cuviper cuviper commented Apr 10, 2025

The standard library is optimized to avoid cloning when returning its last item. We can do the same for our version, and the savings are a lot greater because the waste was multiplied across every split instance as the parallel iterator ran. Now we avoid cloning when returning the last item of any split, so we don't hold any value at all when the count is zero.

Copy link
Collaborator

@adamreichold adamreichold left a comment

Choose a reason for hiding this comment

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

LGTM

As an aside, I wonder whether for Rayon, a variant that requires T: Sync + Clone and packages the element in an Arc would be helpful to avoid the repeated dispatch on the ::Empty case.

@cuviper
Copy link
Member Author

cuviper commented Apr 11, 2025

As an aside, I wonder whether for Rayon, a variant that requires T: Sync + Clone and packages the element in an Arc would be helpful to avoid the repeated dispatch on the ::Empty case.

I think if we add repeat_with, you could easily roll your own to that effect, like repeat_with(move || x.clone()), since that would use F: Fn + Sync which would work based on the captured Sync. I guess we might also want something like repeat_n_with and the same kind of take/zip methods we have on Repeat, so you can have an IndexParallelIterator of this.

@cuviper cuviper added this pull request to the merge queue Apr 11, 2025
Merged via the queue into rayon-rs:main with commit ae07384 Apr 11, 2025
4 checks passed
@cuviper cuviper deleted the repeat_n-clones branch July 22, 2025 20:16
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.

2 participants