-
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
Specialize internal state of Fuse depending on underlying iterator #70502
Conversation
r? @KodrAus (rust_highfive has picked a reviewer for you, use r? to override) |
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.
This looks great to me! And the nice thing about the Infallible
case is that this goes further than niche optimization -- an uninhabited Err
means the Result
will add no size even if the iterator has no niche.
The remaining question is whether the compiler team wants to let std
use specialization on associated types at all.
That was probably unclear from my comment, but I consider this a part of niche optimization. |
@bors try @rust-timer queue |
Awaiting bors try build completion |
⌛ Trying commit 4ea75af with merge 1a2c08e6d58bb6fd8aef01982c4e673069aeca85... |
☀️ Try build successful - checks-azure |
Queued 1a2c08e6d58bb6fd8aef01982c4e673069aeca85 with parent b9d5ee5, future comparison URL. |
Finished benchmarking try commit 1a2c08e6d58bb6fd8aef01982c4e673069aeca85, comparison URL. |
Perf results look like a clear win across the board! I'm actually a little surprised, since |
In #70374 (comment), @timvermeulen noted that
|
It looks like I found the case where the compiler is simply not smart enough. I expected that putting |
Could a |
@llogiq I tried it and it didn't help |
@AnthonyMikh it appears the type system won't allow any reasoning on associated types. I checked various simplified forms of covariant and contravariant types, but their association still stayed invariant. |
Somewhat amusingly, I just found that type specialization was also attempted here when We probably need a warning comment, at least... |
Ping from triage, So, it seems this PR here is blocked, right? It would be nice to actually add the fore-mentioned failing unit test. And maybe create an separate issue for the lifetime check, though. Can @AnthonyMikh help here? |
@crlf0710 it would be nice, though it is unclear where it should be located. So far all the tests are located at
It turns out there are several issues for that: #68375, #35852, #71547 and possibly some other issues. The first one though contains a comment by RustyYato with a possible workaround which would make |
This is really |
I'm going to relabel as S-blocked, as I think specialization is not going to be changing anytime soon to incorporate more things such as associated types; once that changes we can reopen this PR. Is it accurate to say that this PR cannot proceed then? If so I think we should close it for the time being. |
@Mark-Simulacrum I agree with closing. On the one hand, the compiler is unlikely to resolve the issue with specialization any soon. On the other hand, it is even more unlikely that a PR that makes a covariant structure invariant will be accepted. Thereby, it's better to close this PR until the better times. |
Add a test to ensure Fuse stays covariant When rust-lang#70502 attempted to specialize the data types in `Fuse`, one of the problems we found was that it broke variance. This was also realized when `Fuse` was first added, rust-lang#35656 (diff), but now this PR adds a test so we don't forget again.
This PR pushes #70366 further using approach outlined in comment by @cuviper. After this PR
Fuse
holds notOption<I>
butResult<I, State>
whereState
is()
for regular iterators andInfallible
for fused iterators. This enables niche optimisations as well as eliminating excessive code branches for fused iterators in predictable way without relying onunsafe
. Also it concentrates all the specialization in one place and thus removes the need to reimplement methods ofIterator
andDoubleEndedIterator
forI: FusedIterator
.All the caveats listed in #70374 apply here as well.