Skip to content
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
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

oberien
Copy link

@oberien oberien commented May 13, 2020

The original functions could not take references to slices and were somewhat limiting in their input. This change makes them more generic by allowing more types to be passed into them.

While this is technically a breaking change, because code which explicitly states the generics won't compile anymore, all other code should be unaffected. Thus, I expect pretty much no code to break in the wild. A minor version increase might be a good idea anyways.

Comment on lines +69 to +70
let a_len = a.clone().into_iter().count();
let b_len = b.clone().into_iter().count();
Copy link
Member

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:

a.clone().into_iter().count()

instead of

let a_iter = a.into_iter()
a_iter.clone().count()

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.

Copy link
Author

@oberien oberien Jan 1, 2024

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, the Iterator + Clone bound willy be satisfied as the reference implements Clone, the reference.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 full Vec will be cloned either way. In general foo.into_iter().clone() will usually have the same effect as foo.clone().into_iter() as IntoIterator::into_iter consumes self, indicating that the resulting iterator struct (usually called IntoIter) will likely wrap / contain the original collection. Thus, cloning that will also clone the underlying buffer.

Copy link
Member

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 as foo.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

Copy link
Member

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.

Copy link
Author

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 accepting impl IntoIterator + Clone, we still allow all &impl IntoIterator to be passed as &T implements Clone. But we can also pass in more types. If we were to require impl 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 untouched IntoIter, 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 while Vec only has 3 (ptr, size, capacity). Cloning the IntoIter 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.

@maxbachmann
Copy link
Member

This certainly makes sense. Internally we already run into this in the string versions. They use StringWrapper only to workaround this. I would consider this a breaking change though.

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