-
Notifications
You must be signed in to change notification settings - Fork 13.9k
Specialize Iterator::eq{_by}
for TrustedLen
iterators
#137122
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
Merged
Merged
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 hidden or 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
Oops, something went wrong.
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.
Just saw this in This Week in Rust and had a drive by question -- is this
match
construction equivalent to:? If so, is it worth changing? To my naive eyes, the latter generates different assembly. Not sure if it's meaningfully faster or anything, but it also looks like it might be simpler.
Additionally, I don't know how common the case is where someone calls
eq()
on two iterators where one'ssize_hint()
is(usize::MAX, Some(usize::MAX))
and the other is(usize::MAX, None)
, but it would be "even more efficient" if we comparedsize_hint().0 != size_hint().0
, even though we would specialize for fewer cases ⚖️Anyway, just wondering if either of these would be helpful and figured the folks already on this thread would be the experts!😅
Uh oh!
There was an error while loading. Please reload this page.
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.
Yeah, you're absolutely right! I do think the
match
version is slightly easier to reason about, but yours is definitely more concise, and functionally the same.Seems like if you lay out both functions in a way similar to how they're used here, you do get the (exact) same codegen:
https://godbolt.org/z/MTqboa4M1
So it's worth changing only if you think it improves readability, I don't think it'll have any effect on perf. I'm not an official member of any Rust team, just happen to be the PR's author, but you can definitely go ahead and open a PR with your suggestion, and if it makes sense to the libs team then that's absolutely fine by me, FWIW :)
TrustedLen
's safety docs explicitly say that the trait's consumers must inspect the upper bound, but the wording is a little unclear so not sure if that's a strict requirement or not.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.
Oh, good call! That's a clever catch and really surprising haha.
Yeah, I find it slightly more readable, if for no other reason than the current
match
makes me ask, "It looks like we're inliningPartialEq for Option<T>
-- I wonder why," but I'm also biased towards my own interpretations and don't feel strongly enough to go through the whole CI and review process!Ah, yes, thanks for bringing that up! I did read that, but I totally forgot about it 😆 Thanks for helping me understand!