-
Notifications
You must be signed in to change notification settings - Fork 13.2k
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
Remove duplicated code from Iterator::{ne, lt, le, gt, ge} #59262
Conversation
r? @Kimundi (rust_highfive has picked a reviewer for you, use r? to override) |
src/libcore/iter/traits/iterator.rs
Outdated
Some(Ordering::Greater) => return false, | ||
None => return false, | ||
} | ||
match self.partial_cmp(other) { |
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.
Why not self.partial_cmp(other) == Some(Ordering::Less)
?
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, I see, you probably wanted to keep it consistent across le/lt/ge/gt
... I think that's okay then
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.
That was indeed the rationale, but looking at it again, it might actually be worth it. It's a lot simpler and it's still consistent between lt
/gt
.
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.
So is it worth to also fix std::cmp::PartialOrd
?
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.
What do you mean exactly?
What about |
You could also redirect |
@hellow554 Unfortunately we can't forward There's indeed still some code duplication left but I haven't found a nice way to get rid of it without sacrificing readability. I'm open to suggestions though! |
I think it's worth it, because they share the exact same code (except the |
As much as I'd like to get rid of all the duplicate code here, I think that particular abstraction is the wrong one to make (in part because it's not obvious to me whether it will always compile the same way, in case the compiler doesn't figure out the |
r? @scottmcm |
Sure, making the way this is implemented match how le/gt/etc are implemented on PartialOrd sounds good to me. @bors r+ rollup (We might do something different if we could abort early for lengths, but we can't for general iterators) |
📌 Commit 075b269 has been approved by |
💡 This pull request was already approved, no need to approve it again.
|
📌 Commit 075b269 has been approved by |
…scottmcm Remove duplicated code from Iterator::{ne, lt, le, gt, ge} This PR delegates `Iterator::ne` to `Iterator::eq` and `Iterator::{lt, le, gt, ge}` to `Iterator::partial_cmp`. Oddly enough, this change actually simplifies the generated assembly [in some cases](https://rust.godbolt.org/z/riBtNe), although I don't understand assembly well enough to see if the longer assembly is doing something clever. I also added two extremely simple benchmarks: ``` // before test iter::bench_lt ... bench: 98,404 ns/iter (+/- 21,008) test iter::bench_partial_cmp ... bench: 62,437 ns/iter (+/- 5,009) // after test iter::bench_lt ... bench: 61,757 ns/iter (+/- 8,770) test iter::bench_partial_cmp ... bench: 62,151 ns/iter (+/- 13,753) ``` I have no idea why the current `lt`/`le`/`gt`/`ge` implementations don't seem to be compiled optimally, but simply having them call `partial_cmp` seems to be an improvement. See rust-lang#44729 for a previous discussion.
Rollup of 8 pull requests Successful merges: - #59262 (Remove duplicated code from Iterator::{ne, lt, le, gt, ge}) - #59286 (Refactor async fn return type lowering) - #59444 (Implement useful steps_between for all integers) - #59452 (Speed up rustdoc run a bit) - #59533 (Support allocating iterators with arenas) - #59585 (Fixes for shallow borrows) - #59607 (Renames `EvalErrorKind` to `InterpError`) - #59613 (SGX target: convert a bunch of panics to aborts) Failed merges: - #59630 (Shrink `mir::Statement`.) r? @ghost
This PR delegates
Iterator::ne
toIterator::eq
andIterator::{lt, le, gt, ge}
toIterator::partial_cmp
.Oddly enough, this change actually simplifies the generated assembly in some cases, although I don't understand assembly well enough to see if the longer assembly is doing something clever.
I also added two extremely simple benchmarks:
I have no idea why the current
lt
/le
/gt
/ge
implementations don't seem to be compiled optimally, but simply having them callpartial_cmp
seems to be an improvement.See #44729 for a previous discussion.