-
Notifications
You must be signed in to change notification settings - Fork 13.1k
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
Implement all mix/max functions in a (hopefully) more optimization amendable way #136307
Conversation
library/core/src/cmp.rs
Outdated
@@ -1535,7 +1529,7 @@ pub fn max_by<T, F: FnOnce(&T, &T) -> Ordering>(v1: T, v2: T, compare: F) -> T { | |||
#[must_use] | |||
#[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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
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.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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 |
…iaskrgr Rollup of 6 pull requests Successful merges: - rust-lang#130514 (Implement MIR lowering for unsafe binders) - rust-lang#135684 (docs: Documented Send and Sync requirements for Mutex + MutexGuard) - rust-lang#136307 (Implement all mix/max functions in a (hopefully) more optimization amendable way) - rust-lang#136360 (Stabilize `once_wait`) - rust-lang#136364 (document that ptr cmp is unsigned) - rust-lang#136374 (Add link attribute for Enzyme's LLVMRust FFI) r? `@ghost` `@rustbot` modify labels: rollup
…iaskrgr Rollup of 6 pull requests Successful merges: - rust-lang#130514 (Implement MIR lowering for unsafe binders) - rust-lang#135684 (docs: Documented Send and Sync requirements for Mutex + MutexGuard) - rust-lang#136307 (Implement all mix/max functions in a (hopefully) more optimization amendable way) - rust-lang#136360 (Stabilize `once_wait`) - rust-lang#136364 (document that ptr cmp is unsigned) - rust-lang#136374 (Add link attribute for Enzyme's LLVMRust FFI) r? `@ghost` `@rustbot` modify labels: rollup
Rollup merge of rust-lang#136307 - WaffleLapkin:minminmin, r=scottmcm Implement all mix/max functions in a (hopefully) more optimization amendable way Previously the graph was like this: ``` min -> Ord::min -> min_by -> match on compare() (in these cases compare = Ord::cmp) ^ | min_by_key ``` now it looks like this: ``` min -> Ord::min -> `<=` <- min_by_key min_by -> `Ordering::is_le` of `compare()` ``` (`max*` and `minmax*` are the exact same, i.e. they also use `<=` and `is_le`) I'm not sure how to test this, but it should probably be easier for the backend to optimize. r? `@scottmcm` cc rust-lang#115939 (comment)
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)