Uh oh!
There was an error while loading. Please reload this page.
Remove duplicated code from Iterator::{ne, lt, le, gt, ge} - #59262
Conversation
rust-highfive
commented
Mar 17, 2019
r? @Kimundi (rust_highfive has picked a reviewer for you, use r? to override) |
| Some(Ordering::Greater) => return false, | ||
| None => return false, | ||
| } | ||
| match self.partial_cmp(other) { |
There was a problem hiding this comment.
Why not self.partial_cmp(other) == Some(Ordering::Less)?
There was a problem hiding this comment.
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.
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.
So is it worth to also fix std::cmp::PartialOrd?
There was a problem hiding this comment.
What do you mean exactly?
What about |
hellow554
commented
Mar 18, 2019
You could also redirect |
timvermeulen
commented
Mar 18, 2019
@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! |
hellow554
commented
Mar 19, 2019
I think it's worth it, because they share the exact same code (except the |
timvermeulen
commented
Mar 19, 2019
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 |
Centril
commented
Mar 30, 2019
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) |
bors
commented
Apr 2, 2019
📌 Commit 075b269 has been approved by |
bors
commented
Apr 2, 2019
💡 This pull request was already approved, no need to approve it again.
|
bors
commented
Apr 2, 2019
📌 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::netoIterator::eqandIterator::{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/geimplementations don't seem to be compiled optimally, but simply having them callpartial_cmpseems to be an improvement.See #44729 for a previous discussion.