-
-
Notifications
You must be signed in to change notification settings - Fork 38
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make generic_ functions taking &impl IntoIter more generic #45
Open
oberien
wants to merge
1
commit into
rapidfuzz:main
Choose a base branch
from
oberien:master
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+16
−16
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there any reason why this uses:
instead of
I feel like the former might need to copy the underlying buffer, while the second only copies the iterator. I could be wrong about this though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The current code takes a reference to an iterator. This PR allows additionally passing in anything that implements
Iterator
. If you pass in a reference to the iterator as previously, theIterator + Clone
bound willy be satisfied as the reference implementsClone
, thereference.clone()
code will clone the reference, not the underlying container.If, however, you use the more generic trait bounds to directly pass in a container like
Vec
, then the fullVec
will be cloned either way. In generalfoo.into_iter().clone()
will usually have the same effect asfoo.clone().into_iter()
asIntoIterator::into_iter
consumes self, indicating that the resulting iterator struct (usually calledIntoIter
) will likely wrap / contain the original collection. Thus, cloning that will also clone the underlying buffer.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm I would have expected that worst case
foo.into_iter().clone()
does the same asfoo.clone().into_iter()
, since it stores the thing it's iterating over and essentially passes on the clone. However couldn't the iterator decide to use something like a refcounted storage internally? Not sure whether people actually do something like this in praxis though.A second advantage that just came to my mind is that the iterator is a more specific type and so it allows us to avoid monomorphization by moving the implementation into a separate function and forwarding the iterator:
https://godbolt.org/z/dE4drsTqG
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note that I could absolutely be wrong on these things, since I am pretty new to rust.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry for the late response. There is a very simple reason that I require
Iter1: Clone
: Backwards compatibility. Currently, only&impl IntoIterator
are allowed. By acceptingimpl IntoIterator + Clone
, we still allow all&impl IntoIterator
to be passed as&T
implementsClone
. But we can also pass in more types. If we were to requireimpl IntoIterator where Iter: Clone
, not all previously allowed types can be passed in, as we add an additional requirement that wasn't there before. Thus, afaict this change only requires a minor version update.If we ignore compatibility and focus only on performance, either can result in better performance, depending on the implementation.
std::array::IntoIter
contains both the array and a range. As such, if no element was consumed so far, we need to clone the range in addition to all array elements. If, however, elements were already consumed, only the unconsumed elements are cloned. In our case we clone the untouchedIntoIter
, which would be more expensive.std::slice::Iter
&std::slice::IterMut
contain the start-pointer and end-pointer of the iterated-over slice. A slice itself is a pointer and a length. Cloning a slice vs its iterator is equal performance-wise.std::vec::IntoIter
contains 5 pointers / usize whileVec
only has 3 (ptr, size, capacity). Cloning theIntoIter
clones the elements as well as more pointers compared to cloning just the vec.In the end, cloning a few dwords is negligible performance-wise. Either trait bound will in general result in very similar performance.