-
Notifications
You must be signed in to change notification settings - Fork 13k
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
Produce better size_hints from Flatten & FlatMap #48544
Conversation
Adds a const SIZE_HINT to IntoIterator, allowing a good estimate before the actual iterators are available.
r? @BurntSushi (rust_highfive has picked a reviewer for you, use r? to override) |
/// assert_eq!(<Option<String> as IntoIterator>::SIZE_HINT, (0, Some(1))); | ||
/// ``` | ||
#[unstable(feature = "typelevel_size_hint", issue = "7777777")] | ||
const SIZE_HINT: (usize, Option<usize>) = (0, None); |
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.
A style nit, but why are you declaring the const SIZE_HINT
s beneath all of the method definitions? They kind of feel like they should be before method definitions?
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.
I had it up there originally, but then it felt like it was pretending to be too important, and that the things one actually needs to implement should be first. I'm happy to use either.
cc @rust-lang/libs This PR is adding a new (but unstable) |
Hm I'm sort of hesitant about this in the sense that it seems like there's no end to tiny tweaks to iterators and associated traits and items. Are we eventually going to grow a suite of associated items for all the various properties of iterators? Will we one day need a hint like this on the trait itself rather than the into trait? While I also understand the use case here I think it seems a little odd to be an associated constant because size hints are otherwise always associated with an actual instance, so having a difference here seems slightly odd |
How should this PR be handled? Do we wish to promote this to a team discussion? |
@alexcrichton I can't disagree with anything you said 🙂
It doesn't surprise me that we keep finding new things, as we have a bunch of things -- like size_hint -- that help alot but where we can't just open Stepanov to get a list of everything to do. I'm sure there are more to come -- we don't have anything for random-access, for example -- but probably also many things we won't bother with -- as a strawman guess, things like whether the length is even. I don't know in which category this PR falls, but I figured it was worth a PR to have the discussion since flatten and flat_map are so often stuck with
Given that every Iterator is IntoIterator, not necessarily. I would expect that some form of specialization or intersections or something would let an iterator that really cared customize it. Allowing that on stable today would need a const on Iterator for the blanket impl to pick up, but I don't think it's urgent. It's particularly not-urgent since it's rarer for an iterator to be able to say something useful anyway. Since iterators need to shrink, the only possible
I don't disagree, but I do think it provides a distinct value not otherwise possible. And having it on IntoIterator I think helps, to some degree, since it's the "I don't actually have an iterator yet" class, and thus getting a prediction, of sorts, from it is plausible. |
the const can't ever be added to Iterator because its not object safe (and there's no Is there evidence of the performance impact of this change? Any benchmarks or anything? |
Ok thanks for the info @scottmcm! I think I'm personally in camp "prefers things to be simple even if slightly deficient" in the sense that I'm not sure the end-all-be-all iterator library where every possible case is optimized as much as possible should be in the standard library. I personally see optimizations like this as great candidates for community crates (so they can be used when necessary), but otherwise I might prefer to approach this from the point of view of "how do we enable this to be done on crates.io" rather than "how to put this in libstd" |
Ok, that sounds like disposition close to me. |
Adds
const SIZE_HINT
to IntoIterator, allowing a good estimate before the actual iterators are available.These two adapters typically produced largely-unhelpful
size_hint
s, as they had no idea what was going to come out of the inner iterators. This change letsflat_map
do as well asfilter_map
withOption
s, and allowsflatten
to get itssize_hint
exactly right in cases likeU = &[T; N]
.