Implement all mix/max functions in a (hopefully) more optimization amendable way#136307
Conversation
| #[stable(feature = "cmp_min_max_by", since = "1.53.0")] | ||
| pub fn max_by_key<T, F: FnMut(&T) -> K, K: Ord>(v1: T, v2: T, mut f: F) -> T { | ||
| max_by(v1, v2, |v1, v2| f(v1).cmp(&f(v2))) | ||
| if f(&v1) <= f(&v2) { v2 } else { v1 } |
There was a problem hiding this comment.
Hmm, my brain always thinks of this as
| if f(&v1) <= f(&v2) { v2 } else { v1 } | |
| if f(&v1) > f(&v2) { v1 } else { v2 } |
flipping the operation instead of the blocks.
Meh, either way is fine.
scottmcm
left a comment
There was a problem hiding this comment.
I think it's fine to not have a codegen test for this, though if you really wanted to you could probably make one with tuples, since those have lt and friends overridden separately. It'd be fragile, though, since #133984 and the LLVM upgrade are probably going to change our output.
So I think a couple of updated doctests would be nice, then you're good. I have some other comments, but those are all up to your discretion depending whether you think they're worth bothering.
- add tests for `a == b` where missing - try to make all the tests more similar - try to use more illustrative test values
`<` seems to be the "lucky one" for llvm
f9d2512 to
c5835cd
Compare
| /// fn cmp(&self, other: &Self) -> Ordering { Ordering::Equal } | ||
| /// } | ||
| /// | ||
| /// assert_eq!(cmp::min(Equal("v1"), Equal("v2")).0, "v1"); |
There was a problem hiding this comment.
suggestion: also include
| /// assert_eq!(cmp::min(Equal("v1"), Equal("v2")).0, "v1"); | |
| /// assert_eq!(cmp::min(Equal("v1"), Equal("v2")).0, "v1"); | |
| /// assert_eq!(cmp::min(Equal("v2"), Equal("v1")).0, "v2"); |
for emphasis, because cmp::min("v1", "v2") == "v1" too.
There was a problem hiding this comment.
The values were chosen after the function's argument names, this feels confusing...
| /// fn cmp(&self, other: &Self) -> Ordering { Ordering::Equal } | ||
| /// } | ||
| /// | ||
| /// assert_eq!(Equal("self").min(Equal("other")).0, "self"); |
There was a problem hiding this comment.
Ditto here, for emphasis:
| /// assert_eq!(Equal("self").min(Equal("other")).0, "self"); | |
| /// assert_eq!(Equal("self").min(Equal("other")).0, "self"); | |
| /// assert_eq!(Equal("other").min(Equal("self")).0, "other"); |
(though I guess the values work less well here)
|
Ok, this is already better, so my other thoughts aren't blocking. We can always improve the examples more later @bors r+ rollup |
|
Out of curiosity, since everything else in the rollup seemed uninteresting but there were some perf changes: (Not that the results were bad! This might have just actually showed up, which I wouldn't have guessed.) |
This comment has been minimized.
This comment has been minimized.
|
Finished benchmarking commit (2032576): comparison URL. Overall result: ❌✅ regressions and improvements - no action neededBenchmarking this pull request likely means that it is perf-sensitive, so we're automatically marking it as not fit for rolling up. While you can manually mark this PR as fit for rollup, we strongly recommend not doing so since this PR may lead to changes in compiler perf. @bors rollup=never Instruction countThis is the most reliable metric that we have; it was used to determine the overall result at the top of this comment. However, even this metric can sometimes exhibit noise.
Max RSS (memory usage)Results (primary 0.8%, secondary 2.3%)This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.
CyclesThis benchmark run did not return any relevant results for this metric. Binary sizeResults (primary -0.0%, secondary -0.0%)This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.
Bootstrap: 777.851s -> 778.832s (0.13%) |
Previously the graph was like this:
now it looks like this:
(
max*andminmax*are the exact same, i.e. they also use<=andis_le)I'm not sure how to test this, but it should probably be easier for the backend to optimize.
r? @scottmcm
cc #115939 (comment)