-
Notifications
You must be signed in to change notification settings - Fork 13.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #103556 - clubby789:specialize-option-partial-eq, r=sco…
…ttmcm Manually implement PartialEq for Option<T> and specialize non-nullable types This PR manually implements `PartialEq` and `StructuralPartialEq` for `Option`, which seems to produce slightly better codegen than the automatically derived implementation. It also allows specializing on the `core::num::NonZero*` and `core::ptr::NonNull` types, taking advantage of the niche optimization by transmuting the `Option<T>` to `T` to be compared directly, which can be done in just two instructions. A comparison of the original, new and specialized code generation is available [here](https://godbolt.org/z/dE4jxdYsa).
- Loading branch information
Showing
4 changed files
with
128 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// compile-flags: -O -Zmerge-functions=disabled | ||
|
||
#![crate_type = "lib"] | ||
|
||
extern crate core; | ||
use core::num::{NonZeroU32, NonZeroI64}; | ||
use core::ptr::NonNull; | ||
|
||
// CHECK-lABEL: @non_zero_eq | ||
#[no_mangle] | ||
pub fn non_zero_eq(l: Option<NonZeroU32>, r: Option<NonZeroU32>) -> bool { | ||
// CHECK: start: | ||
// CHECK-NEXT: icmp eq i32 | ||
// CHECK-NEXT: ret i1 | ||
l == r | ||
} | ||
|
||
// CHECK-lABEL: @non_zero_signed_eq | ||
#[no_mangle] | ||
pub fn non_zero_signed_eq(l: Option<NonZeroI64>, r: Option<NonZeroI64>) -> bool { | ||
// CHECK: start: | ||
// CHECK-NEXT: icmp eq i64 | ||
// CHECK-NEXT: ret i1 | ||
l == r | ||
} | ||
|
||
// CHECK-lABEL: @non_null_eq | ||
#[no_mangle] | ||
pub fn non_null_eq(l: Option<NonNull<u8>>, r: Option<NonNull<u8>>) -> bool { | ||
// CHECK: start: | ||
// CHECK-NEXT: icmp eq {{(i8\*|ptr)}} | ||
// CHECK-NEXT: ret i1 | ||
l == r | ||
} |